Android开发Thread+Handler演示样本(打地鼠)

时间:2023-03-09 02:18:05
Android开发Thread+Handler演示样本(打地鼠)

直接在代码

package com.mingrisoft;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast; public class MainActivity extends Activity {
private int i = 0; // 记录其打到了几仅仅地鼠
private ImageView mouse; // 声明一个ImageView对象
private Handler handler; // 声明一个Handler对象
public int[][] position = new int[][] { { 231, 325 }, { 424, 349 },
{ 521, 256 }, { 543, 296 }, { 719, 245 }, { 832, 292 },
{ 772, 358 } }; // 创建一个表示地鼠位置的数组 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mouse = (ImageView) findViewById(R.id.imageView1); // 获取ImageView对象
mouse.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
v.setVisibility(View.INVISIBLE); // 设置地鼠不显示
i++;
Toast.makeText(MainActivity.this, "打到[ " + i + " ]仅仅地鼠。",
Toast.LENGTH_SHORT).show(); // 显示消息提示框
return false;
}
}); handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int index = 0;
if (msg.what == 0x101) {
index = msg.arg1; // 获取位置索引值
mouse.setX(position[index][0]); // 设置X轴位置
mouse.setY(position[index][1]); // 设置Y轴位置
mouse.setVisibility(View.VISIBLE); // 设置地鼠显示
}
super.handleMessage(msg);
} };
Thread t = new Thread(new Runnable() { @Override
public void run() {
int index = 0; // 创建一个记录地鼠位置的索引值
while (!Thread.currentThread().isInterrupted()) {
index = new Random().nextInt(position.length); // 产生一个随机数
Message m = handler.obtainMessage(); // 获取一个Message
m.what = 0x101; // 设置消息标识
m.arg1 = index; // 保存地鼠标位置的索引值
handler.sendMessage(m); // 发送消息 try {
Thread.sleep(new Random().nextInt(500) + 500); // 休眠一段时间
} catch (InterruptedException e) {
e.printStackTrace();
} } } });
t.start(); // 开启线程 } }

布局文件:

<?

xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl"
android:background="@drawable/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/mouse" />
</FrameLayout>

版权声明:本文博主原创文章。博客,未经同意不得转载。