Android实现掷骰子效果
程序员文章站
2023-02-22 08:03:04
本文实例为大家分享了android实现掷骰子效果的具体代码,供大家参考,具体内容如下利用handler接受子线程的消息完成骰子点数的不断更替演示start:开始游戏stop:停止游戏recover:重...
本文实例为大家分享了android实现掷骰子效果的具体代码,供大家参考,具体内容如下
利用handler接受子线程的消息完成骰子点数的不断更替
演示
start:开始游戏
stop:停止游戏
recover:重置色子到初始状态
代码
package com.example.homeworkten; import androidx.annotation.nonnull; import androidx.appcompat.app.appcompatactivity; import android.os.bundle; import android.os.handler; import android.os.message; import android.view.view; import android.widget.button; import android.widget.imageview; import android.widget.toast; public class mainactivity extends appcompatactivity { private imageview imageview1,imageview2,imageview3; private button button1,button2,button3; private imageview[] imageviews; private handler handler; //子线程 private thread thread; //记录每个色子的点数 private int number[]; //色子的总点数 private int count; //volatile修饰符用来保证其它线程读取的总是该变量的最新的值 public volatile boolean isstop = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); number = new int[3]; imageview1 = findviewbyid(r.id.imageview1); imageview2 = findviewbyid(r.id.imageview2); imageview3 = findviewbyid(r.id.imageview3); button1 = findviewbyid(r.id.button1); button2 = findviewbyid(r.id.button2); button3 = findviewbyid(r.id.button3); imageviews = new imageview[]{imageview1,imageview2,imageview3}; button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //判断线程是否存在 if (thread != null&&isstop == true){ isstop = false; } handler = new handler(){ @override public void handlemessage(@nonnull message msg) { super.handlemessage(msg); for(int i = 0;i < 3;i++) { switch (number[i]) { case 1: imageviews[i].setimageresource(r.drawable.t1); break; case 2: imageviews[i].setimageresource(r.drawable.t2); break; case 3: imageviews[i].setimageresource(r.drawable.t3); break; case 4: imageviews[i].setimageresource(r.drawable.t4); break; case 5: imageviews[i].setimageresource(r.drawable.t5); break; case 6: imageviews[i].setimageresource(r.drawable.t6); break; } } } }; thread = new thread(new runnable() { @override public void run() { while (!isstop) { message message = handler.obtainmessage(); //总点数归零 count = 0; for (int i = 0; i < 3; i++) { try { thread.sleep(50); } catch (interruptedexception e) { e.printstacktrace(); } //生成随机数 int random = (int) (math.random() * 6 + 1); number[i] = random; count += random; } handler.sendmessage(message); } } }); thread.start(); } }); //停止掷筛子 button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(isstop == false) { isstop = true; try { //当子线程执行完以后才继续执行主线程 thread.join(); } catch (interruptedexception e) { e.printstacktrace(); } //显示掷出的点数 toast.maketext(mainactivity.this, "你掷的点数为" + count, toast.length_short).show(); }else { toast.maketext(mainactivity.this, "请点击开始键进行掷色子", toast.length_short).show(); } } }); //将色子还原到初始状态 button3.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if(isstop == true) { for (int i = 0; i < 3; i++) { imageviews[i].setimageresource(r.drawable.t1); } }else { toast.maketext(mainactivity.this, "请先停止游戏,再重新开始", toast.length_short).show(); } } }); } }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity"> <imageview android:id="@+id/imageview1" android:layout_width="100dp" android:layout_height="80dp" android:layout_marginstart="10dp" android:layout_marginleft="10dp" android:layout_margintop="40dp" android:layout_marginend="10dp" android:layout_marginright="10dp" app:layout_constraintend_tostartof="@+id/imageview2" app:layout_constrainthorizontal_bias="0.482" app:layout_constraintstart_tostartof="parent" app:layout_constrainttop_totopof="parent" app:srccompat="@drawable/t1" /> <imageview android:id="@+id/imageview2" android:layout_width="100dp" android:layout_height="80dp" android:layout_marginstart="10dp" android:layout_marginleft="10dp" android:layout_margintop="40dp" android:layout_marginend="10dp" android:layout_marginright="10dp" app:layout_constraintend_tostartof="@+id/imageview3" app:layout_constraintstart_toendof="@+id/imageview1" app:layout_constrainttop_totopof="parent" app:srccompat="@drawable/t1" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginstart="10dp" android:layout_marginleft="10dp" android:layout_margintop="40dp" android:layout_marginend="10dp" android:layout_marginright="10dp" android:text="start" app:layout_constraintend_tostartof="@+id/button2" app:layout_constraintstart_tostartof="parent" app:layout_constrainttop_tobottomof="@+id/imageview1" /> <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginstart="10dp" android:layout_marginleft="10dp" android:layout_margintop="40dp" android:layout_marginend="10dp" android:layout_marginright="10dp" android:text="stop" app:layout_constraintend_tostartof="@+id/button3" app:layout_constraintstart_toendof="@+id/button1" app:layout_constrainttop_tobottomof="@+id/imageview2" /> <button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginstart="10dp" android:layout_marginleft="10dp" android:layout_margintop="40dp" android:layout_marginend="10dp" android:layout_marginright="10dp" android:text="recover" app:layout_constraintend_toendof="parent" app:layout_constraintstart_toendof="@+id/button2" app:layout_constrainttop_tobottomof="@+id/imageview3" /> <imageview android:id="@+id/imageview3" android:layout_width="100dp" android:layout_height="80dp" android:layout_marginstart="10dp" android:layout_marginleft="10dp" android:layout_margintop="40dp" android:layout_marginend="10dp" android:layout_marginright="10dp" app:layout_constraintend_toendof="parent" app:layout_constraintstart_toendof="@+id/imageview2" app:layout_constrainttop_totopof="parent" app:srccompat="@drawable/t1" /> </androidx.constraintlayout.widget.constraintlayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。