this article from http://blog.csdn.net/hellogv/ , the reference must be indicated by the source!
Last Introduction MediaPlayer A little introduction to the Surfaceview,surfaceview because it can be directly from memory or DMA and other hardware interface to obtain image data, it is a very important drawing container, this time I use two articles to introduce the use of Surfaceview. Online introduction of the use of Surfaceview there are many, writing also different layers, such as inheriting Surfaceview class, or inherit the Surfaceholder.callback class, this can be based on the function of the actual needs of their own choice, I here directly in the ordinary user interface calls Surfaceholder Lockcanvas and Unlockcanvasandpost.
Let's take a look at the program execution:
1 The main demonstration of direct sine wave painting on the Surfaceview
Control the left and right two figures above, and use. Lockcanvas (null) for the image on the left, and the. Lockcanvas (New Rect (OLDX, 0, oldx + length,
Getwindowmanager (). Getdefaultdisplay (). GetHeight ()), compare the two effects, because the left image is drawn by the specified rect, so the efficiency will be higher than the full control drawing on the right, and after the screen is cleared ( Canvas.drawcolor (Color.Black)) will not leave the last painting residue.
Next, post the source code for Main.xml:
<?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 Order 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;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,//the center line OLDX, oldy,//the point @overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (currentx;//) on the x-axis currently drawn to the previous XY point. 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 ();// Dynamically draw sine wave timer Mtimer = new timer (); mtimertask = new Mytimertask ();//Initialize 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++) {//Calculate sine wave y_ Axis[i-1] = centery-(int) (+ * Math.sin (i * 2 * math.pi/180));}} Class Clickevent implements View.onclicklistener {@Overridepublic void OnClick (View v) {if (v = = Btnsimpledraw) {Simpledra W (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 {@Overridepublic void run () {Simpledraw (currentx); currentx++;//go forward if (CurrentX = = Y_ AXIS.LENGTH-1) {//Assuming to the end, 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 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 weight 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 picture}void Cleardraw () {Canvas canvas = Sfh.lockcanvas (NULL); Canvas.drawcolor (Color.Black);//Clear Canvas sfh.unlockcanvasandpost (canvas);}}
Note for the for (int i = OLDX + 1; i < length; i++) {//painting sine wave this sentence, in. Lockcanvas () specifies the number of times the loop line is lowered within a rect to improve drawing efficiency.
Android improves the basic use of the second article Surfaceview