MainActivity is as follows:
package com.cn;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity { private SurfaceViewTest mSurfaceViewTest; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSurfaceViewTest=new SurfaceViewTest(MainActivity.this); setContentView(mSurfaceViewTest); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; }}
SurfaceViewTest is as follows:
Package com.cn; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. paint. style; import android. graphics. rectF; import android. view. surfaceHolder; import android. view. surfaceView; // note // 1 rectangular construction method // RectF rectF = new RectF (10, 10,100,100); // RectF (float left, float top, float right, float bottom) // must ensure that Left <= right and top <= bottom // first, float left, float top // indicates the starting point of the rectangle (that is, the point in the upper left corner) // first, the second parameter float right and float bottom // indicate the end point of the rectangle (that is, the point in the lower right corner) // 2 draw an ellipse // It is not imagined that "direct" is used to draw an ellipse. // instead, an ellipse is drawn in a rectangle. // because a rectangle locates the elliptic public class SurfaceViewTest. extends SurfaceView implements SurfaceHolder. callback {private Context mContext; private Paint mPaint; public SurfaceViewTest (Context context) {super (context); this. mContext = context ;/ /Set the implementation of the lifecycle callback interface this. getHolder (). addCallback (this); mPaint = new Paint (); // anti-sawtooth mPaint. setAntiAlias (true) ;}@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); // --------> draw a white rectangular background // set the paint brush color mPaint. setColor (Color. WHITE); // draw a canvas with a WHITE rectangular background. drawRect (0, 0,350,450, mPaint); mPaint. reset (); // --------> draw a straight mPaint. setColor (Color. RED); // set the line width mPaint. setStrokeWidth (10); canvas. drawLine (30, 10,200, 70, mP Aint); mPaint. reset (); // --------> draw a hollow rectangle with a border (solid rectangle) mPaint. setStrokeWidth (5); mPaint. setARGB (50, 0,255, 0); // The next comment is the solid rectangle mPaint. setStyle (Style. STROKE); RectF rectF1 = new RectF (10, 10,150,150); canvas. drawRect (rectF1, mPaint); mPaint. reset (); // --------> draw a hollow circle (solid circle) mPaint. setColor (Color. GREEN); mPaint. setAntiAlias (true); // The next comment is the solid heart circle mPaint. setStyle (Style. STROKE); canvas. drawCircle (150,200, 30, mPaint); mPaint. Reset (); // --------> draw an oval filled rectangle mPaint. setColor (Color. YELLOW); RectF rectF2 = new RectF (10,230,200,300); canvas. drawOval (rectF2, mPaint); mPaint. reset (); // --------> draw the text mPaint. setColor (Color. BLACK); mPaint. setTextSize (25); // set the Text underline mPaint. setUnderlineText (true); canvas. drawText ("Hello everyone, Hello world", 50,340, mPaint); mPaint. reset ();} // implement SurfaceHolder. method public void surfaceCreated (SurfaceHolder holder) in Callback) {Canvas canvas = holder. lockCanvas (); try {synchronized (holder) {onDraw (canvas) ;}} catch (Exception e) {} finally {if (canvas! = Null) {holder. unlockCanvasAndPost (canvas) ;}}// implement SurfaceHolder. in Callback, the public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}// implements SurfaceHolder. method public void surfaceDestroyed (SurfaceHolder holder) {}} in Callback ){}}