安卓实现老虎机抽奖的案例
2016-04-30 17:08
330 查看
最近看到某理财app上有一个类似老虎机的抽奖功能,觉得蛮好玩就想自己动手去实现下.就做了一个简单的案例,效果如下:
![](http://img.blog.csdn.net/20160430172239933)
我说下思路吧,先准备两组图片,红色背景一组,黄色背景一组,如图
![](http://img.blog.csdn.net/20160430172908048)
![](http://img.blog.csdn.net/20160430173010142)
别忘了中间还有个ic_launcher。
新建一个GridView,把第一组图片适配给GridView。
然后定义一个子线程
Demo下载地址: 点击打开链接 密码:61bh
我说下思路吧,先准备两组图片,红色背景一组,黄色背景一组,如图
// 未开始抽奖时的图片 private int[] imgs1 = { R.drawable.m1, R.drawable.m2, R.drawable.m3, R.drawable.m4, R.drawable.ic_launcher, R.drawable.m5, R.drawable.m6, R.drawable.m7, R.drawable.m8 }; // 开始抽奖时的图片 private int[] imgs2 = { R.drawable.n1, R.drawable.n2, R.drawable.n3, R.drawable.n4, R.drawable.ic_launcher, R.drawable.n5, R.drawable.n6, R.drawable.n7, R.drawable.n8 };
别忘了中间还有个ic_launcher。
新建一个GridView,把第一组图片适配给GridView。
然后定义一个子线程
class MyRunnable implements Runnable { @Override public void run() { handler.sendEmptyMessage(0); //发送消息 //如果到达指定的时间,则停止 if (startTime >= stopTime) { handler.removeCallbacks(mMyRunnable); //提示中奖消息 if (array[num] < 4) { String text = array[num] + 1 + ""; Toast.makeText(context, "恭喜你中了" + text, 0).show(); } else { Toast.makeText(context, "恭喜你中了" + array[num], 0).show(); } startTime = 0; stopTime = 0; return; } //每隔100毫秒运行一次 handler.postDelayed(mMyRunnable, 100); startTime += 100; } }运行的时候每隔一段时间发送消息, 本文是100毫秒, 由handler来处理,当到达指定的时间时就移除消息队列,线程停止运行,startTime初始为0,每运行一次就增加100毫秒,stopTime则是事先设置的随机时间
//定义一个随机数最为结束的时间,这里是2到6秒 stopTime = new Random().nextInt(1000 * 5) + 2000;handler接受消息后处理消息 ,改变GridView中item的背景色,array是转盘id的数组
// 对应转盘id的数组 private int[] array = { 0, 1, 2, 5, 8, 7, 6, 3 };传入num,num从0到8依次增加,当到达最后一个item时则循环
Handler handler = new Handler() { public void handleMessage(Message msg) { Change(array[num]); //改变背景色 num++; //依次下一个 //如果到了最后一个item,则循环 if (num >= 8) { num = 0; } }; };现在我们来看看Change方法.在这个方法里,我们遍历item的个数,当传入的id为选中的Item时则把图片换成图片数组2中对应的图片,如果是中间那个id,即4,那么跳出循环,不处理,其他的id都设置为图片数组1中对应的图片.
private void Change(int id) { for (int i = 0; i < gridView.getChildCount(); i++) { if (i == id) { //如果是选中的,则改变图片为数组2中的图片 ((ImageView) (gridView.getChildAt(i).findViewById(R.id.img))).setBackgroundResource(imgs2[id]); } else if (i == 4) { //如果是到了中间那个,则跳出 continue; } else { //未选中的就设置为数组1中的图片 ((ImageView) (gridView.getChildAt(i).findViewById(R.id.img))).setBackgroundResource(imgs1[i]); } } }这里再贴上布局代码吧,item布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/img" android:layout_width="60dp" android:layout_height="60dp" android:layout_centerInParent="true" /> </RelativeLayout>main布局
<LinearLayout 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:orientation="vertical" > <GridView android:id="@+id/gridview" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:verticalSpacing="40dp" android:layout_gravity="center" android:gravity="center" android:numColumns="3" > </GridView> <Button android:id="@+id/start_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="开始" /> </LinearLayout>
Demo下载地址: 点击打开链接 密码:61bh
相关文章推荐
- SVM核函数
- java中的List和Vector的区别
- Android Studio cannot resolve symbol 'TabLayout'
- android 时间控件的使用
- BZOJ1492: [NOI2007]货币兑换Cash
- Linux内核分析期末总结
- 软件开发模型
- 压缩视频
- 使用struts2的<s>标签出错
- 1029. 旧键盘(20)
- 每天一个linux命令:vmstat
- POJ2516 Minimum Cost
- vim常用命令示意图
- StarUML逆向工程: 通过代码生成类图
- 微分几何之全局参数化
- Neuroph studio 入门教程
- 逆波兰表达式
- Delphi字符串、PChar与字符数组之间的转换
- each-Select
- 《有关SDL的简介》