安卓抽奖功能实现

这个抽奖效果本来是h5写的,正好工作不忙写了原生试试效果。代码比较简单,希望对有些人有用。

安卓抽奖功能实现_第1张图片

主要代码

public class Test extends Activity implements OnClickListener{

    private Button btn_luck_draw;

    private ImageView iv_voucher_one;
    private ImageView iv_voucher_two;
    private ImageView iv_voucher_three;
    private ImageView iv_voucher_four;
    private ImageView iv_voucher_five;
    private ImageView iv_voucher_six;

    public static final int MESSAGE_ONE = 1;
    public static final int MESSAGE_TWO = 2;
    public static final int MESSAGE_THREE = 3;
    public static final int MESSAGE_FOUR = 4;
    public static final int MESSAGE_FIVE = 5;
    public static final int MESSAGE_SIX = 6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

        initView();
        initData();
    }

    private void initView() {
        btn_luck_draw=(Button) findViewById(R.id.btn_luck_draw);
        iv_voucher_one=(ImageView) findViewById(R.id.iv_voucher_one);
        iv_voucher_two=(ImageView) findViewById(R.id.iv_voucher_two);
        iv_voucher_three=(ImageView) findViewById(R.id.iv_voucher_three);
        iv_voucher_four=(ImageView) findViewById(R.id.iv_voucher_four);
        iv_voucher_five=(ImageView) findViewById(R.id.iv_voucher_five);
        iv_voucher_six=(ImageView) findViewById(R.id.iv_voucher_six);

        btn_luck_draw.setOnClickListener(this);
    }

    private void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_luck_draw:
            luckDraw();
            break;
        }
    }

    private Handler mHandler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case MESSAGE_ONE:
                    iv_voucher_one.setVisibility(View.VISIBLE);
                    iv_voucher_two.setVisibility(View.INVISIBLE);
                    iv_voucher_three.setVisibility(View.INVISIBLE);
                    iv_voucher_four.setVisibility(View.INVISIBLE);
                    iv_voucher_five.setVisibility(View.INVISIBLE);
                    iv_voucher_six.setVisibility(View.INVISIBLE);
                    break;
                case MESSAGE_TWO:
                    iv_voucher_one.setVisibility(View.INVISIBLE);
                    iv_voucher_two.setVisibility(View.VISIBLE);
                    iv_voucher_three.setVisibility(View.INVISIBLE);
                    iv_voucher_four.setVisibility(View.INVISIBLE);
                    iv_voucher_five.setVisibility(View.INVISIBLE);
                    iv_voucher_six.setVisibility(View.INVISIBLE);
                    break;
                case MESSAGE_THREE:
                    iv_voucher_one.setVisibility(View.INVISIBLE);
                    iv_voucher_two.setVisibility(View.INVISIBLE);
                    iv_voucher_three.setVisibility(View.VISIBLE);
                    iv_voucher_four.setVisibility(View.INVISIBLE);
                    iv_voucher_five.setVisibility(View.INVISIBLE);
                    iv_voucher_six.setVisibility(View.INVISIBLE);
                    break;
                case MESSAGE_FOUR:
                    iv_voucher_one.setVisibility(View.INVISIBLE);
                    iv_voucher_two.setVisibility(View.INVISIBLE);
                    iv_voucher_three.setVisibility(View.INVISIBLE);
                    iv_voucher_four.setVisibility(View.VISIBLE);
                    iv_voucher_five.setVisibility(View.INVISIBLE);
                    iv_voucher_six.setVisibility(View.INVISIBLE);
                    break;
                case MESSAGE_FIVE:
                    iv_voucher_one.setVisibility(View.INVISIBLE);
                    iv_voucher_two.setVisibility(View.INVISIBLE);
                    iv_voucher_three.setVisibility(View.INVISIBLE);
                    iv_voucher_four.setVisibility(View.INVISIBLE);
                    iv_voucher_five.setVisibility(View.VISIBLE);
                    iv_voucher_six.setVisibility(View.INVISIBLE);
                    break;
                case MESSAGE_SIX:
                    iv_voucher_one.setVisibility(View.INVISIBLE);
                    iv_voucher_two.setVisibility(View.INVISIBLE);
                    iv_voucher_three.setVisibility(View.INVISIBLE);
                    iv_voucher_four.setVisibility(View.INVISIBLE);
                    iv_voucher_five.setVisibility(View.INVISIBLE);
                    iv_voucher_six.setVisibility(View.VISIBLE);
                    break;
                }
            }
        };

    private int number = 6;

    public void luckDraw(){
        Message msg = new Message();
        Random random = new Random();
        number = random.nextInt((6))+1;
        new Thread(new Runnable() {
            public void run() {
                Random random = new Random();
                int num = random.nextInt((6));
                while(number<60+num){
                    Message message = new Message();
                    switch (number%6) {
                    case 0:
                        message.what = MESSAGE_SIX;
                        break;
                    case 1:
                        message.what = MESSAGE_ONE;
                        break;
                    case 2:
                        message.what = MESSAGE_TWO;
                        break;
                    case 3:
                        message.what = MESSAGE_THREE;
                        break;
                    case 4:
                        message.what = MESSAGE_FOUR;
                        break;
                    case 5:
                        message.what = MESSAGE_FIVE;
                        break;
                    case 6:
                        message.what = MESSAGE_SIX;
                        break;
                }
                number++;
                mHandler.sendMessage(message);
                if(number<50){  
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else if(number>50 && number<60){
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    try {
                        Thread.sleep(400);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

            Message message = new Message();
            Bundle bundle = new Bundle();
            bundle.putInt("msg", (num+3));
            message.setData(bundle);
            mHandler.sendMessage(message);
           }
       }).start(); 
       mHandler.sendMessage(msg);
    }

}

下面附上demo 复制到自己demo中运行就行了 http://download.csdn.net/detail/wtq_domian/9651863

你可能感兴趣的:(Android笔记)