本文实例为大家分享了android实现九宫格抽奖的具体代码,供大家参考,具体内容如下

package cq.cake.luckdraw;
import android.graphics.color;
import android.os.bundle;
import android.os.countdowntimer;
import android.support.v7.app.appcompatactivity;
import android.util.log;
import android.view.view;
import android.widget.textview;
import java.util.linkedlist;
import java.util.list;
import java.util.random;
public class mainactivity extends appcompatactivity {
    private textview tv1;
    private textview tv2;
    private textview tv3;
    private textview tv4;
    private textview tvstart;
    private textview tv5;
    private textview tv6;
    private textview tv7;
    private textview tv8;
    private textview tvnotice;
    private list<textview> views = new linkedlist<>();//所有的视图
    private int timec= 100;//变色时间间隔
    private int lightposition = 0;//当前亮灯位置,从0开始
    private int runcount = 10;//需要转多少圈
    private int lunckyposition = 4;//中奖的幸运位置,从0开始
    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);
        init();
    }
    private void init() {
        tv1 = (textview) findviewbyid(r.id.tv1);
        tv2 = (textview) findviewbyid(r.id.tv2);
        tv3 = (textview) findviewbyid(r.id.tv3);
        tv4 = (textview) findviewbyid(r.id.tv4);
        tv5 = (textview) findviewbyid(r.id.tv5);
        tv6 = (textview) findviewbyid(r.id.tv6);
        tv7 = (textview) findviewbyid(r.id.tv7);
        tv8 = (textview) findviewbyid(r.id.tv8);
        tvstart = (textview) findviewbyid(r.id.tvstart);
        tvnotice = (textview) findviewbyid(r.id.tv_notice);
        views.add(tv1);
        views.add(tv2);
        views.add(tv3);
        views.add(tv4);
        views.add(tv5);
        views.add(tv6);
        views.add(tv7);
        views.add(tv8);
        try {
            tvstart.setonclicklistener(new view.onclicklistener() {
                @override
                public void onclick(view v) {
                    tvstart.setclickable(false);
                    tvstart.setenabled(false);
                    tvnotice.settext("");
                    runcount = 10;
                    timec = 100;
                    views.get(lunckyposition).setbackgroundcolor(color.transparent);
                    lunckyposition = randomnum(0,7);
                    new timecount(timec*9,timec).start();
                }
            });
        } catch (exception e) {
            e.printstacktrace();
        }
    }
    /**
     * 生成随机数
     * @param minnum
     * @param maxnum
     * @return
     */
    private int randomnum(int minnum,int maxnum) {
        int max = maxnum;
        int min = minnum;
        random random = new random();
        return random.nextint(max)%(max-min+1) + min;
    }
    class timecount extends countdowntimer{
        public timecount(long millisinfuture, long countdowninterval) {
            super(millisinfuture, countdowninterval);
            lightposition = 0;
        }
        @override
        public void ontick(long millisuntilfinished) {
            log.i(">>>","---"+lightposition);
            //如果是最后一次滚动
            if (runcount>0){
                if (lightposition>0){
                    views.get(lightposition-1).setbackgroundcolor(color.transparent);
                }
                if (lightposition<8){
                    views.get(lightposition).setbackgroundcolor(color.red);
                }
            }else if (runcount==0){
                if (lightposition<=lunckyposition){
                    if (lightposition>0){
                        views.get(lightposition-1).setbackgroundcolor(color.transparent);
                    }
                    if (lightposition<8){
                        views.get(lightposition).setbackgroundcolor(color.red);
                    }
                }
            }
            lightposition++;
        }
        @override
        public void onfinish() {
            log.i(">>>","onfinish=="+runcount);
            //如果不是最后一圈,需要还原最后一块的颜色
            textview tvlast= views.get(7);
            if (runcount!=0){
                tvlast.setbackgroundcolor(color.transparent);
                //最后几转速度变慢
                if (runcount<3) timec += 200;
                new timecount(timec*9,timec).start();
                runcount--;
            }
            //如果是最后一圈且计时也已经结束
            if (runcount==0&&lightposition==8){
                tvstart.setclickable(true);
                tvstart.setenabled(true);
                tvnotice.settext("恭喜你抽中: "+views.get(lunckyposition).gettext().tostring());
                if (lunckyposition!=views.size())
                    tvlast.setbackgroundcolor(color.transparent);
            }
        }
    }
}
<?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"
    android:paddingbottom="@dimen/activity_vertical_margin"
    android:paddingleft="@dimen/activity_horizontal_margin"
    android:paddingright="@dimen/activity_horizontal_margin"
    android:paddingtop="@dimen/activity_vertical_margin"
    tools:context="cq.cake.luckdraw.mainactivity">
    <linearlayout
        android:id="@+id/layout1"
        android:layout_above="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="100dp">
        <textview
            android:text="奖品1"
            style="@style/textviewluckdraw"
            android:id="@+id/tv1"/>
        <textview
            android:text="奖品2"
            style="@style/textviewluckdraw"
            android:id="@+id/tv2"/>
        <textview
            android:text="奖品3"
            style="@style/textviewluckdraw"
            android:id="@+id/tv3"/>
    </linearlayout>
    <linearlayout
        android:id="@+id/layout2"
        android:layout_centerinparent="true"
        android:layout_width="match_parent"
        android:layout_height="100dp">
        <textview
            android:text="奖品8"
            style="@style/textviewluckdraw"
            android:id="@+id/tv8"/>
        <textview
            android:textsize="20sp"
            android:text="开始抽奖"
            style="@style/textviewluckdraw"
            android:id="@+id/tvstart"/>
        <textview
            android:text="奖品4"
            style="@style/textviewluckdraw"
            android:id="@+id/tv4"/>
    </linearlayout>
    <linearlayout
        android:id="@+id/layout3"
        android:layout_below="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="100dp">
        <textview
            android:text="奖品7"
            style="@style/textviewluckdraw"
            android:id="@+id/tv7"/>
        <textview
            android:text="奖品6"
            style="@style/textviewluckdraw"
            android:id="@+id/tv6"/>
        <textview
            android:text="奖品5"
            style="@style/textviewluckdraw"
            android:id="@+id/tv5"/>
    </linearlayout>
    <textview
        android:layout_margintop="16dp"
        android:textsize="20sp"
        android:gravity="center"
        android:textcolor="@color/coloraccent"
        android:layout_below="@+id/layout3"
        android:id="@+id/tv_notice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</relativelayout>

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