本文实例讲述了android开发之多线程中实现利用自定义控件绘制小球并完成小球自动下落功能的方法。分享给大家供大家参考,具体如下:
1、布局界面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<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、封装的一个绘制的图的类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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、创建一个画布和线程的操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
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中的操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
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;
@override
protected 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());
}
@override
public 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{
@override
public 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程序设计有所帮助。