New Project Usingsurfaceview
Create a new class Gameview inherit to Surfaceview and implement the callback interface
Add a constructor method, and make a copy of the callback three non-implemented methods
Create a container contanier to host all view
Public class Contanier {
Private List<contanier> children=null;
Public Contanier () {
Children = New arraylist<contanier> ();
}
Public void Draw (canvas canvas) {
Childrenview (canvas);
for (Contanier C:children) {
C.draw (canvas);
}
}
Public void childrenview (canvas canvas) {
}
Public void Addchildrenview (Contanier child) {
Children.add (child);
}
Public void Removechildrenview (Contanier child) {
Children.remove (child);
}
}
Create a new Contanier sub-class rect
Public class Rect extends contanier{
Private Paint paint = null;
Public Rect () {
paint=New Paint ();
Paint.setcolor (Color. RED);
}
Replication Childrenview Method
Public void childrenview (canvas canvas) {
Super. Childrenview (canvas);
Canvas.drawrect (0, 0, +, +, paint);
}
}
Create a new Contanier subclass
Public class Circle extends contanier{
Private Paint paint=null;
Public Circle () {
paint=New Paint ();
Paint.setcolor (Color. BLUE);
}
@Override
Public void childrenview (canvas canvas) {
TODO auto-generated Method stub
Super. Childrenview (canvas);
Canvas.drawcircle (N, A, (), paint);
}
}
Returns class Gameview for initialization
Private Contanier Contanier;
Private Rect rect;
Private Circle Circle;
Public Gameview (Context context) {
Super (context);
Contanier = new contanier ();
Rect = new rect ();
Circle = New circle ();
Rect.addchildrenview (circle);
Contanier.addchildrenview (rect);
Add callback
Getholder (). Addcallback (this);
}
Call the Draw method in the Surfacechanged method
In the Mainactivity
Setcontentview (new Gameview (this));
Move a drawn shape
Modifying the Contanier class
Private float x = 0,y = 0;
Public float GetX () {
return x;
}
Public void SetX (float x) {
this. x = x;
}
Public float GetY () {
return y;
}
Public void sety (float y) {
this. y = y;
}
Public void Draw (canvas canvas) {
Canvas.save ();
canvas. Translate (GetX (), GetY ());
Childrenview (canvas);
for (Contanier C:children) {
C.draw (canvas);
}
Canvas.restore ();
}
Modifying the Childrenview method of a Rect class
Public void childrenview (canvas canvas) {
Super. Childrenview (canvas);
Canvas.drawrect (0, 0, +, +, paint);
this. Sety (GetY () +1);
}
Modify the class Gameview for a move operation
Private Timer timer = null;
Private TimerTask task = null;
Public void Starttimer () {
Timer = New timer ();
Task = new timertask () {
@Override
Public void Run () {
Draw ();
}
};
Timer.schedule (Task, 100, 100);
}
Public void Stoptimer () {
if (Timer! = null) {
Timer.cancel ();
Timer = null;
}
}
Public void surfacecreated (surfaceholder holder) {
TODO auto-generated Method stub
Starttimer ();
}
@Override
Public void surfacechanged (surfaceholder holder, int format, int width, int height) {
TODO auto-generated Method stub
}
@Override
Public void surfacedestroyed (surfaceholder holder) {
TODO auto-generated Method stub
Stoptimer ();
}
The use of Surfaceview