Android Development Surfaceview Summary

Source: Internet
Author: User

A common custom drawing UI interface class in Android: Surfaceview. You can complete related data updates in an asynchronous thread.

A few basic definitions are introduced first, and the following nouns are also designed in other knowledge:

1.Paint

Brushes, all the images and graphics are done by the brush.

The definition is very simple:

Paint paint = new paint ();

Paint.setcolor (Color.Black);//Set the color of the brush

2.Canvas

The canvas, images, and graphics are painted on the canvas through paint. Canvas objects are usually obtained through Surfaceholder.lockcanvax ().

Canvas canvas = Holder.lockcanvas ();

Canvas.drawrect (0,0,widht,height,paint);//Draw Rectangle

3.Surface

At the time of application, no specific implementation classes and abstract classes are used.

The description in the API is that surface is a handle to the original image buffer (the original image buffer is managed by a screen image synthesizer). So, you know that the data that the canvas needs to show is loaded onto your surface and then displayed on the screen.

4.SurfaceView

A screen display view that controls the display size, location, and so on surface. Plainly, like TextView.

Sub-class is required to implement the specific elaboration function, inheriting Surfaceview, but also need to implement the Surfaceholder.callback interface (explained later).

5.SurfaceHolder

How can you make it clear that Surfaceholder is the controller of surface and provides several methods:

Addcallback (Surfaceholder.callback Callback); Adding a surfaceholder.callback monitor implementation

Lockcanvas ();

Lockcanvas (Rect dirty);

Unlockcanvas (canvas canvas);

6.surfaceholder.callback

is an interface that implements a class that can listen for surfaceview state changes. The interface method is as follows:

Surfacecreated ();

Surfacechanged ();

Surfacedestory ();

  

The use of the demo, the completion of the function is to be a picture, up and down the slide:

1, Rbsurfaceview is the implementation class of surface.

public class Rbsurfaceview extends Surfaceview implements surfaceholder.callback{
private paint paint;
Private WindowManager WindowManager;
Private context context;
private float screenwidth,screenheight;
private float bitmapx,bitmapy;
private int alphanum;
Private Boolean Alphaflag;

Private Onrunthread Onrunthread;
Private Ondrawthread Ondrawthread;

@SuppressWarnings ("deprecation")
Public Rbsurfaceview (Context context) {
Super (context);
TODO auto-generated Constructor stub
This.context = context;
Getholder (). Addcallback (this);
WindowManager = (WindowManager) getcontext (). Getsystemservice (Context.window_service);
ScreenWidth = (float) windowmanager.getdefaultdisplay (). GetWidth ();
ScreenHeight = (float) windowmanager.getdefaultdisplay (). GetHeight ();
Paint = new paint ();
Onrunthread = new Onrunthread (this);
Ondrawthread = new Ondrawthread (this);
Onrunthread.start ();
}
@Override
protected void OnDraw (canvas canvas) {
TODO auto-generated Method Stub
Super.ondraw (canvas);
Paint.setcolor (Color.White);
Canvas.drawrect (0, 0, screenwidth, screenheight, paint);
Bitmap Bitmapa = Bitmapfactory.decoderesource (Context.getresources (), r.drawable.ic_launcher);
Canvas.drawbitmap (Bitmapa, BITMAPX, bitmapy, paint);
if (Alphaflag) {
Bitmap BITMAPB = Bitmapfactory.decoderesource (Context.getresources (), r.drawable.icon_gcoding);
Paint.setalpha (Alphanum);
Canvas.drawbitmap (BITMAPB, 0, 0, paint);
}
}
@Override
public void surfacecreated (Surfaceholder holder) {
TODO auto-generated Method Stub
Ondrawthread.start ();
}

@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

}
public float getbitmapx () {
return bitmapx;
}
public void setbitmapx (float bitmapx) {
THIS.BITMAPX = BITMAPX;
}
public float getbitmapy () {
return bitmapy;
}
public void Setbitmapy (float bitmapy) {
This.bitmapy = bitmapy;
}
public int Getalphanum () {
return alphanum;
}
public void Setalphanum (int alphanum) {
This.alphanum = Alphanum;
}
public float getscreenwidth () {
return screenwidth;
}
public void Setscreenwidth (float screenwidth) {
This.screenwidth = ScreenWidth;
}
public float getscreenheight () {
return screenheight;
}
public void Setscreenheight (float screenheight) {
This.screenheight = ScreenHeight;
}
public Boolean Isalphaflag () {
return alphaflag;
}
public void Setalphaflag (Boolean alphaflag) {
This.alphaflag = Alphaflag;
}
}
  

2, Ondrawthread is constantly let Rbsurfaceview OnDraw method to draw pictures

public class Ondrawthread extends thread{
Private Rbsurfaceview Surfaceview;
Private Surfaceholder holder;
Public Ondrawthread (Rbsurfaceview Surfaceview) {
Super ();
This.surfaceview = Surfaceview;
Holder = Surfaceview.getholder ();
}
@Override
public void Run () {
TODO auto-generated Method Stub
Super.run ();
Canvas canvas = null;
while (true) {
Canvas = Holder.lockcanvas (NULL);
Synchronized (Surfaceview) {
if (canvas! = null) {
Surfaceview.ondraw (canvas);
}
}
Holder.unlockcanvasandpost (canvas);
try {
Thread.Sleep (30);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
}
}

3, Onrunthread mainly responsible for constantly changing the coordinates of the picture, and then set to Rbsurfaceview.

public class Onrunthread extends thread{
Private Rbsurfaceview Surfaceview;
Private Surfaceholder holder;
Public Onrunthread (Rbsurfaceview Surfaceview) {
Super ();
This.surfaceview = Surfaceview;
Holder = Surfaceview.getholder ();
BITMAPX = 0;
bitmapy = 0;
}
Boolean Runflag;
int alphanum;
float bitmapx;
float bitmapy;
@Override
public void Run () {
TODO auto-generated Method Stub
Super.run ();
while (true) {
while (Bitmapx < Surfaceview.getscreenwidth ()) {
SURFACEVIEW.SETBITMAPX (BITMAPX);
Surfaceview.setbitmapy (bitmapy);
if (Runflag) {
Bitmapy = bitmapY-20;
}else {
Bitmapy = bitmapy + 20;
}
if (Bitmapy <=0) {
Runflag = false;
}else if (bitmapy >= surfaceview.getscreenheight () +60) {
Runflag = true;
}
try {
Thread.Sleep (30);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

Surfaceview.setalphaflag (TRUE);
for (alphanum = 0; alphanum <= 255; alphanum++) {
if (Alphanum = = 255) {
Surfaceview.setalphaflag (FALSE);
BITMAPX = 0;
Bitmapy = Surfaceview.getscreenheight () +60;
}
Surfaceview.setalphanum (Alphanum);
try {
Thread.Sleep (20);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
}

  

  

    

Android Development Surfaceview Summary

Related Article

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.