Android九宫格抽奖_第1张图片

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 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>

更多相关文章

  1. Android 应用安装位置
  2. Android使用百度地图SDK获得当前设备位置所在的省、市(系列1)
  3. android studio调整默认的debug.keystore位置。
  4. 关于FragmentLayout布局的位置问题
  5. Android Geocoder(位置解析)
  6. Android使用百度地图移动到我的位置
  7. edittext的光标的位置
  8. Android:Random生成随机数
  9. android GPS 获取位置

随机推荐

  1. Android(安卓)repo 出现error.GitError:
  2. Android SDK 2.2 下载安装方法
  3. Android 10 创建文件失败
  4. Android中WebView和JavaScript通信
  5. android 网络详解
  6. 转:善用Android预定义样式
  7. No label views point to this text fiel
  8. Android下SQLite3数据库操作笔记
  9. Android常用开源框架
  10. Android第三方开源FloatingActionButton(c