The last article introduced the MediaPlayer related content, this time uses two articles to introduce the Surfaceview usage. 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, I am here directly in the ordinary user interface call Surfaceholder Lockcanvas and Unlockcanvasandpost.
Let's take a look at the screenshot of the program running:
Screenshot 1 mainly demonstrates the direct sine wave painting on the Surfaceview
In contrast to the above two figure, the right picture uses. Lockcanvas (null), while the left image uses. Lockcanvas (new Rect (OLDX, 0, OLDX + length,getwindowmanager (). Getdefaultdisplay (). GetHeight ()) to compare the two effects, because the left image is the specified rect painting, the efficiency will be higher than the full control of the right figure painting, and after the screen (Canvas.drawcolor (color.black) will not leave the last painting residue.
Next post the source code of the Main.xml:
<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 android:id= "@+id/button02" android:layout_width= "Wrap_content"
android:layout_height= "wrap_content" android:text= "timer painting" >
<surfaceview android:id= "@+id/" SurfaceView01 "
android:layout_width=" fill_parent "android:layout_height=" Fill_parent ">
Next post the program source code:
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 the ' is the ' The activity ' is the ' the '
Draw, Btntimerdraw;
Surfaceview SFV;
Surfaceholder SFH;
Private Timer Mtimer;
Private Mytimertask Mtimertask; int y_axis[],//Save the point on the y-axis of a sine wave centery,//the centerline oldx,oldy,//the point on the x-axis that is currently drawn to the X axis on the currentx;//@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 ();
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) (100
* Math.sin (i * 2 * math.pi/180));
Class Clickevent implements View.onclicklistener {@Override public void OnClick (View v) { if (v = = Btnsimpledraw) {Simpledraw (y_axis.length-1);//directly Draw sine wave} els E 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 (). Get Height ());/key: Get 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.LOCKCA
Nvas (NULL);
Canvas.drawcolor (Color.Black);/clear canvas sfh.unlockcanvasandpost (canvas);
}
}
The above is the entire contents of this article, I hope to learn more about Android software programming help.