标签:
在Android中还可以绘制几何图形:
下面我们先来看几个方法:
drawRect:绘制矩形
drawCircle:绘制圆
drawOval:绘制椭圆
drawPath:绘制任意多边形
drawLine:绘制直线
drawPoint:绘制点
下面通过一个实例解释怎么使用这些方法
package com.example.kutuke;
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Path; import android.graphics.Rect; import android.graphics.RectF; import android.util.Log; import android.view.View;
public class MyGameView extends View implements Runnable{ public Paint paint = null; public MyGameView02 mgv02 = null; public MyGameView(Context context){ super(context); paint = new Paint(); mgv02 = new MyGameView02(context); new Thread(this).start(); } public void run(){ while(!Thread.currentThread().isInterrupted()){ try{ Thread.sleep(100); }catch(InterruptedException e){ Thread.currentThread().interrupt(); } postInvalidate(); } } public void onDraw(Canvas canvas){ super.onDraw(canvas); //设置画笔为空心 paint.setStyle(Style.STROKE); canvas.drawColor(Color.BLACK); { //定义矩形对象 Rect rect = new Rect(); rect.left = 10; rect.top = 10; rect.right = 110; rect.bottom = 110; paint.setColor(Color.RED); canvas.drawRect(rect, paint); canvas.drawCircle(250, 60, 50, paint); //定义椭圆对象 RectF rectF = new RectF(); rectF.left = (getWidth()-220); rectF.top = (10); rectF.right = (getWidth()-20); rectF.bottom = (160); paint.setColor(Color.YELLOW); //绘制椭圆 canvas.drawOval(rectF, paint); //绘制多边形 Path path = new Path(); //设置多边形的点 path.moveTo(0, 150); path.lineTo(150, 150); path.lineTo(90, 250); path.lineTo(50, 250); path.close(); //绘制多边形 paint.setColor(Color.BLUE); canvas.drawPath(path, paint); //绘制直线 canvas.drawLine(0, 255, getWidth(), 255, paint); } }
}
标签:
原文地址:http://www.cnblogs.com/Smart-Du/p/4302429.html