import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.Rect;import android.graphics.RectF;import android.graphics.drawable.ShapeDrawable;import android.graphics.drawable.shapes.OvalShape;import android.graphics.drawable.shapes.PathShape;import android.graphics.drawable.shapes.RectShape;import android.view.View;/** * @version 2012-8-10 上午10:25:09 **/public class GameView extends View implements Runnable { Paint mPaint = null; GameView2 mGameView2 = null; public GameView(Context context) { super(context); mPaint = new Paint(); mGameView2 = new GameView2(context); 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); // 画空心 mPaint.setStyle(Paint.Style.STROKE); { // 定义矩形 Rect rect1 = new Rect(); // 设置矩形大小 rect1.left = 5; rect1.top = 5; rect1.bottom = 25; rect1.right = 45; mPaint.setColor(Color.BLUE); // 绘制矩形 canvas.drawRect(rect1, mPaint); mPaint.setColor(Color.YELLOW); // 绘制圆形(圆心x坐标,圆心y坐标,半径r,p) canvas.drawCircle(40, 70, 30, mPaint); // 椭圆形 RectF rectf1 = new RectF(); rectf1.left = 80; rectf1.top = 30; rectf1.right = 150; rectf1.bottom = 70; mPaint.setColor(Color.LTGRAY); canvas.drawOval(rectf1, mPaint); // 绘制多边形 Path path1 = new Path(); // 起始点 path1.moveTo(150 + 5, 80 - 50); path1.lineTo(150 + 45, 80 - 50); path1.lineTo(150 + 30, 120 - 50); path1.lineTo(150 + 20, 120 - 50); // 是这些点构成封闭的多边形 path1.close(); mPaint.setColor(Color.GRAY); canvas.drawPath(path1, mPaint); mPaint.setColor(Color.RED); // 设置线的宽度 mPaint.setStrokeWidth(3); // 划线 canvas.drawLine(5, 110, 315, 110, mPaint); } mGameView2.DrawShape(canvas); } public class GameView2 extends View { // 声明ShapeDrawable对象 ShapeDrawable mShapeDrawable = null; public GameView2(Context context) { super(context); } public void DrawShape(Canvas canvas) { // 实例化ShapeDrawable对象并说明是绘制一个矩形 mShapeDrawable = new ShapeDrawable(new RectShape()); // 得到画笔Paint对象并设置其颜色 mShapeDrawable.getPaint().setColor(Color.RED); Rect bounds = new Rect(15, 250, 55, 280); // 设置图像显示区域 mShapeDrawable.setBounds(bounds); // 绘制图像 mShapeDrawable.draw(canvas); /* =========================== */ // 绘制椭圆形 mShapeDrawable = new ShapeDrawable(new OvalShape()); // 设置画笔颜色 mShapeDrawable.getPaint().setColor(Color.GREEN); // 设置显示区域 mShapeDrawable.setBounds(70, 250, 150, 280); // 绘制图像 mShapeDrawable.draw(canvas); // 绘制多边形 Path path1 = new Path(); path1.moveTo(150 + 5, 80 + 80 - 50); path1.lineTo(150 + 45, 80 + 80 - 50); path1.lineTo(150 + 30, 80 + 120 - 50); path1.lineTo(150 + 20, 80 + 120 - 50); path1.close(); // 设置宽高 mShapeDrawable = new ShapeDrawable(new PathShape(path1, 150, 150)); mShapeDrawable.getPaint().setColor(Color.BLUE); // 设置显示区域 mShapeDrawable.setBounds(100, 170, 200, 280); // 绘制图像 mShapeDrawable.draw(canvas); } }}
// 矩形Rect rect = new Rect();rect.top = 5;rect.left = 5;rect.bottom = 100;rect.right = 100;canvas.drawRect(rect, mPaint);// 圆形cx,cy为圆心坐标,radius为半径canvas.drawCircle(160, 60, 50, mPaint);// 椭圆RectF rectF = new RectF();rectF.top = 5;rectF.left = 230;rectF.bottom = 100;rectF.right = 400;canvas.drawOval(rectF, mPaint);// 任意多边形Path path = new Path();path.moveTo(5, 120);path.lineTo(100, 150);path.lineTo(130, 250);path.close();canvas.drawPath(path, mPaint);// 划线canvas.drawLine(5, 300, 50, 400, mPaint);// 画点canvas.drawPoint(200, 400, mPaint);
版权声明:本文为博主原创文章,未经博主允许不得转载。