标签:
package com.example.puke; import android.os.Bundle; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { private ImageView img1; private ImageView img2; private ImageView img3; private TextView show; private int[] imgs = { R.drawable.a1, R.drawable.a2, R.drawable.a3 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img1 = (ImageView) findViewById(R.id.img1); img2 = (ImageView) findViewById(R.id.img2); img3 = (ImageView) findViewById(R.id.img3); show = (TextView) findViewById(R.id.show); setImg(); img1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { img1.setImageResource(imgs[0]); img2.setImageResource(imgs[1]); img3.setImageResource(imgs[2]); if (imgs[0] == R.drawable.a2) { show.setText("bingo"); }else{ show.setText("error"); } //设置模糊度 img2.setAlpha(100); img3.setAlpha(100); } }); img2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { img1.setImageResource(imgs[0]); img2.setImageResource(imgs[1]); img3.setImageResource(imgs[2]); if (imgs[1] == R.drawable.a2) { show.setText("bingo"); }else{ show.setText("error"); } img1.setAlpha(100); img3.setAlpha(100); } }); img3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { img1.setImageResource(imgs[0]); img2.setImageResource(imgs[1]); img3.setImageResource(imgs[2]); if (imgs[2] == R.drawable.a2) { show.setText("bingo"); }else{ show.setText("error"); } img2.setAlpha(100); img1.setAlpha(100); } }); findViewById(R.id.reset).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub setImg(); img1.setImageResource(R.drawable.a4); img2.setImageResource(R.drawable.a4); img3.setImageResource(R.drawable.a4); img1.setAlpha(255); img2.setAlpha(255); img3.setAlpha(255); show.setText(""); } }); } //随机变化 private void setImg() { for (int i = 0; i < imgs.length; i++) { int temp = imgs[i]; int in = (int) (Math.random() * 2); imgs[i] = imgs[in]; imgs[in] = temp; } } }
布局
<LinearLayout 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:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="寻找红心A" android:textColor="@android:color/secondary_text_dark" android:textSize="30sp" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/show" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textSize="15sp" android:layout_gravity="center_horizontal" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="50dp" > <ImageView android:id="@+id/img1" android:layout_width="0dp" android:layout_height="100dp" android:src="@drawable/a4" android:layout_weight="1" /> <ImageView android:id="@+id/img2" android:layout_width="0dp" android:layout_height="100dp" android:src="@drawable/a4" android:layout_weight="1" /> <ImageView android:id="@+id/img3" android:layout_width="0dp" android:layout_height="100dp" android:src="@drawable/a4" android:layout_weight="1" /> </LinearLayout> <Button android:id="@+id/reset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="洗牌" android:layout_gravity="center_horizontal" android:layout_marginTop="50dp" /> </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/84126858jmz/p/4904917.html