使用Handler实现打地鼠(下)。
该说不说,上代码!
首先给地鼠一个位置:
int position= msg.arg1;
activity.mDiglettImageView.setX(activity.mPosition[position][0]);
activity.mDiglettImageView.setY(activity.mPosition[position][1]);
activity.mDiglettImageView.setVisibility(View.VISIBLE);
int randomTime=new Random().nextInt(500)+500;//随机从0-500之间取一个数
activity.next(randomTime);
break;
然后在next里面写方法,延时、自加:
int position=new Random().nextInt(mPosition.length);
Message message=Message.obtain();
message.what=123;
message.arg1=position;
mHandler.sendMessageDelayed(message,delayTime);
mTotalCount++;
start方法:
//发送消息 handlers sendmessagedelayer
mResultTextView.setText("开始啦");
mStarButton.setText("游戏中...");
mStarButton.setEnabled(false);
next(0);
如果打到,接停止:
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setVisibility(View.GONE);
mSuccessCount++;
mResultTextView.setText("打到了"+mSuccessCount+"只,共"+Max_count+"只.");
return false;
}
最后数据清零:
private void clear() {
mTotalCount=0;
mSuccessCount=0;
mDiglettImageView.setVisibility(View.GONE);
mStarButton.setText("点击开始");
mStarButton.setEnabled(true);
}
完整的代码如下:
package com.example.handlerproject;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.lang.ref.WeakReference;
import java.util.Random;
public class DiglettActivity extends AppCompatActivity implements View.OnClickListener ,View.OnTouchListener{
private TextView mResultTextView;
private ImageView mDiglettImageView;
private Button mStarButton;
//坐标用二维数组表示
public int[][] mPosition=new int[][]{
{342,180},{432,880},
{354,215},{456,741},
{258,180},{147,880},
{126,456},{159,426}
};
private int mTotalCount;
private int mSuccessCount;
public static final int Max_count=10;
private DiglettHandler mHandler=new DiglettHandler(this);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diglettactivity);
//初始化控件
initView();
}
private void initView() {
mResultTextView = findViewById(R.id.text_View);
mDiglettImageView = findViewById(R.id.image_View);
mStarButton = findViewById(R.id.button);
mStarButton.setOnClickListener(this);
mDiglettImageView.setOnTouchListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
start();
break;
}
}
private void start() {
//发送消息 handlers sendmessagedelayer
mResultTextView.setText("开始啦");
mStarButton.setText("游戏中...");
mStarButton.setEnabled(false);
next(0);
}
public void next(int delayTime){
int position=new Random().nextInt(mPosition.length);
Message message=Message.obtain();
message.what=123;
message.arg1=position;
mHandler.sendMessageDelayed(message,delayTime);
mTotalCount++;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setVisibility(View.GONE);
mSuccessCount++;
mResultTextView.setText("打到了"+mSuccessCount+"只,共"+Max_count+"只.");
return false;
}
//做一个handler,实现handlers,不然会引起内存泄露
public static class DiglettHandler extends Handler{
public final WeakReference<DiglettActivity> mWeakReference;
public DiglettHandler(DiglettActivity activity) {
this.mWeakReference = new WeakReference<>(activity);
}
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
DiglettActivity activity=mWeakReference.get();
switch (msg.what){
case 123:
if (activity.mTotalCount>Max_count){
activity.clear();
Toast.makeText(activity, "地鼠打完了", Toast.LENGTH_SHORT).show();
return;
}
int position= msg.arg1;
activity.mDiglettImageView.setX(activity.mPosition[position][0]);
activity.mDiglettImageView.setY(activity.mPosition[position][1]);
activity.mDiglettImageView.setVisibility(View.VISIBLE);
int randomTime=new Random().nextInt(500)+500;//随机从0-500之间取一个数
activity.next(randomTime);
break;
}
}
}
private void clear() {
mTotalCount=0;
mSuccessCount=0;
mDiglettImageView.setVisibility(View.GONE);
mStarButton.setText("点击开始");
mStarButton.setEnabled(true);
}
}
|