您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这期内容当中小编将会给大家带来有关android开发中利用handler制作一个打地鼠小游戏,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/main_imageview" android:layout_width="80dp" android:layout_height="80dp" android:src="@mipmap/icon" android:visibility="gone" /> <Button android:id="@+id/main_button" android:layout_width="90dp" android:layout_height="50dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="30dp" android:text="开始游戏" /> <TextView android:id="@+id/main_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>
java
package com.tcy.digettetest; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; 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 java.lang.ref.WeakReference; import java.util.IllegalFormatCodePointException; import java.util.Random; public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener { public static final int CODE = 1001; public static final int MAX_NUMBER = 10; public static final int RANDOM_NUMBER = 500; Button statrGameButton; TextView resultTextView; ImageView saratogaImage; int[][] mPostion = new int[][]{ {342, 180}, {432, 880}, {521, 256}, {429, 780}, {456, 976}, {145, 665}, {123, 678}, {564, 567}, }; int mTotalCount; int mSuccessCount; SaratogaHandler mHandler = new SaratogaHandler(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); setTitle("打加加"); } private void initView() { statrGameButton = findViewById(R.id.main_button); resultTextView = findViewById(R.id.main_textview); saratogaImage = findViewById(R.id.main_imageview); statrGameButton.setOnClickListener(this); saratogaImage.setOnTouchListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.main_button: startGame(); break; } } private void startGame() { statrGameButton.setEnabled(false); resultTextView.setText("开始啦"); statrGameButton.setText("游戏中……"); //发送消息 next(0); } public void next(int delayTime) { int position = new Random().nextInt(mPostion.length); Message message = Message.obtain(); message.what = CODE; message.arg1 = position; mHandler.sendMessageDelayed(message, delayTime); mTotalCount++; } @Override public boolean onTouch(View v, MotionEvent event) { v.setVisibility(View.GONE); mSuccessCount++; resultTextView.setText("打到了" + mSuccessCount + "只,共" + MAX_NUMBER + "只."); return false; } public static class SaratogaHandler extends Handler { final WeakReference<MainActivity> weakReference; public SaratogaHandler(MainActivity activity) { this.weakReference = new WeakReference<>(activity); } @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); MainActivity activity = weakReference.get(); switch (msg.what) { case CODE: if (activity.mTotalCount > MAX_NUMBER) { activity.clear(); Toast.makeText(activity, "小加加打完了", Toast.LENGTH_SHORT).show(); return; } int position = msg.arg1; activity.saratogaImage.setX(activity.mPostion[position][0]); activity.saratogaImage.setY(activity.mPostion[position][1]); activity.saratogaImage.setVisibility(View.VISIBLE); int randomTime = new Random().nextInt(RANDOM_NUMBER) + RANDOM_NUMBER; activity.next(randomTime); break; } } } private void clear() { mTotalCount = 0; mSuccessCount = 0; saratogaImage.setVisibility(View.GONE); statrGameButton.setText("点击开始"); statrGameButton.setEnabled(true); } }
上述就是小编为大家分享的android开发中利用handler制作一个打地鼠小游戏了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。