android 绘图方法
来源:互联网 发布:js设置表单action 编辑:程序博客网 时间:2024/05/01 22:30
绘制各种图形、文字使用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高的矩形
图形的样式由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高的矩形
阅读全文
0 0
- Android Native 绘图方法
- android 绘图方法
- android中绘图的方法
- android中绘图的方法
- android绘图常用方法归纳
- android简单的GDI绘图方法
- Android Native code 的绘图方法2
- Android OpenGL ES 绘图方法参数解析
- android绘图canvas.clipRect()方法的作用
- android绘图canvas.drawBitmap方法的作用
- 整理Android绘图时图形变换方法
- Android中Canvas绘图方法的实现
- android绘图
- android:绘图
- Android绘图
- android 绘图
- android:绘图
- Android 绘图
- Controller 方法的返回值
- java 判断中文是否乱码
- 蓝桥杯-今有7对数字
- qt从5.8.0降到5.5.1或者5.4.2-----给自己mark一下
- 安卓video层级怎么解决
- android 绘图方法
- 全排模板-java
- Recyclerview列表,item嵌套横向Gridview 解决滑动冲突,数据加载问题
- LINQ查询返回DataTable类型
- WindowManager.LayoutParams(2)
- 使用python及百度API对百度poi数据进行爬取(二)
- Android开发笔记(一百四十七)标签布局TabLayout
- Jmeter负载测试之固定吞吐量
- IP问问:高精准(街道级别)IP地址定位技术的特点及应用领域