Android實(shí)現(xiàn)九宮格抽獎(jiǎng)
本文實(shí)例為大家分享了Android實(shí)現(xiàn)九宮格抽獎(jiǎng)的具體代碼,供大家參考,具體內(nèi)容如下
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;//變色時(shí)間間隔 ? ? private int lightPosition = 0;//當(dāng)前亮燈位置,從0開始 ? ? private int runCount = 10;//需要轉(zhuǎn)多少圈 ? ? private int lunckyPosition = 4;//中獎(jiǎng)的幸運(yùn)位置,從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(); ? ? ? ? } ? ? } ? ? /** ? ? ?* 生成隨機(jī)數(shù) ? ? ?* @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); ? ? ? ? ? ? //如果是最后一次滾動(dòng) ? ? ? ? ? ? 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); ? ? ? ? ? ? ? ? //最后幾轉(zhuǎn)速度變慢 ? ? ? ? ? ? ? ? if (runCount<3) timeC += 200; ? ? ? ? ? ? ? ? new TimeCount(timeC*9,timeC).start(); ? ? ? ? ? ? ? ? runCount--; ? ? ? ? ? ? } ? ? ? ? ? ? //如果是最后一圈且計(jì)時(shí)也已經(jīng)結(jié)束 ? ? ? ? ? ? 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="獎(jiǎng)品1" ? ? ? ? ? ? style="@style/TextViewLuckDraw" ? ? ? ? ? ? android:id="@+id/tv1"/> ? ? ? ? <TextView ? ? ? ? ? ? android:text="獎(jiǎng)品2" ? ? ? ? ? ? style="@style/TextViewLuckDraw" ? ? ? ? ? ? android:id="@+id/tv2"/> ? ? ? ? <TextView ? ? ? ? ? ? android:text="獎(jiǎng)品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="獎(jiǎng)品8" ? ? ? ? ? ? style="@style/TextViewLuckDraw" ? ? ? ? ? ? android:id="@+id/tv8"/> ? ? ? ? <TextView ? ? ? ? ? ? android:textSize="20sp" ? ? ? ? ? ? android:text="開始抽獎(jiǎng)" ? ? ? ? ? ? style="@style/TextViewLuckDraw" ? ? ? ? ? ? android:id="@+id/tvStart"/> ? ? ? ? <TextView ? ? ? ? ? ? android:text="獎(jiǎng)品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="獎(jiǎng)品7" ? ? ? ? ? ? style="@style/TextViewLuckDraw" ? ? ? ? ? ? android:id="@+id/tv7"/> ? ? ? ? <TextView ? ? ? ? ? ? android:text="獎(jiǎng)品6" ? ? ? ? ? ? style="@style/TextViewLuckDraw" ? ? ? ? ? ? android:id="@+id/tv6"/> ? ? ? ? <TextView ? ? ? ? ? ? android:text="獎(jiǎng)品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>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 實(shí)現(xiàn)九宮格抽獎(jiǎng)功能
- Android自定義view制作抽獎(jiǎng)轉(zhuǎn)盤
- Android自定義View實(shí)現(xiàn)抽獎(jiǎng)轉(zhuǎn)盤
- Android自定義View實(shí)現(xiàn)QQ運(yùn)動(dòng)積分轉(zhuǎn)盤抽獎(jiǎng)功能
- Android抽獎(jiǎng)輪盤的制作方法
- Android使用surfaceView自定義抽獎(jiǎng)大轉(zhuǎn)盤
- Android打造流暢九宮格抽獎(jiǎng)活動(dòng)效果
- Android中利用SurfaceView制作抽獎(jiǎng)轉(zhuǎn)盤的全流程攻略
- Android App中實(shí)現(xiàn)簡單的刮刮卡抽獎(jiǎng)效果的實(shí)例詳解
- Android簡單實(shí)現(xiàn)圓盤抽獎(jiǎng)界面
相關(guān)文章
Android事件分發(fā)機(jī)制深入刨析原理及源碼
Android?的事件分發(fā)機(jī)制大體可以分為三部分:事件生產(chǎn)、事件分發(fā)?、事件消費(fèi)。事件的生產(chǎn)是由用戶點(diǎn)擊屏幕產(chǎn)生,我們這次著重分析事件的分發(fā)和消費(fèi),因?yàn)槭录职l(fā)和處理聯(lián)系的過于緊密,這篇文章將把事件的分發(fā)和消費(fèi)放在一起分析2023-04-04android private libraries 中的包源代碼添加方法
這篇文章主要介紹了android private libraries 中的包源代碼添加方法,方法很簡單,看完本文即可學(xué)會(huì),需要的朋友可以參考下2015-05-05Flutter實(shí)現(xiàn)底部導(dǎo)航欄創(chuàng)建詳解
ConvexBottomBar是一個(gè)底部導(dǎo)航欄組件,用于展現(xiàn)凸起的TAB效果,支持多種內(nèi)置樣式與動(dòng)畫交互。本文將利用ConvexBottomBar創(chuàng)建漂亮的底部導(dǎo)航欄,感興趣的可以學(xué)習(xí)一下2022-01-01Android Studio自動(dòng)排版的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Android Studio自動(dòng)排版的兩種實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android自定義控件之開關(guān)按鈕學(xué)習(xí)筆記分享
這篇文章主要為大家分享了Android自定義開關(guān)按鈕的學(xué)習(xí)筆記,內(nèi)容豐富,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Android Studio編寫AIDL文件后如何實(shí)現(xiàn)自動(dòng)編譯生成
這篇文章主要介紹了Android Studio編寫AIDL文件后如何實(shí)現(xiàn)自動(dòng)編譯生成,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03