本文实例为大家分享了Android使用Handler实现打地鼠的具体代码,供大家参考,具体内容如下
1.实现效果
如下图所示:
2.代码实现
新建一个名为DiglettDemo的项目,activity_main.xml代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout 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"
tools:context = ".MainActivity" >
< ImageView
android:id = "@+id/imageView"
android:layout_width = "80dp"
android:layout_height = "80dp"
android:src = "@drawable/diglett"
android:visibility = "gone" />
< Button
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 = "点击开始" />
< TextView
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" />
</ RelativeLayout >
|
MainActivity.class代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
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<MainActivity> 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 );
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qingjianduoyun/article/details/80634068