import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.view.View; /** * @version 2012-8-10 上午10:25:09 **/ public class GameView extends View implements Runnable { Paint mPaint = null; public GameView(Context context) { super(context); mPaint = new Paint(); new Thread(this).start(); } @Override public void run() { // 判读该线程是否中断 while(!Thread.currentThread().isInterrupted()) { try { Thread.sleep(100); } catch(Exception e) { Thread.currentThread().interrupt(); } // 使用postInvalidate能够直接在线程中更新界面 postInvalidate(); } } @Override protected void onDraw(Canvas canvas) { // 设置画布颜色 canvas.drawColor(Color.BLACK); // 取消锯齿效果 mPaint.setAntiAlias(true); // 设置裁剪区域 canvas.clipRect(10, 10, 280, 260); // 锁定画布 canvas.save(); // 旋转画布 canvas.rotate(45.0f); // 设置画笔颜色 mPaint.setColor(Color.RED); // 画矩形 canvas.drawRect(new Rect(15, 15, 140, 70), mPaint); // 解锁画布 canvas.restore(); // 设置画笔颜色 mPaint.setColor(Color.GREEN); // 画矩形 canvas.drawRect(new Rect(150, 75, 260, 120), mPaint); } }
版权声明:本文为博主原创文章,未经博主容许不得转载。java