android实现简单的活动转盘

本文实例为大家分享了android实现简单活动转盘的具体代码,供大家参考,具体内容如下
页面

public class CircleTurntableActivity extends AppCompatActivity {private Animation mStartAnimation; private ImageView mLuckyTurntable; private booleanisRunning; private boolean mIsLucky = false; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_circle_turntable); mLuckyTurntable = (ImageView) findViewById(R.id.id_lucky_turntable); ImageView mStartBtn = (ImageView) findViewById(R.id.id_start_btn); mStartBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (!isRunning) {isRunning = true; mIsLucky = !mIsLucky; startAnimation(); }}}); }/*** 开启动画* 5秒旋转5圈+中奖所在位置角度*/private void startAnimation() {float toDegree; //结束角度(以实际转盘图为准计算角度)if (mIsLucky) {toDegree = 360 * 5 + 30f; } else {toDegree = 360 * 5 + 90f; }if (mStartAnimation != null) {mStartAnimation.reset(); }// 按中心点旋转 toDegree度// 参数:旋转的开始角度、旋转的结束角度、X轴的伸缩模式、X坐标的伸缩值、Y轴的伸缩模式、Y坐标的伸缩值mStartAnimation = new RotateAnimation(0, toDegree, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); mStartAnimation.setDuration(5000); // 设置旋转时间mStartAnimation.setRepeatCount(0); // 设置重复次数mStartAnimation.setFillAfter(true); // 动画执行完后是否停留在执行完的状态mStartAnimation.setInterpolator(new AccelerateDecelerateInterpolator()); // 动画播放的速度mStartAnimation.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {isRunning = false; Toast.makeText(CircleTurntableActivity.this, mIsLucky ? "精美礼品" : "谢谢参与", Toast.LENGTH_SHORT).show(); }@Overridepublic void onAnimationRepeat(Animation animation) {}}); mLuckyTurntable.startAnimation(mStartAnimation); }}

页面布局

效果:
android实现简单的活动转盘
文章图片

【android实现简单的活动转盘】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读