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);