網站首頁 編程語言 正文
本文實例為大家分享了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>
原文鏈接:https://blog.csdn.net/xiaoshubing/article/details/52766739
相關推薦
- 2022-03-28 Easyx實現窗口自動碰撞的小球_C 語言
- 2022-08-01 React中的Hooks進階理解教程_React
- 2023-05-06 React組件渲染后對DOM的操作方式_React
- 2022-05-17 bat命令實現批量提取、去空格、修改文件名的方法_DOS/BAT
- 2022-01-10 解決遮罩下方元素禁止滾動
- 2023-12-02 富文本組件中圖片間空白處理小技巧
- 2022-09-15 React手寫一個手風琴組件示例_React
- 2023-02-04 golang?Gorm框架講解_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支