本文主要是介绍android日常 游戏:跳舞男孩,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package com.mz.boy;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;public class MainActivity extends Activity {private ImageView img;//定义一个线程对象private Thread thread;//定义一个handler对象private Handler handler;//定义一个线程运行的标志private boolean isDancing=true;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);img=(ImageView)this.findViewById(R.id.img);//handler对象实例化的过程,处理消息handler=new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);switch (msg.what) {case 0:img.setBackgroundResource(R.drawable.boy0);break;case 1:img.setBackgroundResource(R.drawable.boy1);break;case 2:img.setBackgroundResource(R.drawable.boy2);break;case 3:img.setBackgroundResource(R.drawable.boy3);break;case 4:img.setBackgroundResource(R.drawable.boy4);break;case 5:img.setBackgroundResource(R.drawable.boy5);break;case 6:img.setBackgroundResource(R.drawable.boy6);break;case 7:img.setBackgroundResource(R.drawable.boy7);break;case 8:img.setBackgroundResource(R.drawable.boy8);break;case 9:img.setBackgroundResource(R.drawable.boy9);break;case 10:img.setBackgroundResource(R.drawable.boy10);break;case 11:img.setBackgroundResource(R.drawable.boy11);break;case 12:img.setBackgroundResource(R.drawable.boy12);break;case 13:img.setBackgroundResource(R.drawable.boy13);break;case 14:img.setBackgroundResource(R.drawable.boy14);break;case 15:img.setBackgroundResource(R.drawable.boy15);break;case 16:img.setBackgroundResource(R.drawable.boy16);break;case 17:img.setBackgroundResource(R.drawable.boy17);break;case 18:img.setBackgroundResource(R.drawable.boy18);break;default:break;}}};thread=new MyThread();thread.start();}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubisDancing=false;super.onDestroy();}//子线程的定义class MyThread extends Thread{@Overridepublic void run() {// TODO Auto-generated method stubint what=1;while(isDancing){handler.sendEmptyMessage((what++)%19);try {//每隔多长时间线程运行thread.sleep(100);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}}}
}
以上是MainActivity中的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg"android:orientation="vertical" ><ImageView
android:id="@+id/img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center" /></LinearLayout>
以上为main.xml中代码
实现
这篇关于android日常 游戏:跳舞男孩的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!