猜猜紅桃A在哪裏(ImageView單擊事件與透明度處理)
新建一個繼承Activity類的ImageViewGameActivity,並設置佈局文件爲:imageviewgame.xml。
首先定義佈局文件。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
<ImageView android:id="@+id/imageviewgame_image01" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="1" android:src="@drawable/p_b1" />
<ImageView android:id="@+id/imageviewgame_image02" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="1" android:src="@drawable/p_b1" />
<ImageView android:id="@+id/imageviewgame_image03" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="1" android:src="@drawable/p_b1" /> </LinearLayout>
<Button android:id="@+id/imageviewgame_btn" style="@android:style/Widget.Button.Inset" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/sure" />
<TextView android:id="@+id/imageviewgame_tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_horizontal" android:text="@string/sure" android:textSize="20sp" />
</LinearLayout> |
效果:
而後在Activity獲取這5個組件。
package lyx.feng.second; ...... public class ImageViewGame extends Activity implements OnClickListener { private ImageView image1 = null; private ImageView image2 = null; private ImageView image3 = null; private Button btn = null; private TextView tv = null; private List<Integer> cards = new ArrayList<Integer>();
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.imageviewgame); initViews();
this.btn.setText("再來一次"); this.btn.setVisibility(View.GONE); }
private List<Integer> getRandomCards(List<Integer> cards) { Collections.shuffle(cards); return cards; }
private void setImages(List<Integer> cards) { this.image1.setImageResource(cards.get(0)); this.image2.setImageResource(cards.get(1)); this.image3.setImageResource(cards.get(2)); }
private void initViews() { this.image1 = (ImageView) super .findViewById(R.id.imageviewgame_image01); this.image2 = (ImageView) super .findViewById(R.id.imageviewgame_image02); this.image3 = (ImageView) super .findViewById(R.id.imageviewgame_image03); this.btn = (Button) super.findViewById(R.id.imageviewgame_btn); this.tv = (TextView) super.findViewById(R.id.imageviewgame_tv);
cards.add(R.drawable.p_h_2); cards.add(R.drawable.p_h_4); cards.add(R.drawable.p_h_7);
this.image1.setOnClickListener(this); this.image2.setOnClickListener(this); this.image3.setOnClickListener(this); this.btn.setOnClickListener(this); this.tv.setVisibility(View.GONE); }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.imageviewgame_image01: showcard(); break; case R.id.imageviewgame_image02: showcard(); break; case R.id.imageviewgame_image03: showcard(); break; case R.id.imageviewgame_btn: this.image1.setImageResource(R.drawable.p_b1); this.image2.setImageResource(R.drawable.p_b1); this.image3.setImageResource(R.drawable.p_b1); this.image1.setClickable(true); this.image2.setClickable(true); this.image3.setClickable(true); this.btn.setVisibility(View.GONE); break; } }
private void showcard() { setImages(getRandomCards(cards)); this.image1.setClickable(false); this.image2.setClickable(false); this.image3.setClickable(false); this.btn.setVisibility(View.VISIBLE); } }
|