一聚教程网:一个值得你收藏的教程网站

热门教程

Android抽奖轮盘的制作方法

时间:2022-06-25 23:22:24 编辑:袖梨 来源:一聚教程网

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


 
  
 
  
 
  
 
  
 
 

main代码

//设置一个时间常量,此常量有两个作用,1.圆灯视图显示与隐藏中间的切换时间;2.指针转一圈所需要的时间,现设置为500毫秒
privatestaticfinallongONE_WHEEL_TIME =500;
//记录圆灯视图是否显示的布尔常量
privatebooleanlightsOn =true;
//开始转动时候的角度,初始值为0
privateintstartDegree =0;
 
privateImageView lightIv;
privateImageView pointIv;
privateImageView wheelIv;
 
//指针转圈圈数数据源
privateint[] laps = {5,7,10,15};
//指针所指向的角度数据源,因为有6个选项,所有此处是6个值
privateint[] angles = {0,60,120,180,240,300};
//转盘内容数组
privateString[] lotteryStr = {"索尼PSP","10元红包","谢谢参与","DNF钱包",
    "OPPO MP3","5元红包", };
 
//子线程与UI线程通信的handler对象
privateHandler mHandler =newHandler() {
 
  publicvoidhandleMessage(android.os.Message msg) {
    switch(msg.what) {
      case0:
        if(lightsOn) {
          // 设置lightIv不可见
          lightIv.setVisibility(View.INVISIBLE);
          lightsOn =false;
        }else{
          // 设置lightIv可见
          lightIv.setVisibility(View.VISIBLE);
          lightsOn =true;
        }
        break;
 
      default:
        break;
    }
  };
 
};
 
//监听动画状态的监听器
privateAnimation.AnimationListener al =newAnimation.AnimationListener() {
 
  @Override
  publicvoidonAnimationStart(Animation animation) {
    // TODO Auto-generated method stub
 
  }
 
  @Override
  publicvoidonAnimationRepeat(Animation animation) {
    // TODO Auto-generated method stub
 
  }
 
  @Override
  publicvoidonAnimationEnd(Animation animation) {
    String name = lotteryStr[startDegree %360/60];
    Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
  }
};
 
 
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  setupViews();
  flashLights();
 
  pointIv.setOnClickListener(newView.OnClickListener() {
 
    @Override
    publicvoidonClick(View v) {
      intlap = laps[(int) (Math.random() *4)];
      intangle = angles[(int) (Math.random() *6)];
      //每次转圈角度增量
      intincreaseDegree = lap *360+ angle;
      //初始化旋转动画,后面的四个参数是用来设置以自己的中心点为圆心转圈
      RotateAnimation rotateAnimation =newRotateAnimation(
          startDegree, startDegree + increaseDegree,
          RotateAnimation.RELATIVE_TO_SELF,0.5f,
          RotateAnimation.RELATIVE_TO_SELF,0.5f);
      //将最后的角度赋值给startDegree作为下次转圈的初始角度
      startDegree += increaseDegree;
      //计算动画播放总时间
      longtime = (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);
    }
  });
 
}
 
privatevoidsetupViews(){
  lightIv = (ImageView) findViewById(R.id.light);
  pointIv = (ImageView) findViewById(R.id.point);
  wheelIv = (ImageView) findViewById(R.id.main_wheel);
}
 
//控制灯圈动画的方法
privatevoidflashLights() {
 
  Timer timer =newTimer();
  TimerTask tt =newTimerTask() {
 
    @Override
    publicvoidrun() {
      // 向UI线程发送消息
      mHandler.sendEmptyMessage(0);
 
    }
  };
 
  // 每隔ONE_WHEEL_TIME毫秒运行tt对象的run方法
  timer.schedule(tt,0, ONE_WHEEL_TIME);
}
 
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  returntrue;
}

热门栏目