This paper introduces the MediaPlayer usage of Android, and introduces the Surfaceview,surfaceview because it can obtain image data directly from hardware interfaces such as memory or DMA, so it is a very important drawing container, This time I will introduce the usage of surfaceview in more detail. There are many uses of Surfaceview on the web, and different ways of writing them, such as inheriting Surfaceview classes, or inherit Surfaceholder.callback class, this can be based on the actual needs of the function of their own choice, the method described in this article directly in the ordinary user interface calls Surfaceholder Lockcanvas and Unlockcanvasandpost.
Let's take a look at the results of the program run as follows:
Screenshot 1 mainly demonstrates the direct sine wave painting on the Surfaceview.
Take a look at the following two screenshots:
Compared to the top and bottom two graphs, the following figure uses. Lockcanvas (null), while the above diagram uses. Lockcanvas (new Rect (OLDX, 0, oldx + length,
Getwindowmanager (). Getdefaultdisplay (). GetHeight ()) to compare the two effects, because the image above is painted with the specified rect, the efficiency is higher than the entire control drawing in the following illustration, and after the screen is cleared ( Canvas.drawcolor (Color.Black)) will not leave the last painting residue.
Main.xml source code is as follows:
<?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 post the Java Program source code as follows:
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; The public class Testsurfaceview extends activity {/** called the ' when ' is the ' The activity ' is the ' the '
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,//centerline oldx,oldy,//the last xy point currentx;//the point on the x-axis currently drawn @Override public void OnCreate (B
Undle 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 ();
Dynamic drawing of sine wave timer Mtimer = new timer ();
Mtimertask = new Mytimertask ();
Initializes the y-axis data CenterY = (Getwindowmanager (). Getdefaultdisplay (). GetHeight ()-SFV. GetTop ())/2;
Y_axis = new Int[getwindowmanager (). Getdefaultdisplay (). GetWidth ()]; for (int i = 1; i < y_axis.length i++) {//Compute sine wave y_axis[i-1] = centery-(int) (M * Math.sin (i * 2 * MATH.PI/1
80)); Class Clickevent implements View.onclicklistener {@Override public void OnClick (View v) {if (v = = Btnsimpledr
AW) {Simpledraw (y_axis.length-1);//directly draw sine wave} else if (v = = Btntimerdraw) {oldy = CenterY; Mtimer.schedule (mtimertask, 0, 5);//dynamically Draw sine wave}} class Mytimertask extends TimerTask {@Override public void run ()
{Simpledraw (CURRENTX);
currentx++;//to Forward if (CurrentX = = y_axis.length-1) {//If the end point, then clear screen again Cleardraw (); CurrentX = 0;
Oldy = CenterY;
}/* * Draw the specified area/void Simpledraw (int length) {if (length = 0) oldx = 0; Canvas Canvas = Sfh.lockcanvas (New Rect (OLDX, 0, oldx + length, Getwindowmanager (). Getdefaultdisplay (). GetHeight ()));
Key: Get the canvas log.i ("Canvas:", string.valueof (OLDX) + "," + string.valueof (oldx + length));
Paint mpaint = new Paint ();
Mpaint.setcolor (Color.green);//Brush is Green mpaint.setstrokewidth (2);//set brush thickness int y;
for (int i = OLDX + 1; i < length; i++) {//painting sine wave y = y_axis[i-1];
Canvas.drawline (OLDX, Oldy, I, Y, mpaint);
OLDX = i;
Oldy = y;
} sfh.unlockcanvasandpost (canvas);//unlock canvas, submit painted image} void Cleardraw () {Canvas canvas = Sfh.lockcanvas (NULL);
Canvas.drawcolor (Color.Black);/clear canvas sfh.unlockcanvasandpost (canvas);
}
}
Here also need to note: for (int i = OLDX + 1; i < length; i++) {//Painting sine wave, in. Lockcanvas () Specify the number of times that you want to reduce looping lines within RECT, which can improve drawing efficiency.