温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android如何制作抽奖轮盘

发布时间:2021-06-22 11:24:46 来源:亿速云 阅读:217 作者:小新 栏目:移动开发

这篇文章主要介绍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;
//开始转动时候的角度,初始值为0
private 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();
  }
};


@Override
protected 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,
          RotateAnimation.RELATIVE_TO_SELF, 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, 0, ONE_WHEEL_TIME);
}

@Override
public 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如何制作抽奖轮盘”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI