This example describes the Surfaceview learning examples of Android programming. Share to everyone for your reference, specific as follows:
Surfaceview is a subclass of view that is used in exactly the same way as any view-derived class, and can be applied to animations like other view and placed in a layout.
The surface in the Surfaceview package supports drawing using all the standard canvas methods described earlier in this chapter, as well as the full OpenGL ES Library.
Using OpenGL, you can draw any supported 2D or 3D objects on the surface, which can greatly improve performance by relying on hardware acceleration (when available), compared to simulating the same effect on the 2D canvas.
Surfaceview is particularly useful for displaying dynamic 3D images, such as those that use the Google Earth feature, or for interactive games that provide immersion experience. It is also the best choice for real-time display of camera previews.
The obvious difference between Surfaceview and View is that:
1. A view that inherits Surfaceview can be another thread, or update a view in a child thread.
2. The drawing method of the Surfaceview is performed in a child thread and the example of the view class is executed in the UI thread.
3, Surfaceview must use the Lockcanvas method to lock the canvas before drawing, and then draw the canvas, then use the Unlockcanvasandpost method to unlock the canvas when the drawing is complete, and then display to the screen.
The event handling rules for the Surfaceview class are the same as view.
Specific examples:
Activity
public class Activity01 extends activity {
Gamesurfaceview mgamesurfaceview;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Mgamesurfaceview = new Gamesurfaceview (this);
Setcontentview (Mgamesurfaceview);
}
@Override Public
Boolean ontouchevent (Motionevent event) {
if (event.getaction () = = Motionevent.action_down) {
mgamesurfaceview.x = Event.getx ();
MGAMESURFACEVIEW.Y = Event.gety ();
}
return true;
}
@Override Public
boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = Keyevent.keycode_back) {
this.finish ();
}
return true;
}
Gamesurfaceview
public class Gamesurfaceview extends Surfaceview implements Surfaceholder.callback, Runnable {Boolean mbloop = false;
Surfaceholder msurfaceholder = null;
int count = 0;
float x = +, y = 50;
int screenwidth = ScreenHeight = 800;
Public Gamesurfaceview {Super (context);
Mbloop = true;
Msurfaceholder = This.getholder ();
Msurfaceholder.addcallback (this);
This.setfocusable (TRUE); @Override public void surfacecreated (Surfaceholder holder) {New Thread (this). Start (). Start Paint thread} @Override public void surfacechanged (surfaceholder holder, int format, int width, int hei ght) {screenwidth = width; Reset width When screen orientation is changed screenheight = height; Reset height When screen orientation are changed} @Override public void surfacedestroyed (Surfaceholder holder)
{Mbloop = false;
@Override public void Run () {while (Mbloop) { Synchronized (Msurfaceholder) {OnDraw ();
try {thread.sleep (200);
The catch (Exception e) {}} is public void OnDraw () {Canvas Canvas = Msurfaceholder.lockcanvas ();
if (Msurfaceholder = NULL | | canvas = NULL) {return;
} if (Count <) {count++;
else {count = 0;
} Paint mpaint = new Paint ();
Mpaint.setantialias (TRUE);
Mpaint.setcolor (Color.cyan); Canvas.drawrect (0, 0, screenwidth, ScreenHeight, Mpaint);
Repaint background color switch (count% 4) {case 0:mpaint.setcolor (color.blue);
Break
Case 1:mpaint.setcolor (Color.green);
Break
Case 2:mpaint.setcolor (color.red);
Break
Case 3:mpaint.setcolor (Color.yellow);
Break
Default:mPaint.setColor (Color.White);
Break
} canvas.drawcircle (x, Y, M, mpaint); Msurfaceholder.unloCkcanvasandpost (canvas); }
}
Operation Effect:
Full instance code code click here to download the site.
I hope this article will help you with the Android program.