Android中最重要的一个类当属Canvas了,所有View在UI上绘制全部是由Canvas实现的。而在使用SurfaceView的时候,对Canvas的使用就更重要了。
这里简单介绍下Canvas中常用的方法:
一. save(),restore() 保存和还原
这两个方法并不是用来对图形进行处理的(移动,旋转,缩放)。但却是绘制复杂图形必不可少的方法。
save();
方法作用:用来保存canvas状态的。
restore();
方法作用:用来恢复canvas状态的,还原到上一次savie()时的状态。
save()时可以得到一个返回值,是用来确定保存时间的。使用restoreToCount()方法时可以把此值当参数传递,可以还原到指定的保存时间。
二. translate() 位移
translate(float dx, float dy);
方法作用:移动canvas和它的原点到不同的位置上。默认原点坐标为(0,0)
参数:
dx,左右偏移量(正数是向右移动)
dy,上下偏移量(正数是向下移动)
三. rotate() 旋转
rotate(float degrees);
方法作用:以原点为中心对canvas旋转。默认原点坐标为(0,0)
参数:
四. scale() 缩放
scale(float sx, float sy);
方法作用:增减图形在canvas中的像素数目,对形状,位图进行缩小或者放大。
参数:
sx,横轴缩放大小
sy,数轴缩放大小
scale(float sx, float sy, float px, float py);
方法作用:增减图形在canvas中的像素数目,对形状,位图进行缩小或者放大。
参数:
sx,横轴缩放大小
sy,数轴缩放大小
px,设置原点的位置(与rotate中的px正好相反,正数是向左移动)
py,设置原点的位置(与rotate中的py正好相反,正数是向上移动)
在测试scale(sx, sy, px, py)方法时,一直在用正数测试,结果发现图形一直没有出现在屏幕上,浪费了好长时间,郁闷。目前还不清楚为什么这个方法中的px和py是这么设置的。
degrees,旋转角度
rotate(float degrees, float px, float py);
方法作用:以px,py为中心对canvas旋转
degrees,旋转角度
px,设置旋转中心的横坐标(正数是向右移动)
py,设置旋转中心的竖坐标(正数是向下移动)
唉,粘的乱七八糟的。要说明是这里为我们重写drawRegion提供了另一套思路。
从大飞博客及论坛技术园前文可以看出,现提到的一种方法就是建立用matrix的scale,rotate方法来创建我们所需图片的翻转格式,然后直接drawBitmap
而这里的Canvas中scale,rotate我们从API中可以看到
//------------------
rotate
public final void rotate(float degrees,
float px,
float py)
Preconcat the current matrix with the specified rotation.
参数:
degrees - The amount to rotate, in degrees
px - The x-coord for the pivot point (unchanged by the rotation)
py - The y-coord for the pivot point (unchanged by the rotation)
//---------------------
其实思路是一致的
先save
然后根据transform来rotate/scale
直接drawBitmap,Canvas restore之后自然就翻转了
然后restore
//-----------------------
m_canvas.save();
...
case TRANS_ROT90:
{
m_canvas.rotate(90, x_dest + height, y_dest);
x_dest += height;
break;
}
...
m_canvas.drawBitmap(m_bitmap, x_dest, y_dest, null);
m_canvas.restore();
这里简单介绍下Canvas中常用的方法:
一. save(),restore() 保存和还原
这两个方法并不是用来对图形进行处理的(移动,旋转,缩放)。但却是绘制复杂图形必不可少的方法。
save();
方法作用:用来保存canvas状态的。
restore();
方法作用:用来恢复canvas状态的,还原到上一次savie()时的状态。
save()时可以得到一个返回值,是用来确定保存时间的。使用restoreToCount()方法时可以把此值当参数传递,可以还原到指定的保存时间。
二. translate() 位移
translate(float dx, float dy);
方法作用:移动canvas和它的原点到不同的位置上。默认原点坐标为(0,0)
参数:
dx,左右偏移量(正数是向右移动)
dy,上下偏移量(正数是向下移动)
三. rotate() 旋转
rotate(float degrees);
方法作用:以原点为中心对canvas旋转。默认原点坐标为(0,0)
参数:
四. scale() 缩放
scale(float sx, float sy);
方法作用:增减图形在canvas中的像素数目,对形状,位图进行缩小或者放大。
参数:
sx,横轴缩放大小
sy,数轴缩放大小
scale(float sx, float sy, float px, float py);
方法作用:增减图形在canvas中的像素数目,对形状,位图进行缩小或者放大。
参数:
sx,横轴缩放大小
sy,数轴缩放大小
px,设置原点的位置(与rotate中的px正好相反,正数是向左移动)
py,设置原点的位置(与rotate中的py正好相反,正数是向上移动)
在测试scale(sx, sy, px, py)方法时,一直在用正数测试,结果发现图形一直没有出现在屏幕上,浪费了好长时间,郁闷。目前还不清楚为什么这个方法中的px和py是这么设置的。
degrees,旋转角度
rotate(float degrees, float px, float py);
方法作用:以px,py为中心对canvas旋转
degrees,旋转角度
px,设置旋转中心的横坐标(正数是向右移动)
py,设置旋转中心的竖坐标(正数是向下移动)
唉,粘的乱七八糟的。要说明是这里为我们重写drawRegion提供了另一套思路。
从大飞博客及论坛技术园前文可以看出,现提到的一种方法就是建立用matrix的scale,rotate方法来创建我们所需图片的翻转格式,然后直接drawBitmap
而这里的Canvas中scale,rotate我们从API中可以看到
//------------------
rotate
public final void rotate(float degrees,
float px,
float py)
Preconcat the current matrix with the specified rotation.
参数:
degrees - The amount to rotate, in degrees
px - The x-coord for the pivot point (unchanged by the rotation)
py - The y-coord for the pivot point (unchanged by the rotation)
//---------------------
其实思路是一致的
先save
然后根据transform来rotate/scale
直接drawBitmap,Canvas restore之后自然就翻转了
然后restore
//-----------------------
m_canvas.save();
...
case TRANS_ROT90:
{
m_canvas.rotate(90, x_dest + height, y_dest);
x_dest += height;
break;
}
...
m_canvas.drawBitmap(m_bitmap, x_dest, y_dest, null);
m_canvas.restore();