首页 > 系统 > Android > 正文

Android开发之多线程中实现利用自定义控件绘制小球并完成小球自动下落功能实例

2020-04-11 11:07:37
字体:
来源:转载
供稿:网友

本文实例讲述了Android开发之多线程中实现利用自定义控件绘制小球并完成小球自动下落功能的方法。分享给大家供大家参考,具体如下:

1、布局界面

<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".GameActivity" > <www.csdn.net.tetris.view.GameView  android:id="@+id/gameview"  android:layout_width="wrap_content"  android:layout_height="wrap_content"/> <Button  android:id="@+id/btn_left"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignLeft="@+id/gameview"  android:layout_alignParentBottom="true"  android:text="@string/btn_text" /> <Button  android:id="@+id/btn_right"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignBottom="@+id/gameview"  android:layout_centerHorizontal="true"  android:layout_alignParentBottom="true"  android:text="@string/btn_right" /></RelativeLayout>

2、封装的一个绘制的图的类

package www.csdn.net.tetris.domain;import www.csdn.net.tetris.view.GameView;public class Block { private int x=20,y=20; public GameView gameView; public Block(GameView gameView){  this.gameView=gameView; } //向左移动 public void moveLeft(){  x-=10;  gameView.invalidate(); } //向右移动 public void moveRight(){  x+=10;  gameView.invalidate(); } //下落方法  public void downLoad(){   y+=10;   gameView.invalidate();//重新绘制  } public int getX() {  return x; } public void setX(int x) {  this.x = x; } public int getY() {  return y; } public void setY(int y) {  this.y = y; }}

3、创建一个画布和线程的操作

package www.csdn.net.tetris.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.view.View;import www.csdn.net.tetris.domain.Block;public class GameView extends View { public static Block block; public Handler handler; //定义方向 public static int dir=-1; //上下左右 public static final int DIRUP=1; public static final int DIRDOWN=2; public static final int DIRLEFT=3; public static final int DIRRIGHT=4; public GameView(Context context,AttributeSet attrs) {  super(context);  //创建俄罗斯方块对象  this.block=new Block(this);  handler=new Handler(){   public void handleMessage(Message msg) {    super.handleMessage(msg);    switch (msg.what) {    case DIRLEFT:     GameView.block.moveLeft();     break;    case DIRRIGHT:     GameView.block.moveRight();     break;    default:     GameView.block.downLoad();     break;    }   }  };  //创建线程  new Thread (new Runnable(){   @Override   public void run() {    while(true){     try{      System.out.println("子线程名称:::"+Thread.currentThread().getName());      //block.downLoad();      Thread.sleep(1000);      handler.sendEmptyMessage(dir);     }catch(Exception e){      e.printStackTrace();     }    }   }  }).start(); } @Override protected void onDraw(Canvas canvas) {  super.onDraw(canvas);  //设置了画布的颜色  canvas.drawARGB(255, 0, 0, 255);  //设置一个画笔  Paint paint=new Paint();  paint.setARGB(255, 255, 0, 0);  canvas.drawCircle(block.getX(), block.getY(), 10, paint); }}

4、在MainActivity中的操作

package com.example.tetris;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.Toast;import www.csdn.net.tetris.domain.Block;import www.csdn.net.tetris.view.GameView;public class GameActivity extends Activity {//声明按钮控件public Button btn_start;public Button btn_right;public Button btn_left;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_game);//获取按钮控件btn_start=(Button) findViewById(R.id.btn_start);btn_start.setOnClickListener(new MyOnClickListener());System.out.println("主线程:::"+Thread.currentThread().getName());}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}class MyOnClickListener implements View.OnClickListener{@Overridepublic void onClick(View v) {int id=v.getId();switch (id) {case R.id.btn_start://GameView gameView=new GameView(GameActivity.this,null);GameActivity.this.setContentView(R.layout.activity_layout);//获取控件btn_right=(Button) findViewById(R.id.btn_right);btn_left=(Button) findViewById(R.id.btn_left);btn_left.setOnClickListener(new MyOnClickListener());btn_right.setOnClickListener(new MyOnClickListener());break;case R.id.btn_left:Toast.makeText(GameActivity.this, "向左移动", Toast.LENGTH_LONG).show();GameView.block.moveLeft();//GameView.dir=GameView.DIRLEFT;break;case R.id.btn_right:Toast.makeText(GameActivity.this, "向右移动", Toast.LENGTH_LONG).show();GameView.block.moveRight();//GameView.dir=GameView.DIRRIGHT;break;default:break;}}}}

希望本文所述对大家Android程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表