from:
http://www.oschina.net/question/54100_29060
The android canvas has many fun functions to help me to draw the view . The warning point is , onDraw function WILL NOT update every frame automatically, must call the invalidate() to force it to refresh the screen.
Remember the OpenGL ES , in the Renderer , onDrawFrame(GL10 gl) will update automatically every frame to refresh the screen.
But the advantage of the canvas is , it can drawBitmap as the java SDK default !
扫描二维码关注公众号,回复:
744785 查看本文章
package com.drawviewtest; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.opengl.Matrix; import android.os.Bundle; import android.util.Log; import android.view.View; import java.util.logging.Level; import java.util.logging.Logger; public class DrawViewTest extends Activity { private Paint paint; private int i=0; private int times=1; private class DrawView extends View{ public DrawView(Context context){ super(context); //draw text part // paint=new Paint(); // paint.setColor(Color.GREEN); // paint.setTextSize(25); // paint.setAntiAlias(true); //dynamic draw part paint=new Paint(); paint.setColor(Color.GREEN); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); } @Override protected void onDraw(Canvas canvas) { //draw text part // super.onDraw(canvas); // // text, x, y, paintParam // canvas.drawText("Hello world", 5, 230, paint); // // this.invalidate(); //dynamic draw part super.onDraw(canvas); Log.i("l","this run"+i+"times!"); canvas.drawArc(new RectF(60,120,260,320), 0, i, true, paint); if(i<360){ i+=10; } else{ i=0; } try { Thread.sleep(500); } catch (Exception ex) { Log.i("l","thread exception:"+ex); } this.invalidate(); } } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new DrawView(this)); } }