View Code
1 package com.example.flexd7; 2 3 import android.os.Bundle; 4 import android.annotation.SuppressLint; 5 import android.app.Activity; 6 import android.content.res.Resources; 7 import android.util.Log; 8 import android.view.Menu; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 import android.widget.ImageView; 13 14 public class MainActivity extends Activity { 15 16 private Button bt1; 17 private ImageView iv1; 18 private ImageView iv2; 19 private ImageView iv3; 20 21 private int[] poker = new int[]{R.drawable.p01,R.drawable.p02,R.drawable.p03}; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 28 //Widgets 29 bt1 = (Button)findViewById(R.id.bt1); 30 iv1 = (ImageView)findViewById(R.id.iv1); 31 iv2 = (ImageView)findViewById(R.id.iv2); 32 iv3 = (ImageView)findViewById(R.id.iv3); 33 34 //Listener 35 bt1.setOnClickListener(buttonOnClickListener); 36 iv1.setOnClickListener(imageViewOnClickListener); 37 iv2.setOnClickListener(imageViewOnClickListener); 38 iv3.setOnClickListener(imageViewOnClickListener); 39 40 //洗牌 41 //random(); 42 } 43 44 /** 45 * Button监听 46 * Button点击的时候,设置三张牌显示为背面,并且不透明 就是可以看见,然后洗牌,牌面值存在数组里 47 */ 48 private OnClickListener buttonOnClickListener = new OnClickListener(){ 49 50 //android自带lint工具 帮助提升代码 51 @SuppressLint("NewApi") 52 @Override 53 public void onClick(View v) { 54 //iv1.setImageResource(R.drawable.p04); 55 iv1.setImageDrawable(getResources().getDrawable(R.drawable.p04)); 56 iv1.setAlpha(255); 57 58 //iv2.setImageResource(R.drawable.p04); 59 iv2.setImageDrawable(getResources().getDrawable(R.drawable.p04)); 60 iv2.setAlpha(255); 61 62 //iv3.setImageResource(R.drawable.p04); 63 iv3.setImageDrawable(getResources().getDrawable(R.drawable.p04)); 64 iv3.setAlpha(255); 65 66 //洗牌 67 random2(); 68 } 69 }; 70 71 /** 72 * ImageView监听 73 * A牌点击则设置BC牌为透明,并且分别读入数组中的牌面值 74 */ 75 private OnClickListener imageViewOnClickListener = new OnClickListener() { 76 77 @SuppressLint("NewApi") 78 @Override 79 public void onClick(View v) { 80 Log.i("Tag", "imageViewOnClick View:" + v.getId()); 81 //设置其他两个为透明 82 if(v.getId()==R.id.iv1){ 83 iv2.setImageAlpha(100); 84 iv3.setImageAlpha(100); 85 }else if(v.getId()==R.id.iv2){ 86 iv1.setImageAlpha(100); 87 iv3.setImageAlpha(100); 88 }else if(v.getId()==R.id.iv3){ 89 iv2.setImageAlpha(100); 90 iv1.setImageAlpha(100); 91 }else { 92 } 93 iv1.setImageResource(poker[0]); 94 iv2.setImageResource(poker[1]); 95 iv3.setImageResource(poker[2]); 96 } 97 }; 98 99 100 /** 101 * 洗牌方法1 102 * Math.random()是产生[0,1)的随机数,(int)(Math.random()*2)就随机产生0或1 103 */ 104 private void random(){ 105 for (int i = 0; i < 3; i++) { 106 int change = (int) (Math.random() * 2);//0、1、2三张牌依次跟随机到的0或1调换 107 int temp = poker[i]; 108 poker[i] = poker[change]; 109 poker[change] = temp; 110 } 111 112 //iv1.setImageResource(poker[0]); 113 //iv2.setImageResource(poker[1]); 114 //iv3.setImageResource(poker[2]); 115 116 //iv1.setImageDrawable(getResources().getDrawable(poker[0])); 117 //iv2.setImageDrawable(getResources().getDrawable(poker[1])); 118 //iv3.setImageDrawable(getResources().getDrawable(poker[2])); 119 } 120 121 /** 122 * 洗牌方法2 123 */ 124 private void random2(){ 125 int change = ((int)(Math.random()*10)%3);//Math.random()*10产生小于10随机数,%3产生小于3余数 126 for (int i = 0; i < 3; i++) {//0、1、2三张牌依次跟随机到的 确定的0或1或2调换 127 int temp = poker[i]; 128 poker[i] = poker[change]; 129 poker[change] = temp; 130 } 131 } 132 133 @Override 134 public boolean onCreateOptionsMenu(Menu menu) { 135 // Inflate the menu; this adds items to the action bar if it is present. 136 getMenuInflater().inflate(R.menu.activity_main, menu); 137 return true; 138 } 139 140 }