Android surfaceview usage Summary

Source: Internet
Author: User

1. Concept

Surfaceview is a subclass of the View class. It is a very important drawing view to obtain image data directly from memory or DMA and other hardware interfaces. Its feature is that it can be drawn to the screen outside the main thread. In this way, the main thread blocking can be avoided when the drawing task is heavy, thus improvingProgramResponse speed. Surfaceview is often used in game development. The background, characters, and animations in the game should be drawn in the canvas whenever possible.

2. Implementation Method

1) implementation steps

A. inherit surfaceview

B. Implement the surfaceholder. Callback Interface

2) method to be rewritten

(1)Public VoidSurfacechanged (surfaceholder holder,IntFormat,IntWidth,IntHeight ){}//Triggered when the surface size changes

(2)Public VoidSurfacecreated (surfaceholder holder ){}//It is triggered when it is created. Generally, the drawing thread is called here.

(3)Public VoidSurfacedestroyed (surfaceholder holder ){}//It is triggered when the image is destroyed. Generally, the drawing thread is stopped and released here.

3) surfaceholder

Surfaceholder, the surface controller, used to manipulate the surface. It processes effects and animations drawn on its canvas, controls the surface, size, pixels, and so on.
Several Methods to note:

(1 ), Abstract   Void Addcallback (surfaceholder. Callback callback );
// Send a callback object to the current owner of surfaceview.
(2 ), Abstract Canvas lockcanvas ();
// Lock the canvas. Generally, after locking the canvas, you can use its returned canvas object to draw a picture on it.
(3 ), Abstract Canvas lockcanvas (rect dirty );
// Lock a certain area of the canvas for drawing, etc. .. because after drawing, the following unlockcanvasandpost will be called to change the display content.
// For games with relatively high memory requirements, you do not need to repeat pixels in other regions outside dirty to increase the speed.
(4 ), Abstract Void Unlockcanvasandpost (canvas );
// Stop locking the drawing and submit the changes.

4) summarize the entire process

Inherit surfaceview and implement surfaceholder. callback interface ----> surfaceview. getholder () obtains the surfaceholder object ----> surfaceholder. addcallback (callback) adds the callback function ----> surfaceholder. lockcanvas () Get the canvas object and lock the canvas ----> canvas painting ----> surfaceholder. unlockcanvasandpost (canvas) ends the lock drawing, and submits changes to display the drawing.

The following is a complete case:

 
Public ClassViewtestExtendsActivity {
 
@ Override
Public VoidOncreate (bundle savedinstancestate ){
Super. Oncreate (savedinstancestate );
Setcontentview (NewMyview (This));
}
      //  View internal class 
Class Myview Extends Surfaceview Implements Surfaceholder. Callback
{
Private Surfaceholder holder;
Private Mythread;
Public Myview (context ){
Super (Context );
// Todo auto-generated constructor stub
Holder = This . Getholder ();
Holder. addcallback ( This );
Mythread = New Mythread (holder ); // Create a drawing thread
}

@ Override
Public Void Surfacechanged (surfaceholder holder, Int Format,Int Width,
Int Height ){
// Todo auto-generated method stub

}

@ Override
Public Void Surfacecreated (surfaceholder holder ){
// Todo auto-generated method stub
Mythread. isrun = True ;
Mythread. Start ();
}

@ Override
Public Void Surfacedestroyed (surfaceholder holder ){
// Todo auto-generated method stub
Mythread. isrun = False ;
}
}
// Internal Thread class
Class Mythread Extends Thread
{
Private Surfaceholder holder;
Public Boolean Isrun;
Public Mythread (surfaceholder holder)
{
This . Holder = holder;
Isrun = True ;
}
@ Override
Public Void Run ()
{
Int Count = 0;
While (Isrun)
{
Canvas c = Null ;
Try
{
Synchronized (Holder ){
C = holder. lockcanvas (); // Lock the canvas. Generally, after locking the canvas, you can use its returned canvas object to draw a picture on it.
C. drawcolor (color. Black ); // Set canvas background color
Paint P = New Paint (); // Create paint brush
P. setcolor (color. White );
Rect r = New Rect (100, 50,300,250 );
C. drawrect (R, P );
C. drawtext ("this is the" + (count ++) + "second", 100,310, P );
Thread. Sleep (1000 ); // The sleep time is 1 second.
}
}
Catch (Exception e ){
E. printstacktrace ();
}
Finally
{
If (C! = Null )
{
Holder. unlockcanvasandpost (C ); // Stop locking the drawing and submit the changes.
}
}
}
}
}
}
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.