本文实例为大家分享了Android使用Handler实现打地鼠的具体代码,供大家参考,具体内容如下
1.实现效果
如下图所示:
2.代码实现
新建一个名为DiglettDemo的项目,activity_main.xml代码如下:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/diglett"
android:visibility="gone" />
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="45dp"
android:text="点击开始" />
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textColor="#ff0000"
android:textSize="20sp" />
MainActivity.class代码如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {
public static final int CODE = 999;
public static final int RANDOM_NUMBER = 500;
private TextView mTextView;
private Button mButton;
private ImageView mImageView;
/**
* 定义地鼠的位置
*/
public int[][] mPosition = new int[][]{
{342, 180}, {432, 880}, {521, 256}, {429, 780},
{456, 976}, {145, 665}, {123, 678}, {564, 567},
};
private int mTotalCount, mSuccessCount = 0;
public static final int MAX_COUNT = 10;
private MyHandler mMyHandler = new MyHandler(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mTextView = findViewById(R.id.textView);
mButton = findViewById(R.id.button);
mImageView = findViewById(R.id.imageView);
mButton.setOnClickListener(this);
mImageView.setOnTouchListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
start();
break;
}
}
private void start() {
mTextView.setText("游戏开始了!");
mButton.setText("游戏中...");
mButton.setEnabled(false);
//发送消息
next(RANDOM_NUMBER);
}
private void next(int delayTime) {
//产生一个0——数组长度的随机数
int positon = new Random().nextInt(mPosition.length);
Message message = Message.obtain();
message.what = CODE;
message.arg1 = positon;
mMyHandler.sendMessageDelayed(message, delayTime);
//每发送一次消息,总数就加一
mTotalCount++;
}
/**
* 图片点击事件
* @param v
* @param event
* @return
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
//每次触碰到地鼠,则地鼠消失,打到地鼠的数量加一
v.setVisibility(View.GONE);
mSuccessCount++;
mTextView.setText("打到了" + mSuccessCount + "只,共" + MAX_COUNT + "只");
return false;
}
public static class MyHandler extends Handler {
private final WeakReference mWeakReference;
public MyHandler(MainActivity activity) {
this.mWeakReference = new WeakReference<>(activity);
}
@Override
public void handleMessage(Message msg) {
MainActivity activity = mWeakReference.get();
super.handleMessage(msg);
switch (msg.what) {
case CODE:
if (activity.mTotalCount > MAX_COUNT) {
//游戏结束,初始化游戏
activity.clear();
Toast.makeText(activity, "地鼠打完了!", Toast.LENGTH_SHORT).show();
return;
} else {
int position = msg.arg1;
activity.mImageView.setX(activity.mPosition[position][0]);
activity.mImageView.setY(activity.mPosition[position][1]);
activity.mImageView.setVisibility(View.VISIBLE);
//在随机位置上显示地鼠之后,再次发送消息
int randomTime = new Random().nextInt(RANDOM_NUMBER) + RANDOM_NUMBER;
activity.next(randomTime);
}
break;
}
}
}
/**
* 游戏结束,初始化游戏
*/
private void clear() {
mTotalCount = 0;
mSuccessCount = 0;
mImageView.setVisibility(View.GONE);
mButton.setText("点击开始");
mButton.setEnabled(true);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。