广告
返回顶部
首页 > 资讯 > 移动开发 >android实现简单的活动转盘
  • 419
分享到

android实现简单的活动转盘

2024-04-02 19:04:59 419人浏览 薄情痞子
摘要

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

本文实例为大家分享了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();
                }
            }
        });
    }

    
    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>

效果:

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

--结束END--

本文标题: android实现简单的活动转盘

本文链接: https://www.lsjlt.com/news/137675.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作