android实现简单的活动转盘

 更新时间:2021年10月10日 11:23:56   作者:勿安欧  
这篇文章主要为大家详细介绍了android实现简单的活动转盘,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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 实现锚点定位思路详解

    本篇文章就使用tablayout、scrollview来实现android锚点定位的功能。通过<a href="#head" rel="external nofollow" > 去设置页面内锚点定位跳转。具体实现思路大家跟随脚本之家小编一起通过本文看下吧
    2018-07-07
  • android studio后台服务使用详解

    android studio后台服务使用详解

    这篇文章主要为大家详细介绍了android studio后台服务,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • Android中的xml解析介绍

    Android中的xml解析介绍

    大家好,本篇文章主要讲的是Android中的xml解析介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • Android中库项目的使用方法图文介绍

    Android中库项目的使用方法图文介绍

    类似开发其他Java应用一样,我们可以将可复用的代码,打成一个jar包,供所有需要的项目使用。这样,可以解决很大一部分代码复用的问题,本文将详细介绍,需要了解的朋友可以参考下
    2012-12-12
  • Android应用开发中自定义ViewGroup的究极攻略

    Android应用开发中自定义ViewGroup的究极攻略

    这里我们要演示的自定义ViewGroup中将实现多种方式排列和滑动等效果,并且涵盖子View之间Touch Event的拦截与处理等问题,完全干货,下面就为大家送上Android应用开发中自定义ViewGroup的究极实例攻略
    2016-05-05
  • Android自定义控件实现折线图

    Android自定义控件实现折线图

    这篇文章主要为大家详细介绍了Android自定义控件实现折线图,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • Flutter Ping检查服务器通讯信号强度实现步骤

    Flutter Ping检查服务器通讯信号强度实现步骤

    这篇文章主要为大家介绍了Flutter Ping检查服务器通讯信号强度实现步骤详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • Android 获取内外SD卡路径几种方法总结

    Android 获取内外SD卡路径几种方法总结

    这篇文章主要介绍了Android 获得内外SD卡路径几种方法总结的相关资料,需要的朋友可以参考下
    2016-12-12
  • Android实现边录边播应用

    Android实现边录边播应用

    这篇文章主要为大家详细介绍了Android实现边录边播应用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • Android使用WebView实现文件下载功能

    Android使用WebView实现文件下载功能

    这篇文章主要为大家详细介绍了Android使用WebView实现文件下载功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05

最新评论