android如何实现简单的活动转盘

发布时间:2021-10-11 09:21:55 作者:小新
来源:亿速云 阅读:138

这篇文章主要介绍android如何实现简单的活动转盘,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

具体内容如下

页面

public class CircleTurntableActivity extends AppCompatActivity {

    private Animation mStartAnimation;
    private ImageView mLuckyTurntable;
    private boolean   isRunning;

    private boolean mIsLucky = false;

    @Override
    protected 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() {
            @Override
            public 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() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                isRunning = false;
                Toast.makeText(CircleTurntableActivity.this, mIsLucky ? "精美礼品" : "谢谢参与", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mLuckyTurntable.startAnimation(mStartAnimation);
    }

}

页面布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<!--转盘-->
    <ImageView
        android:id="@+id/id_lucky_turntable"
        android:layout_width="613.33px"
        android:layout_height="613.33px"
        android:layout_centerInParent="true"
        android:src="@mipmap/lucky_turntable_bg" />
<!--指针-->
    <ImageView
        android:paddingBottom="40px"
        android:id="@+id/id_start_btn"
        android:layout_width="266.66px"
        android:layout_height="266.66px"
        android:layout_centerInParent="true"
        android:src="@mipmap/lucky_start_icon" />

</RelativeLayout>

效果:

android如何实现简单的活动转盘

以上是“android如何实现简单的活动转盘”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. html5简单拖拽实现自动左右贴边+幸运大转盘
  2. 如何实现Jquery转盘抽奖程序

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

android

上一篇:C语言如何运用回调函数实现计算器

下一篇:js怎样实现简易购物车功能

相关阅读

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

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