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>
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Android seekbar實現可拖動進度條
- Android使用代碼動態生成界面
- Android文本視圖TextView實現跑馬燈效果
- Android studio六大基本佈局詳解
- Android生成隨機數的方法實例