android中绘图的方法

绘制各种图形、文字使用Canvas类中drawRect、drawText等方法,详细函数列表以及参数说明可以查看sdk

图形的样式由paint参数控制

Paint类也有很多参数设置方法

坐标由Rect和RectF类管理

通过Canvas、Paint和Rect 就可以绘制游戏中需要的大多数基本图形了

需要注意的一些细节

绘制实心矩形,需要设置paint属性:paint.setStyle(Style.FILL); 通过Style的几个枚举值改变绘制样式

以下写的有点乱,随时添加一些记录点, 以后再整理啦~~~~~

1. Rect对象

一个区域对象Rect(left, top, right, bottom) , 是一个左闭右开的区域,即是说使用 Rect.contains(left, top)为true, Rect.contains(right, bottom)为false

2.drawLine方法

drawLine(float startX, float startY, float stopX, float stopY, Paint paint)  也是一个左闭右开的区间,只会绘制到stopX-1,stopY-1

验证方法:

Canvas c = canvas;

paint.setColor(Color. RED );

c .drawLine(x, y, x+ c .getWidth()-1, y, paint);

c .drawLine(x, y+height-1, x+ c .getWidth(), y+height-1, paint);

paint.setColor(Color. BLUE );

c .drawPoint(x+ c .getWidth()-1, y, paint);

绘制的结果是:


说明drawLine是没有绘制到右边最后一个点的

3. drawRect(Rect r, Paint paint)

  当绘制空心矩形时,绘制的是一个左闭右闭的区域

 

验证方法:

rect.set(x, y, x+width, y+height);

paint.setStyle(Style. STROKE );

paint.setColor(Color. BLUE );

c .drawRect(rect, paint);

paint.setColor(Color. RED );

c .drawLine(x, y, x+width, y, paint);

c .drawLine(x, y+height, x+width, y+height, paint);

c .drawLine(x, y, x, y+height, paint);

c .drawLine(x+width, y, x+width, y+height, paint);

 

绘制的结果是:


当绘制实心矩形时,绘制的是一个左闭右开的区域

 

验证方法:

rect.set(x, y, x+width, y+height);

paint.setColor(Color. RED );

c .drawLine(x, y, x+width, y, paint);

c .drawLine(x, y+height, x+width, y+height, paint);

c .drawLine(x, y, x, y+height, paint);

c .drawLine(x+width, y, x+width, y+height, paint);

paint.setStyle(Style. FILL );

paint.setColor(Color. BLUE );

c .drawRect(rect, paint);

 

绘制的结果是:


这个规则跟j2me也是一样的,在j2me里,drawRect长宽会多画出1px。SDK的说明是:

The resulting rectangle will cover an area (width + 1) pixels wide by (height + 1) pixels tall. If either width or height is less than zero, nothing is drawn.

 

例如 drawRect(10,10,100,1)绘制,结果是一个2px高的矩形,用fillRect(10,10,100,1),结果是一个1px高的矩形

 

转自:http://www.cnblogs.com/jacktu/archive/2008/12/18/1357640.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
private float refX; //手指移动时上一刻在X轴上的位置 private float refY; //Y private float initialX; //触摸屏幕时,按下去时的X轴上位置 private float initialY; //Y private float centerX; //画圆时圆心点X轴位置 private float centerY; //Y轴位置 private float circleRadius = 1; //半径 aaaaaa private RectF ovalRect; //用于画椭圆时的矩形类 private Point point; //用于画点的“点”类 private Rect rect; //用于画正方形/矩形的矩形类 private Point[] trianglePoints = new Point[3]; //用于画三角形时存储三角形的三个点 private Point[] points = new Point[3]; private Paint paint; //画笔 private Path path; //图像路径 private Bitmap mBitmap; //用来实现双缓冲的Bitmap private Canvas mCanvas; //用来实现双缓冲的Canvas private DisplayMetrics dm; private int drawType = DrawType.Scrawl; //画图类型 private boolean isClear = false; //onDraw方法用来判断当前是否是清除图像 public DrawView(Context context, AttributeSet attrs) { super(context, attrs); createCanvas(); //设置画笔属性 paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.RED); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(5); paint.setStrokeCap(Paint.Cap.ROUND); //设置笔刷圆形样式 paint.setPathEffect(new CornerPathEffect(20)); //实例“路径”对象 path = new Path(); } @Override public boolean onTouchEvent(MotionEvent event) { float currentX = event.getX(); float currentY = event.getY(); //如果是随意涂鸦 if(drawType == DrawType.Scrawl){ switch(event.getAction()){ case MotionEvent.ACTION_DOWN: //手按下屏幕的时候 paint.setPathEffect(new CornerPathEffect(20)); //设置圆滑效果 path.moveTo(currentX, currentY); //将画笔移动到此,设置路径的第一个点 refX = currentX; refY = currentY; break; case MotionEvent.ACTION_MOVE: //手指在屏幕上移动的时候 path.lineTo(currentX, currentY); //为路径添加一个点 refX = currentX; refY = currentY; break; case MotionEvent.ACTION_UP: //手指提起来的时候 mCanvas.drawPath(path, paint); paint.setPathEffect(new CornerPathEffect(0)); //画完后取消圆滑效果,否则在画矩形等图形的时候边角也会是圆角 path.reset(); //清空Path的任何直线和曲线,让它变成空,以便下次使用 break; } invalidate(); //这个方法请求View进行重绘,所以会调用onDraw方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值