Android抽奖轮盘的制作方法

Android抽奖轮盘的制作方法,第1张

概述本文实例为大家分享了Android抽奖轮盘的具体代码,供大家参考,具体内容如下

本文实例为大家分享了AndroID抽奖轮盘的具体代码,供大家参考,具体内容如下

main布局(图片资源请自行寻找,抱歉)

<FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"  androID:layout_wIDth="fill_parent"  androID:layout_height="fill_parent"  androID:layout_gravity="center">  <ImageVIEw    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:src="@drawable/bigwheelgg"    />  <ImageVIEw    androID:ID="@+ID/light"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:src="@drawable/light"    />  <ImageVIEw    androID:ID="@+ID/main_wheel"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:src="@drawable/bigwheel"    />  <ImageVIEw    androID:ID="@+ID/point"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:src="@drawable/point"    /></FrameLayout>

main代码

//设置一个时间常量,此常量有两个作用,1.圆灯视图显示与隐藏中间的切换时间;2.指针转一圈所需要的时间,现设置为500毫秒private static final long ONE_WHEEL_TIME = 500;//记录圆灯视图是否显示的布尔常量private boolean lightsOn = true;//开始转动时候的角度,初始值为0private int startDegree = 0;private ImageVIEw lightIv;private ImageVIEw pointIv;private ImageVIEw wheeliv;//指针转圈圈数数据源private int[] laps = { 5,7,10,15 };//指针所指向的角度数据源,因为有6个选项,所有此处是6个值private int[] angles = { 0,60,120,180,240,300 };//转盘内容数组private String[] lotteryStr = { "索尼PSP","10元红包","谢谢参与","DNF钱包","OPPO MP3","5元红包",};//子线程与UI线程通信的handler对象private Handler mHandler = new Handler() {  public voID handleMessage(androID.os.Message msg) {    switch (msg.what) {      case 0:        if (lightsOn) {          // 设置lightIv不可见          lightIv.setVisibility(VIEw.INVISIBLE);          lightsOn = false;        } else {          // 设置lightIv可见          lightIv.setVisibility(VIEw.VISIBLE);          lightsOn = true;        }        break;      default:        break;    }  };};//监听动画状态的监听器private Animation.AnimationListener al = new Animation.AnimationListener() {  @OverrIDe  public voID onAnimationStart(Animation animation) {    // Todo auto-generated method stub  }  @OverrIDe  public voID onAnimationRepeat(Animation animation) {    // Todo auto-generated method stub  }  @OverrIDe  public voID onAnimationEnd(Animation animation) {    String name = lotteryStr[startDegree % 360 / 60];    Toast.makeText(MainActivity.this,name,Toast.LENGTH_LONG).show();  }};@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.activity_main);  setupVIEws();  flashlights();  pointIv.setonClickListener(new VIEw.OnClickListener() {    @OverrIDe    public voID onClick(VIEw v) {      int lap = laps[(int) (Math.random() * 4)];      int angle = angles[(int) (Math.random() * 6)];      //每次转圈角度增量      int increaseDegree = lap * 360 + angle;      //初始化旋转动画,后面的四个参数是用来设置以自己的中心点为圆心转圈      RotateAnimation rotateAnimation = new RotateAnimation(          startDegree,startDegree + increaseDegree,RotateAnimation.relative_TO_SELF,0.5f,0.5f);      //将最后的角度赋值给startDegree作为下次转圈的初始角度      startDegree += increaseDegree;      //计算动画播放总时间      long time = (lap + angle / 360) * ONE_WHEEL_TIME;      //设置动画播放时间      rotateAnimation.setDuration(time);      //设置动画播放完后,停留在最后一帧画面上      rotateAnimation.setFillAfter(true);      //设置动画的加速行为,是先加速后减速      rotateAnimation.setInterpolator(MainActivity.this,androID.R.anim.accelerate_decelerate_interpolator);      //设置动画的监听器      rotateAnimation.setAnimationListener(al);      //开始播放动画      pointIv.startAnimation(rotateAnimation);    }  });}private voID setupVIEws(){  lightIv = (ImageVIEw) findVIEwByID(R.ID.light);  pointIv = (ImageVIEw) findVIEwByID(R.ID.point);  wheeliv = (ImageVIEw) findVIEwByID(R.ID.main_wheel);}//控制灯圈动画的方法private voID flashlights() {  Timer timer = new Timer();  TimerTask tt = new TimerTask() {    @OverrIDe    public voID run() {      // 向UI线程发送消息      mHandler.sendEmptyMessage(0);    }  };  // 每隔ONE_WHEEL_TIME毫秒运行tt对象的run方法  timer.schedule(tt,ONE_WHEEL_TIME);}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) {  // Inflate the menu; this adds items to the action bar if it is present.  getMenuInflater().inflate(R.menu.main,menu);  return true;}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android抽奖轮盘的制作方法全部内容,希望文章能够帮你解决Android抽奖轮盘的制作方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1144600.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存