本文实例为大家分享了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 listviews = 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); } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。