The use of Surfaceview

Source: Internet
Author: User
Tags gety

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.