Android graphics rendering BASICS (2)

Source: Internet
Author: User

MainActivity is as follows:

package;import android.os.Bundle;import;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(, menu);        return true;    }}

SurfaceViewTest is as follows:

Package; 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 ){}}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.