欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 美食大转盘详解流程

程序员文章站 2022-03-21 07:52:48
目录效果视频前言美食大转盘初始化surfaceview测量绘制绘制盘块开始旋转转盘停止旋转转盘自定义转盘等份控件引用沉浸式体验效果图reveal animator效果视频自定义转盘代码xml布局代码a...

效果视频

Android 美食大转盘详解流程

前言

你还在为明天吃什么而烦恼嘛
美食大赏帮你解决选择困难症
帮你做出最佳的选择
做吃货,我们是认真的

美食大转盘

本示例使用surfaceview绘制而成,接下来逐步分析,
文末会贴出全部代码``文末会贴出全部代码``文末会贴出全部代码

初始化surfaceview

    private void init() {
        msurfaceholder = this.getholder();
        // 管理surfaceview的生命周期
        msurfaceholder.addcallback(this);
        this.setfocusable(true);
        this.setfocusableintouchmode(true);
        this.setkeepscreenon(true);
    }

测量

通过获取高、宽,然后减去mpadding 的长度获取到控件中心点,然后存储测量的宽、高

    @override
    protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
        super.onmeasure(widthmeasurespec, heightmeasurespec);
        int width = math.min(getmeasuredwidth(), getmeasuredheight());
        mpadding = getpaddingleft();
        mradius = width - mpadding * 2;
        // 中心点
        mcenter = width / 2;
        setmeasureddimension(width, width);
    }

绘制

绘制圆盘背景

设置背景图片

private bitmap mbgbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.turntable_bgcolor );

绘制背景突出部分

private void drawbgcolor() {
        mcanvas.drawcolor(0xffffffff);
        mcanvas.drawbitmap(mbgbitmap, null, new rect(mpadding / 2, mpadding / 2, getmeasuredwidth() - mpadding / 2, getmeasuredwidth() - mpadding / 2), null);
    }

绘制盘块

 private void drawarea() {
        // 起始角度
        float tempangle = mstartangle;
        // 每个盘块绘制的角度
        float sweepangele = 360 / mitemcount;
        for (int i = 0; i < mitemcount; i++) {
            marcpaint.setcolor(itemcolors[i]);
            // 绘制盘块
            mcanvas.drawarc(mrange, tempangle, sweepangele, true, marcpaint);
            // 绘制文本
            drawtext(tempangle, sweepangele, itemcharstring[i]);
            // 绘制图标
            drawicon(tempangle, mbitmaps[i]);

            tempangle += sweepangele;
        }
        mstartangle += mspeed;

        // 如果需要停止,让转速逐渐变小直到0
        if (isshouldend) {
            mspeed -= mdifferspeed;
        }
        if (mspeed <= 0) {
            mspeed = 0;
            isshouldend = false;
        }

    }

绘制盘块内文字

 private void drawtext(float tempangle, float sweepangele, string itemtextstr) {
        path path = new path();
        path.addarc(mrange, tempangle, sweepangele);
        // 利用水平偏移量让文字居中
        float textwidth = mtextpaint.measuretext(itemtextstr);
        int hoffset = (int) (mradius * math.pi / mitemcount / 2 - textwidth / 2);
        // 利用垂直偏移量让文字向圆心靠拢
        int voffset = mradius / 2 / 6;
        mcanvas.drawtextonpath(itemtextstr, path, hoffset, voffset, mtextpaint);
    }

绘制盘块内图标

private void drawicon(float tempangle, bitmap bitmap) {
        // 约束图片的宽度,为直径的1/8,可以作为可变参数设置
        int imgwidth = mradius / 8;
        // 获取弧度值
        float angle = (float) ((tempangle + 360 / mitemcount / 2) * math.pi / 180);
        // 约定图片位置在直径1/4处
        int x = (int) (mcenter + mradius / 4 * math.cos(angle));
        int y = (int) (mcenter + mradius / 4 * math.sin(angle));
        // 确定图片位置
        rect rect = new rect(x - imgwidth / 2, y - imgwidth / 2, x + imgwidth / 2, y + imgwidth / 2);
        mcanvas.drawbitmap(bitmap, null, rect, null);
    }

开始旋转转盘

   public void start(int index) {
        if (isstart()) {
            return;
        }
        if (index < 0) {
            mspeed = 50 * (1 + math.random() * (0.5));
            isshouldend = false;
            return;
        }
    }

停止旋转转盘

    public void stop() {
        if (isshouldend()) {
            return;
        }
        // 将初始角度重置
        mstartangle = 0;
        isshouldend = true;
    }

自定义转盘等份

因为我们要根据需要制作不同等份的转盘,需要跟用户产生交互,所有需要暴露一个方法供用户选择

public void initnumber(int number){
        if (number <= 0){
            return;
        }
        /**
         * 确保为偶数*/
        if (number % 2 == 0){
            initarea(number);
        }
    }

本示例以4,6,8等份为例

private void initarea(int number){
        switch (number){
            case 4:
                fourparts();
                break;
            case 6:
                sixparts();
                break;
            case 8:
                eightparts();
                break;
                default:
                    sixparts();
        }
    }

每一次选择转盘等份,都需要对view进行重绘,需要多次改变图片数量,所以我们将图片设置成一个公共的方法,避免内存浪费

private void fourparts(){
        mitemcount = 4;
        itemcharstring = new string[]{"粉条", "面条", "米饭", "粥",};
        itemimages = new int[]{r.drawable.fen, r.drawable.mian, r.drawable.rice, r.drawable.tang};
        itemcolors = new int[]{0xffffc300, 0xfff17e01, 0xffffc300, 0xfff17e01};
        initimage(mitemcount,itemimages);
    }
private void initimage(int count,int[] item){
        mbitmaps = new bitmap[mitemcount];
        for (int i = 0; i < count; i++) {
            mbitmaps[i] = bitmapfactory.decoderesource(getresources(), item[i]);
        }
    }

控件引用

布局引用

xml布局文件内引用如下,其中中间的指针为图片类型

<com.franzliszt.foodturntable.turntable
        android:id="@+id/turntable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp"
        android:layout_margin="10dp"
        android:layout_centerinparent="true"/>
        
        <imageview
        android:id="@+id/startandend"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerinparent="true"
        android:src="@drawable/start"
        android:onclick="start"/>

activity 引用

默认设置8等份,如果自定义多种等份,默认必须为最大的等份,不然会出现区域空白

turntable.initnumber( 8 );

图标切换

对开始与暂停图标进行切换,根据点击次数进行切换

 public void start(view view) {
        count++;
        /*暂停*/
        if (count % 2 == 0) {
            turntable.stop();
            starticon();
        } else {
            /*开始*/
            turntable.start( -1 );
            stopicon();
        }
    }

更换转盘等份

首先提供三个radiobutton供用户选择,然后通过sharedpreferences进行存储数据,有关sharedpreferences封装的知识,请移步到另一篇博文android sharedpreferences存取操作以及封装详解

private void selectnumber() {
        rg.setoncheckedchangelistener( new radiogroup.oncheckedchangelistener() {
            @override
            public void oncheckedchanged(radiogroup group, int checkedid) {
                switch (checkedid) {
                    case r.id.fourparts:
                        sp.putdata( context, "num", 4 );
                        break;
                    case r.id.sixparts:
                        sp.putdata( context, "num", 6 );
                        break;
                    case r.id.eightparts:
                        sp.putdata( context, "num", 8 );
                        break;
                }
            }
        } );
    }

然后取出数据,并回调等份数值即可

public void confirm(view view) {
        revealanim( view );
        loadingview.setvisibility( view.visible );
        startloading();
        startpercentmockthread();
        int num = (int) sp.getdata( context, "num", 0 );
        turntable.initnumber( num );
    }

沉浸式体验

效果图

Android 美食大转盘详解流程

沉浸式体验即标题栏与系统状态栏主题一致我们分为以下几个步骤完成以上效果

建立一个样式

首先我们需要建立一个标题栏为空的样式,我们有两种方式去实现
第一种,使用系统提供样式

<style name="notactionbar_1" parent="theme.appcompat.light.noactionbar">
        <item name="colorprimary">@color/colorprimary</item>
        <item name="colorprimarydark">@color/colorprimarydark</item>
        <item name="coloraccent">@color/coloraccent</item>
    </style>

第二种,直接使用标志进行设置
其实第一种的源码就是第二种,(废话文学)

<style name="notactionbar" parent="theme.appcompat.light.darkactionbar">
        <!-- customize your theme here. -->
       <item name="windownotitle">true</item>
        <item name="windowactionbar">false</item>
    </style>

引用空标题栏样式

我们需要在我们要隐藏标题栏的activity中引用如下代码android:theme="@style/notactionbar"
配置图如下,地址:清单文件中进行配置

Android 美食大转盘详解流程

自定义标题栏

我们隐藏了标题栏之后,我们需要自定义一个自己喜欢的风格的标题栏
推荐使用toolbar,本示例为了简单,就直接使用了textview,代码如下

    <linearlayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:background="#cc00cc"
        android:gravity="center"
        android:paddingtop="20dp">
        <textview
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="吃货大赏"
            android:textcolor="#ffffff"
            android:textsize="17sp"
            android:padding="10dp"
            />
    </linearlayout>

合二为一

终于到了最后一步,我们需要在设置activity对系统栏进行设置
首先需要对版本进行一个判定,防止版本不兼容
其次获取decorview实例
然后使用标志符对系统状态进行设置,其中以下两个系统标志符为全屏和隐藏系统状态栏
重中之重,以下代码必须在setcontentview( r.layout.activity_main );之前执行
重中之重,以下代码必须在setcontentview( r.layout.activity_main );之前执行
重中之重,以下代码必须在setcontentview( r.layout.activity_main );之前执行

view.system_ui_flag_layout_fullscreen和view.system_ui_flag_layout_stable
 protected void oncreate(bundle savedinstancestate) {
        super.oncreate( savedinstancestate );
        if (build.version.sdk_int >= 21) {
            view decorview = getwindow().getdecorview();
            decorview.setsystemuivisibility( view.system_ui_flag_layout_fullscreen | view.system_ui_flag_layout_stable );
            getwindow().setstatusbarcolor( color.transparent );
        }
        setcontentview( r.layout.activity_main );
        initview();
        selectnumber();
    }

reveal animator

效果视频

Android 美食大转盘详解流程

建立一个圆形样式

首先我们需要建立一个圆形样式,在res->drawable下建立一个oval.xml文件
代码如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="#cc00cc"/>
</shape>

动画设置

在xml文件中配置我们建立的圆形样式

 android:background="@drawable/oval"

然后再activity中进行动画设置,其中createcircularreveal()方法的五个参数分别为:控件,动画开始的中心的x,动画开始的中心的y,动画开始的半径,动画结束的半径

 private void revealanim(view view) {
        animator animator = viewanimationutils.createcircularreveal(
                view, view.getwidth() / 2, view.getheight() / 2, view.getwidth(), 0
        );
        animator.setinterpolator( new acceleratedecelerateinterpolator() );
        animator.setduration( 2000 );
        animator.start();
    }

自定义转盘代码

public class turntable extends surfaceview implements surfaceholder.callback, runnable {

    private surfaceholder msurfaceholder;
    private canvas mcanvas;
    /**
     * 用于surfaceview绘制的子线程
     */
    private thread mthread;
    /**
     * 控制子线程开关
     */
    private boolean isrunning;
    /**
     * 字样*/
    private string[] itemcharstring;
    /**
     * 图片*/
    private int[] itemimages;
    private bitmap[] mbitmaps;

    /**
     * 背景
     */
    private bitmap mbgbitmap = bitmapfactory.decoderesource(getresources(), r.drawable.turntable_bgcolor );
   /**
    * 色块*/
    private int[] itemcolors;
   /**
    * 默认等份*/
    private int mitemcount = 8;

    /**
     * 整个盘块的范围
     */
    private rectf mrange = new rectf();
    /**
     * 整个盘块的直径
     */
    private int mradius;
    /**
     * 绘制盘块的画笔
     */
    private paint marcpaint;
    /**
     * 绘制文本的画笔
     */
    private paint mtextpaint;
    /**
     * 字体大小*/
    private float mtextsize = typedvalue.applydimension( typedvalue.complex_unit_sp, 20, getresources().getdisplaymetrics());
    /**
     * 盘块滚动的速度
     */
    private double mspeed = 0;

    /**
     * 转盘的中心位置
     */
    private int mcenter;
    /**
     * 这里我们的padding直接取paddingleft
     */
    private int mpadding;

    /**
     * volatile保证线程间的可见性
     */
    private volatile float mstartangle = 0;

    /**
     * 判断是否点击了停止按钮
     */
    private boolean isshouldend = false;

    /**
     * 设置单次绘制最低时间,如果在该时间内绘制完成,让子线程sleep到改时间结束
     * 这样防止了线程绘制频繁,先消耗性能的问题
     */
    private long monetimeminmillionseconds = 50;

    private int mdifferspeed = 1;// 调用停止后递减的速度差值 要大于0

    public turntable(context context) {
        super(context);
        init();
    }

    public turntable(context context, attributeset attrs) {
        super(context, attrs);
        init();
    }

    public turntable(context context, attributeset attrs, int defstyleattr) {
        super(context, attrs, defstyleattr);
        init();
    }
    public void initnumber(int number){
        if (number <= 0){
            return;
        }
        /**
         * 确保为偶数*/
        if (number % 2 == 0){
            initarea(number);
        }
    }
    private void initarea(int number){
        switch (number){
            case 4:
                fourparts();
                break;
            case 6:
                sixparts();
                break;
            case 8:
                eightparts();
                break;
                default:
                    sixparts();
        }
    }
    private void fourparts(){
        mitemcount = 4;
        itemcharstring = new string[]{"粉条", "面条", "米饭", "粥",};
        itemimages = new int[]{r.drawable.fen, r.drawable.mian, r.drawable.rice, r.drawable.tang};
        itemcolors = new int[]{0xffffc300, 0xfff17e01, 0xffffc300, 0xfff17e01};
        initimage(mitemcount,itemimages);
    }
    private void sixparts(){
        mitemcount = 6;
        itemcharstring = new string[]{"火锅", "汉堡", "巧克力", "奶茶", "蛋糕", "炸鸡"};
        itemimages = new int[]{r.drawable.huoguo, r.drawable.hanbao, r.drawable.qiaokeli, r.drawable.naicha, r.drawable.dangao, r.drawable.zhaji1};
        itemcolors = new int[]{0xffffc300, 0xfff17e01, 0xffffc300, 0xfff17e01, 0xffffc300, 0xfff17e01};
        initimage(mitemcount,itemimages);
    }
    private void eightparts(){
        mitemcount = 8;
        itemcharstring = new string[]{"苹果", "香蕉", "榴莲", "西瓜", "葡萄", "火龙果","芒果","草莓"};
        itemimages = new int[]{r.drawable.apple, r.drawable.xaingjiao, r.drawable.liulian, r.drawable.xigua, r.drawable.putao, r.drawable.huolongguo,r.drawable.mangguo,r.drawable.caomei};
        itemcolors = new int[]{0xffffc300, 0xfff17e01, 0xffffc300, 0xfff17e01, 0xffffc300, 0xfff17e01,0xffffc300,0xfff17e01};
        initimage(mitemcount,itemimages);
    }
    private void init() {
        msurfaceholder = this.getholder();
        // 管理surfaceview的生命周期
        msurfaceholder.addcallback(this);
        // 能够获取焦点
        this.setfocusable(true);
        this.setfocusableintouchmode(true);
        // 保持常亮
        this.setkeepscreenon(true);
    }

    @override
    protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
        super.onmeasure(widthmeasurespec, heightmeasurespec);
        int width = math.min(getmeasuredwidth(), getmeasuredheight());
        mpadding = getpaddingleft();
        mradius = width - mpadding * 2;
        // 中心点
        mcenter = width / 2;
        setmeasureddimension(width, width);
    }
    private void initimage(int count,int[] item){
        mbitmaps = new bitmap[mitemcount];
        for (int i = 0; i < count; i++) {
            mbitmaps[i] = bitmapfactory.decoderesource(getresources(), item[i]);
        }
    }
    @override
    public void surfacecreated(surfaceholder surfaceholder) {
        // 初始化盘块画笔
        marcpaint = new paint();
        marcpaint.setantialias(true);
        marcpaint.setdither(true);
        // 初始化文字画笔
        mtextpaint = new paint();
        mtextpaint.setcolor(0xffffffff);
        mtextpaint.settextsize(mtextsize);
        // 初始化盘块绘制范围
        mrange = new rectf(mpadding, mpadding, mpadding + mradius, mpadding + mradius);
        // 初始化图片
        initimage(mitemcount,itemimages);
        isrunning = true;
        mthread = new thread(this);
        mthread.start();
    }

    @override
    public void surfacechanged(surfaceholder surfaceholder, int i, int i1, int i2) {

    }

    @override
    public void surfacedestroyed(surfaceholder surfaceholder) {
        isrunning = false;
    }

    @override
    public void run() {
        /**
         * 不断的进行绘制
         */
        while (isrunning) {
            long premillions = system.currenttimemillis();
            draw();
            long aftermillions = system.currenttimemillis();
            long drawoncetime = aftermillions - premillions;
            if (drawoncetime < monetimeminmillionseconds) {
                try {
                    thread.sleep(monetimeminmillionseconds - drawoncetime);
                } catch (interruptedexception e) {
                    e.printstacktrace();
                }
            }
        }
    }

    private void draw() {
        try {
            mcanvas = msurfaceholder.lockcanvas();
            if (mcanvas != null) {
               /* 绘制背景颜色*/
                drawbgcolor();
                /*绘制区域*/
                drawarea();
            }
        } catch (exception e) {
           e.printstacktrace();
        } finally {
            if (mcanvas != null) {
                msurfaceholder.unlockcanvasandpost(mcanvas);
            }
        }
    }

    /**
     * 绘制盘块
     */
    private void drawarea() {
        // 起始角度
        float tempangle = mstartangle;
        // 每个盘块绘制的角度
        float sweepangele = 360 / mitemcount;
        for (int i = 0; i < mitemcount; i++) {
            marcpaint.setcolor(itemcolors[i]);
            // 绘制盘块
            mcanvas.drawarc(mrange, tempangle, sweepangele, true, marcpaint);
            // 绘制文本
            drawtext(tempangle, sweepangele, itemcharstring[i]);
            // 绘制图标
            drawicon(tempangle, mbitmaps[i]);

            tempangle += sweepangele;
        }
        mstartangle += mspeed;

        // 如果需要停止,让转速逐渐变小直到0
        if (isshouldend) {
            mspeed -= mdifferspeed;
        }
        if (mspeed <= 0) {
            mspeed = 0;
            isshouldend = false;
        }
    }

    /**
     * 绘制每个盘块的图标
     *
     * @param tempangle
     * @param bitmap
     */
    private void drawicon(float tempangle, bitmap bitmap) {
        // 约束图片的宽度,为直径的1/8,可以作为可变参数设置
        int imgwidth = mradius / 8;
        // 获取弧度值
        float angle = (float) ((tempangle + 360 / mitemcount / 2) * math.pi / 180);
        // 约定图片位置在直径1/4处
        int x = (int) (mcenter + mradius / 4 * math.cos(angle));
        int y = (int) (mcenter + mradius / 4 * math.sin(angle));
        // 确定图片位置
        rect rect = new rect(x - imgwidth / 2, y - imgwidth / 2, x + imgwidth / 2, y + imgwidth / 2);
        mcanvas.drawbitmap(bitmap, null, rect, null);
    }

    /**
     * 绘制每个盘块的文本
     *
     * @param tempangle
     * @param sweepangele
     * @param itemtextstr
     */
    private void drawtext(float tempangle, float sweepangele, string itemtextstr) {
        path path = new path();
        path.addarc(mrange, tempangle, sweepangele);
        // 利用水平偏移量让文字居中
        float textwidth = mtextpaint.measuretext(itemtextstr);
        int hoffset = (int) (mradius * math.pi / mitemcount / 2 - textwidth / 2);
        // 利用垂直偏移量让文字向圆心靠拢
        int voffset = mradius / 2 / 6;
        mcanvas.drawtextonpath(itemtextstr, path, hoffset, voffset, mtextpaint);
    }

    /**
     * 绘制背景
     */
    private void drawbgcolor() {
        mcanvas.drawcolor(0xffffffff);
        mcanvas.drawbitmap(mbgbitmap, null, new rect(mpadding / 2, mpadding / 2, getmeasuredwidth() - mpadding / 2, getmeasuredwidth() - mpadding / 2), null);
    }

    /**
     * 启动转盘
     * 能够控制到具体某个index范围内停止
     */
    public void start(int index) {
        if (isstart()) {
            return;
        }
        if (index < 0) {
            mspeed = 50 * (1 + math.random() * (0.5));
            isshouldend = false;
            return;
        }
    }

    /**
     * 停止转盘
     */
    public void stop() {
        if (isshouldend()) {
            return;
        }
        // 将初始角度重置
        mstartangle = 0;
        isshouldend = true;
    }

    /**
     * 转盘是否在旋转
     *
     * @return
     */
    public boolean isstart() {
        return mspeed != 0;
    }

    /**
     * 是否停止状态(但可能处于旋转减速到停止)
     *
     * @return
     */
    public boolean isshouldend() {
        return isshouldend;
    }
}

xml布局代码

<relativelayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">
    <linearlayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal"
        android:background="#cc00cc"
        android:gravity="center"
        android:paddingtop="20dp">
        <textview
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="吃货大赏"
            android:textcolor="#ffffff"
            android:textsize="17sp"
            android:padding="10dp"
            />
    </linearlayout>
    <com.franzliszt.foodturntable.turntable
        android:id="@+id/turntable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp"
        android:layout_margin="10dp"
        android:layout_centerinparent="true"/>
    <imageview
        android:id="@+id/startandend"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerinparent="true"
        android:src="@drawable/start"
        android:onclick="start"/>
    <com.franzliszt.foodturntable.animatedcircleloadingview
        android:id="@+id/loadingview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000"
        android:alpha="0.9"
        android:layout_centerinparent="true"
        app:animcircleloadingview_maincolor="#cc00cc"
        app:animcircleloadingview_secondarycolor="#ff0000"
        app:animcircleloadingview_textcolor="@android:color/white"
        android:visibility="gone"
        />
    <linearlayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/turntable"
        android:orientation="vertical"
        android:layout_margintop="20dp"
        >
        <radiogroup
            android:id="@+id/rg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center"
            android:gravity="center"
            >
            <radiobutton
                android:id="@+id/fourparts"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="主食"
                android:layout_marginright="10dp"
                />
            <radiobutton
                android:id="@+id/sixparts"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="外卖"
                android:layout_marginright="10dp"
                android:layout_marginleft="10dp"/>
            <radiobutton
                android:id="@+id/eightparts"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="水果"
                android:layout_marginleft="10dp"/>
        </radiogroup>
        <linearlayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_margintop="20dp">

            <button
                android:id="@+id/confirmselection"
                android:layout_width="40dp"
                android:layout_height="50dp"
                android:layout_alignparentleft="true"
                android:layout_alignparentbottom="true"
                android:layout_marginright="20dp"
                android:background="@drawable/oval"
                android:elevation="4dp"
                android:onclick="confirm"
                android:text="confirm"
                android:textallcaps="false"
                android:textcolor="#ffffff"
                android:textsize="12sp" />
            <button
                android:elevation="10dp"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:text="reset"
                android:textsize="12sp"
                android:textallcaps="false"
                android:textcolor="#ffffff"
                android:layout_alignparentright="true"
                android:layout_alignparentbottom="true"
                android:onclick="reset"
                android:background="@drawable/oval"
                android:layout_marginleft="20dp"/>
        </linearlayout>
    </linearlayout>
</relativelayout>

activity代码

public class mainactivity extends appcompatactivity {
    private animatedcircleloadingview loadingview;
    private turntable turntable;
    private int count = 0;
    private imageview changestatus;
    private radiogroup rg;
    private sp sp;
    private context context = null;


    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate( savedinstancestate );
        if (build.version.sdk_int >= 21) {
            view decorview = getwindow().getdecorview();
            decorview.setsystemuivisibility( view.system_ui_flag_layout_fullscreen | view.system_ui_flag_layout_stable );
            getwindow().setstatusbarcolor( color.transparent );
        }
        setcontentview( r.layout.activity_main );
        initview();
        selectnumber();
    }

    private void initview() {
        turntable = findviewbyid( r.id.turntable );
        loadingview = findviewbyid( r.id.loadingview );
        changestatus = findviewbyid( r.id.startandend );
        rg = findviewbyid( r.id.rg );
        /*默认设置8等份*/
        turntable.initnumber( 8 );
        if (context == null) {
            context = mainactivity.this;
        }
        sp = new sp( context );
    }

    public void start(view view) {
        count++;
        /*暂停*/
        if (count % 2 == 0) {
            turntable.stop();
            starticon();
        } else {
            /*开始*/
            turntable.start( -1 );
            stopicon();
        }
    }

    private void starticon() {
        changestatus.setimagedrawable( getresources().getdrawable( r.drawable.start ) );
    }

    private void stopicon() {
        changestatus.setimagedrawable( getresources().getdrawable( r.drawable.stop ) );
    }

    public void confirm(view view) {
        revealanim( view );
        loadingview.setvisibility( view.visible );
        startloading();
        startpercentmockthread();
        int num = (int) sp.getdata( context, "num", 0 );
        turntable.initnumber( num );
    }

    public void reset(view view) {
        revealanim( view );
        loadingview.setvisibility( view.gone );
        resetloading();
    }

    private void startloading() {
        loadingview.startindeterminate();
    }

    private void startpercentmockthread() {
        runnable runnable = new runnable() {
            @override
            public void run() {
                try {
                    thread.sleep( 500 );
                    for (int i = 0; i <= 100; i++) {
                        thread.sleep( 40 );
                        changepercent( i );
                    }
                } catch (interruptedexception e) {
                    e.printstacktrace();
                }
            }
        };
        new thread(runnable).start();
    }

    private void changepercent(final int percent) {
        runonuithread( new runnable() {
            @override
            public void run() {
                loadingview.setpercent( percent );
            }
        } );
    }

    public void resetloading() {
        runonuithread( new runnable() {
            @override
            public void run() {
                loadingview.resetloading();
            }
        } );
    }

    private void revealanim(view view) {
        animator animator = viewanimationutils.createcircularreveal(
                view, view.getwidth() / 2, view.getheight() / 2, view.getwidth(), 0
        );
        animator.setinterpolator( new acceleratedecelerateinterpolator() );
        animator.setduration( 2000 );
        animator.start();

    }

    private void selectnumber() {
        rg.setoncheckedchangelistener( new radiogroup.oncheckedchangelistener() {
            @override
            public void oncheckedchanged(radiogroup group, int checkedid) {
                switch (checkedid) {
                    case r.id.fourparts:
                        sp.putdata( context, "num", 4 );
                        break;
                    case r.id.sixparts:
                        sp.putdata( context, "num", 6 );
                        break;
                    case r.id.eightparts:
                        sp.putdata( context, "num", 8 );
                        break;
                }
            }
        } );
    }
}

代码下载地址

gitee下载地址

到此这篇关于android 美食大转盘详解流程的文章就介绍到这了,更多相关android 美食大转盘内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!