drawRect:矩形
drawCircle:绘制圆形
drawOval:椭圆
drawPath:任意多边形
drawLine:直线
drawPoint:绘制点
代码示例
第一种
package com.example.examples_05_05;
import android.annotation.SuppressLint;
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.view.View;
@SuppressLint("DrawAllocation")
public class GameView extends View implements Runnable {
Paint mpaint;//声明画笔
public GameView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mpaint=new Paint();//实例化对象
new Thread(this).start();//开启线程
}
public void onDraw(Canvas canvas) {
//调用两次
super.onDraw(canvas);
//设置画布为黑色的背景
canvas.drawColor(Color.BLACK);
//取消锯齿
mpaint.setAntiAlias(true);
mpaint.setStyle(Paint.Style.STROKE);
{
//定义矩形对象
Rect rect=new Rect();
//设置矩形大小
rect.left=5;
rect.right=45;
rect.top=5;
rect.bottom=25;
mpaint.setColor(Color.BLUE);
//绘制矩形
canvas.drawRect(rect, mpaint);
mpaint.setColor(Color.RED);
//绘制矩形
canvas.drawRect(50, 5, 90, 25, mpaint);
mpaint.setColor(Color.YELLOW);
//绘制圆形
canvas.drawCircle(40, 70, 30, mpaint);
//定义椭圆的对象
RectF rectF=new RectF();
//设置椭圆的大小
rectF.left=80;
rectF.right=120;
rectF.top=30;
rectF.bottom=70;
mpaint.setColor(Color.LTGRAY);
//绘制椭圆
canvas.drawOval(rectF, mpaint);
//定义多边形
Path path=new Path();
//设置多边形的点
path.moveTo(150+5, 80-50);
path.lineTo(150+45, 80-50);
path.lineTo(150+30, 120-50);
path.lineTo(150+20, 120-50);
//使这些点构成封闭的多边形
path.close();
mpaint.setColor(Color.GRAY);
//绘制多边形
canvas.drawPath(path,mpaint);
mpaint.setColor(Color.RED);
mpaint.setStrokeWidth(3);
//绘制直线
canvas.drawLine(5, 110, 315, 110, mpaint);
}
}
public void run() {
// TODO Auto-generated method stub
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
Thread.currentThread().interrupt();
}
postInvalidate();
}
}
}
第二种
package com.example.examples_05_05;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.Rect;
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;
public class GameView2 extends View implements Runnable {
//声明一个ShapeDrawable对象
ShapeDrawable mShapeDrawable=null;
public GameView2(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void onDraw(Canvas canvas) {
//实例化ShapeDrawable对象并说明是绘制一个矩形
super.onDraw(canvas);
mShapeDrawable =new ShapeDrawable(new RectShape());
//得到画笔,并设置其颜色
mShapeDrawable.getPaint().setColor(Color.RED);
//设置矩形
Rect bounds=new Rect(5,250,55,280);
//设置图像显示区域
mShapeDrawable.setBounds(bounds);
//在画布上绘制矩形
mShapeDrawable.draw(canvas);
/*============================================================*/
//实例化ShapeDrawable对象绘制椭圆
mShapeDrawable=new ShapeDrawable(new OvalShape());
//得到画笔并设置颜色
mShapeDrawable.getPaint().setColor(Color.GREEN);
//设置显示区域
mShapeDrawable.setBounds(70,250,150,280);
//绘制图像
mShapeDrawable.draw(canvas);
Path path=new Path();
//设置多边形
path.moveTo(155, 110);
path.lineTo(195, 110);
path.lineTo(180, 150);
path.lineTo(170, 150);
//使这些点构造成封闭多边形
path.close();
//PathShape后面两个参数分别是宽和高
mShapeDrawable=new ShapeDrawable(new PathShape(path, 150, 150));
//得到画笔并设置颜色
mShapeDrawable.getPaint().setColor(Color.BLUE);
//设置图像显示区域
mShapeDrawable.setBounds(100, 170, 200, 280);
//绘制图像
mShapeDrawable.draw(canvas);
}
public void run() {
// TODO Auto-generated method stub
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
Thread.currentThread().interrupt();
}
postInvalidate();
}
}
}
全部
package com.example.examples_05_05;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// GameView gameView=new GameView(this);
setContentView(new GameView2(this));
}
}