This article describes the Android development of the multi-threading implementation of the use of custom control to draw a small ball and complete the ball automatic drop function method. Share to everyone for your reference, specific as follows:
1. Layout interface
<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=" Wra P_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:layou T_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, the package of a drawn diagram of the class
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
}
Move the public
void MoveLeft () {x-=10 to the left
;
Gameview.invalidate ();
}
Move the public
void MoveRight () {x+=10 to the right
;
Gameview.invalidate ();
}
Drop method public
void DownLoad () {
y+=10;
Gameview.invalidate ()//Redraw
} 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. Create a canvas and thread operation
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 The extends View {public static blocks block;
Public Handler Handler;
Defines the direction public static int dir=-1;
Up and down about 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);
Create Tetris Object 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
}
}
}; Create thread new Runnable () {@Override public void run () {while (true) {try{System.out.pri
NTLN ("Child Thread Name:::" +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);
Set the color of the canvas Canvas.drawargb (255, 0, 0, 255);
Set a brush Paint paint=new Paint ();
Paint.setargb (255, 255, 0, 0);
Canvas.drawcircle (Block.getx (), block.gety (), paint);
}
}
4, actions in 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 the activity {//Declare button control public buttons Btn_start; public pushbutton btn_right; public button Btn_
Left @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_game);
Gets the button btn_start= Findviewbyid (R.id.btn_start);
Btn_start.setonclicklistener (New Myonclicklistener ());
System.out.println ("Main thread:::" +thread.currentthread (). GetName ()); @Override public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds the items to the action bar if it I
s 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://g
Ameview gameview=new Gameview (gameactivity.this,null);
GameActivity.this.setContentView (r.layout.activity_layout);
Gets the control 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, "Move Left", Toast.length_long). Show ();
GameView.block.moveLeft ();
Gameview.dir=gameview.dirleft;
Break
Case R.id.btn_right:toast.maketext (gameactivity.this, "Move Right", Toast.length_long). Show ();
GameView.block.moveRight ();
Gameview.dir=gameview.dirright;
Break
Default:break;
}
}
}
}
I hope this article will help you with your Android programming.