Android实现九宫格抽奖
作者:Small_Cake
这篇文章主要为大家详细介绍了Android实现九宫格抽奖,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
