Android自定义控件(二 .1)Canvas的操作
2017-03-28 09:52
543 查看
Canvas的操作可以帮助我们更好的画图。
要学习Canvas的用法,可以访问Google官网,https://developer.android.google.cn/reference/android/graphics/Canvas.html
Canvas操作主要有以下几种:
1. translate是坐标系的移动,可以为图形绘制选择一个合适的坐标系。 请注意,位移是基于当前位置移动,而不是每次基于屏幕左上角的(0,0)点移动
![](http://upload-images.jianshu.io/upload_images/3897939-732f82bec52862a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/124)
2.可以看到,缩放提供了来两个方法,这两个方法中前两个参数是相同的分别为x轴和y轴的缩放比例。而第二种方法比前一种多了两个参数,用来控制缩放中心位置的。
缩放比例(sx,sy)取值范围详解:
- 通过缩放效果,可以看出,缩放的中心默认为坐标原点,而缩放中心轴就是坐标轴
![](http://upload-images.jianshu.io/upload_images/3897939-69867117a374ab0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 我们使用第二种方法让缩放中心位置稍微改变一下,如下:
![](http://upload-images.jianshu.io/upload_images/3897939-98ae38d17eabf859.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 当缩放比例为负数的时候会根据缩放中心轴进行翻转,缩放还是在坐标原点
![](http://upload-images.jianshu.io/upload_images/3897939-53eac7401973390a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 如果这个时候在移动一下所放位置呢
![](http://upload-images.jianshu.io/upload_images/3897939-9a245838c109c78c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
PS:和位移(translate)一样,缩放也是可以叠加的。
canvas.scale(0.5f,0.5f);
canvas.scale(0.5f,0.1f);
调用两次缩放则 x轴实际缩放为0.5x0.5=0.25 y轴实际缩放为0.5x0.1=0.05
下面我们利用这一特性制作一个有趣的图形。
![](http://upload-images.jianshu.io/upload_images/3897939-06d2ec9f25f26b1d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 旋转(rotate)
旋转提供了两种方法:
public void rotate (float degrees)
public final void rotate (float degrees, float px, float py)
和缩放一样,第二种方法多出来的两个参数依旧是控制旋转中心点的。
默认的旋转中心依旧是坐标原点:
![](http://upload-images.jianshu.io/upload_images/3897939-6b25e78b6cadca6b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 改变旋转中心位置:
![](http://upload-images.jianshu.io/upload_images/3897939-9ff31f0141c60a30.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
当然,rotate也是可以叠加的,
![](http://upload-images.jianshu.io/upload_images/3897939-e3652f212e6a0a09.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 斜切(skew)
斜切只提供了一种方法:
public void skew (float sx, float sy)
参数含义:
float sx:将画布在x方向上倾斜相应的角度,sx倾斜角度的tan值,
float sy:将画布在y轴方向上倾斜相应的角度,sy为倾斜角度的tan值.
变换后:
X = x + sx * y
Y = y+sy * x
要学习Canvas的用法,可以访问Google官网,https://developer.android.google.cn/reference/android/graphics/Canvas.html
Canvas操作主要有以下几种:
操作 | 方法 | 备注 |
---|---|---|
位移(translate) | translate(float dx, float dy) | Preconcat the current matrix with the specified translation |
缩放(scale) | scale(float sx, float sy)/scale(float sx, float sy, float px, float py) | Preconcat the current matrix with the specified scale. |
旋转(rotate) | rotate(float degrees)/rotate(float degrees, float px, float py) | Preconcat the current matrix with the specified rotation. |
斜切(skew) | skew(float sx, float sy) | Preconcat the current matrix with the specified skew. |
canvas.drawCircle(60,60,50,mPaint2); canvas.translate(200,200); canvas.drawCircle(60,60,50,mPaint2);
![](http://upload-images.jianshu.io/upload_images/3897939-732f82bec52862a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/124)
2.可以看到,缩放提供了来两个方法,这两个方法中前两个参数是相同的分别为x轴和y轴的缩放比例。而第二种方法比前一种多了两个参数,用来控制缩放中心位置的。
缩放比例(sx,sy)取值范围详解:
取值范围(n) | 说明 |
---|---|
[-∞, -1) | 先根据缩放中心放大n倍,再根据中心轴进行翻转 |
-1 | 根据缩放中心轴进行翻转 |
(-1, 0) | 先根据缩放中心缩小到n,再根据中心轴进行翻转 |
0 | 不会显示,若sx为0,则宽度为0,不会显示,sy同理 |
(0, 1) | 根据缩放中心缩小到n |
1 | 没有变化 |
(1, +∞) | 根据缩放中心放大n倍 |
int width=getWidth(); int height=getHeight(); canvas.translate(width/2,height/2); //将坐标移到中心 RectF rectF=new RectF(0,-400,400,0); canvas.drawRect(rectF,mPaint1); canvas.scale(0.5f,0.5f); canvas.drawRect(rectF,mPaint1);
![](http://upload-images.jianshu.io/upload_images/3897939-69867117a374ab0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 我们使用第二种方法让缩放中心位置稍微改变一下,如下:
int width=getWidth(); int height=getHeight(); canvas.translate(width/2,height/2); //将坐标移到中心 RectF rectF=new RectF(0,-400,400,0); canvas.drawRect(rectF,mPaint1); canvas.scale(0.5f,0.5f,200,0); canvas.drawRect(rectF,mPaint1);
![](http://upload-images.jianshu.io/upload_images/3897939-98ae38d17eabf859.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 当缩放比例为负数的时候会根据缩放中心轴进行翻转,缩放还是在坐标原点
int width=getWidth(); int height=getHeight(); canvas.translate(width/2,height/2); //将坐标移到中心 RectF rectF=new RectF(0,-400,400,0); canvas.drawRect(rectF,mPaint1); canvas.scale(-0.5f,-0.5f); canvas.drawRect(rectF,mPaint1);
![](http://upload-images.jianshu.io/upload_images/3897939-53eac7401973390a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 如果这个时候在移动一下所放位置呢
int width=getWidth(); int height=getHeight(); canvas.translate(width/2,height/2); //将坐标移到中心 RectF rectF=new RectF(0,-400,400,0); canvas.drawRect(rectF,mPaint1); canvas.scale(-0.5f,-0.5f,200,0); canvas.drawRect(rectF,mPaint1);
![](http://upload-images.jianshu.io/upload_images/3897939-9a245838c109c78c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
PS:和位移(translate)一样,缩放也是可以叠加的。
canvas.scale(0.5f,0.5f);
canvas.scale(0.5f,0.1f);
调用两次缩放则 x轴实际缩放为0.5x0.5=0.25 y轴实际缩放为0.5x0.1=0.05
下面我们利用这一特性制作一个有趣的图形。
int width=getWidth(); int height=getHeight(); canvas.translate(width/2,height/2); //将坐标移到中心 RectF rectF=new RectF(-400,-400,400,400); canvas.drawRect(rectF,mPaint1); for (int i=0;i<20;i++){ canvas.scale(0.9f,0.9f); canvas.drawRect(rectF,mPaint1); }
![](http://upload-images.jianshu.io/upload_images/3897939-06d2ec9f25f26b1d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 旋转(rotate)
旋转提供了两种方法:
public void rotate (float degrees)
public final void rotate (float degrees, float px, float py)
和缩放一样,第二种方法多出来的两个参数依旧是控制旋转中心点的。
默认的旋转中心依旧是坐标原点:
int width=getWidth(); int height=getHeight(); canvas.translate(width/2,height/2); //将坐标移到中心 RectF rectF=new RectF(0,-400,400,0); canvas.drawRect(rectF,mPaint1); canvas.rotate(180); canvas.drawRect(rectF,mPaint1);
![](http://upload-images.jianshu.io/upload_images/3897939-6b25e78b6cadca6b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 改变旋转中心位置:
int width=getWidth(); int height=getHeight(); canvas.translate(width/2,height/2); //将坐标移到中心 RectF rectF=new RectF(0,-400,400,0); canvas.drawRect(rectF,mPaint1); canvas.rotate(180,200,0); canvas.drawRect(rectF,mPaint1);
![](http://upload-images.jianshu.io/upload_images/3897939-9ff31f0141c60a30.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
当然,rotate也是可以叠加的,
int width=getWidth(); int height=getHeight(); canvas.translate(width/2,height/2); //将坐标移到中心 RectF rectF=new RectF(0,-400,400,0); canvas.drawRect(rectF,mPaint1); canvas.rotate(180); canvas.rotate(50); canvas.drawRect(rectF,mPaint1);
![](http://upload-images.jianshu.io/upload_images/3897939-e3652f212e6a0a09.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)
- 斜切(skew)
斜切只提供了一种方法:
public void skew (float sx, float sy)
参数含义:
float sx:将画布在x方向上倾斜相应的角度,sx倾斜角度的tan值,
float sy:将画布在y轴方向上倾斜相应的角度,sy为倾斜角度的tan值.
变换后:
X = x + sx * y
Y = y+sy * x
int width=getWidth(); int height=getHeight(); canvas.translate(width/2,height/2); //将坐标移到中心 RectF rectF=new RectF(0,0,200,200); canvas.drawRect(rectF,mPaint1); canvas.skew(1,0); //水平斜切45 canvas.drawRect(rectF,mPaint1);
相关文章推荐
- Android 自定义控件 (一) ,柱状图 ,Canvas 绘制 柱状图 ,支持触摸操作
- Android自定义控件_Canvas分析
- Android自定义控件 -Canvas绘制折线图(实现动态报表效果)
- android Graphics(四):canvas变换与操作
- 自定义控件之绘图篇(四):canvas变换与操作
- Android自定义控件:绘图基础Canvas、Paint类
- 自定义控件: android中手势操作图片的平移、缩放、旋转 并保存
- Android自定义View高级(三)-Canvas之画布操作
- android Graphics(四):canvas变换与操作
- android Graphics(四):canvas变换与操作
- Android 自定义控件-Canvas和Paint绘图详解-手把手带你绘制一个时钟.
- android Graphics(四):canvas变换与操作
- Android自定义控件前导基础知识学习(一)——Canvas
- android canvas 操作 test
- Android Graphics(四):canvas变换与操作
- android自定义控件(二)Canvas
- Android应用程序开发之图片操作(一)——Bitmap,surfaceview,imageview,Canvas
- android 自定义控件Canvas用方法
- Android应用程序开发之图片操作(一)——Bitmap,surfaceview,imageview,Canvas
- Android canvas画图操作之切割画布实现方法(clipRect)