The last time we introduced MediaPlayer, we introduced SurfaceView. SurfaceView is a very important drawing container because it can directly obtain image data from hardware interfaces such as memory or DMA, this time I will use two articles to introduce the usage of SurfaceView. There are many usage of SurfaceView on the Internet, and the writing method is also different, such as inheriting SurfaceView class or SurfaceHolder. callback class, which can be selected based on actual functional needs. I will call the lockCanvas and unlockCanvasAndPost of SurfaceHolder on the general user interface.
Let's take a look at the program running:
1. demonstrate how to directly draw a sine wave on SurfaceView.
Compare the two pictures on the left and right, and use. lockCanvas (null) on the Right, while use. lockCanvas (new Rect (oldX, 0, oldX + length,
GetWindowManager (). getdefadisplay display (). getHeight (), compare the two effects. Because the left image is painted by the specified Rect, the efficiency is higher than that of the full control on the right image, and after the screen is cleared (canvas. drawColor (Color. BLACK.
Next, paste the source code of main. xml:
View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Android: orientation = "vertical">
<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content">
<Button android: id = "@ + id/Button01" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "simple painting"> </Button>
<Button android: id = "@ + id/Button02" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "timer painting"> </Button>
</LinearLayout>
<SurfaceView android: id = "@ + id/SurfaceView01"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </SurfaceView>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Android: orientation = "vertical">
<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content">
<Button android: id = "@ + id/Button01" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "simple painting"> </Button>
<Button android: id = "@ + id/Button02" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "timer painting"> </Button>
</LinearLayout>
<SurfaceView android: id = "@ + id/SurfaceView01"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </SurfaceView>
</LinearLayout>
Next, paste the program source code:
View plaincopy to clipboardprint?
Package com. testSurfaceView;
Import java. util. Timer;
Import java. util. TimerTask;
Import android. app. Activity;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Rect;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. View;
Import android. widget. Button;
Public class testSurfaceView extends Activity {
/** Called when the activity is first created .*/
Button btnSimpleDraw, btnTimerDraw;
SurfaceView sfv;
SurfaceHolder sfh;
Private Timer mTimer;
Private MyTimerTask mTimerTask;
Int Y_axis [], // Save the point on the Y axis of the sine wave
CenterY, // center line
OldX, oldY, // The Last XY point
CurrentX; // the point on the X-axis currently drawn
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
BtnSimpleDraw = (Button) this. findViewById (R. id. Button01 );
BtnTimerDraw = (Button) this. findViewById (R. id. Button02 );
BtnSimpleDraw. setOnClickListener (new ClickEvent ());
BtnTimerDraw. setOnClickListener (new ClickEvent ());
Sfv = (SurfaceView) this. findViewById (R. id. SurfaceView01 );
Sfh = sfv. getHolder ();
// A timer that dynamically draws a sine wave
MTimer = new Timer ();
MTimerTask = new MyTimerTask ();
// Initialize Y-axis data
CenterY = (getWindowManager (). getdefadisplay display (). getHeight ()-sfv
. GetTop ()/2;
Y_axis = new int [getWindowManager (). getdefadisplay display (). getWidth ()];
For (int I = 1; I <Y_axis.length; I ++) {// calculate the sine wave
Y_axis [I-1] = centerY
-(Int) (100 * Math. sin (I * 2 * Math. PI/180 ));
}
}
Class ClickEvent implements View. OnClickListener {
@ Override
Public void onClick (View v ){
If (v = btnSimpleDraw