本文转自:http://blog.csdn.net/zyting_love/article/details/7268461
Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种,每一种变换在Android的API里都提供了set,post和pre三种操作方式,除了translate,其他三种操作都可以指定中心点。
set是直接设置Matrix的值,每次set一次,整个Matrix的数组都会变掉。
post是后乘,当前的矩阵乘以参数给出的矩阵。可以连续多次使用post,来完成所需的整个变换。
pre是前乘,参数给出的矩阵乘以当前的矩阵。所以操作是在当前矩阵的最前面发生的。
旋转、缩放和倾斜都可以围绕一个中心点来进行,如果不指定,默认情况下,是围绕(0,0)点来进行。
- package cn.demo.matrix;
- import android.graphics.Bitmap;
- import android.graphics.Matrix;
- import android.graphics.Point;
- /**
- * 图片的矩阵操作
- *
- * @author Administrator
- *
- */
- public class MatrixOptions {
- private Matrix matrix;
- // private Bitmap bitmap;
- private Point currentPoint;
- private Point nextPoint;
- private int width;
- private int height;
- private int showingWidth;
- private int showingHeight;
- public MatrixOptions(Bitmap bitmap) {
- // this.bitmap = bitmap;
- matrix = new Matrix();
- currentPoint = new Point();
- nextPoint = new Point();
- width = bitmap.getWidth();
- height = bitmap.getHeight();
- showingHeight = height;
- showingWidth = width;
- }
- /**
- * 根据偏移量移动图片
- *
- * @param xOffset
- * @param yOffset
- */
- public void move(float xOffset, float yOffset) {
- matrix.postTranslate(xOffset, yOffset);
- currentPoint.x += xOffset;
- currentPoint.y += yOffset;
- }
- /**
- * 移动图片
- */
- public void move() {
- float xOffset = nextPoint.x - currentPoint.x;
- float yOffset = nextPoint.y - currentPoint.y;
- matrix.postTranslate(xOffset, yOffset);
- currentPoint = nextPoint;
- }
- /**
- * 移动图片到(x,y)
- *
- * @param x
- * @param y
- */
- public void moveTo(int x, int y) {
- nextPoint.set(x, y);
- move();
- }
- /**
- * 将(x,y)设为图片中心,并据此移动图片
- *
- * @param x
- * @param y
- */
- public void centerTo(int x, int y) {
- nextPoint.set(x - showingWidth / 2, y - showingHeight / 2);
- move();
- }
- /**
- * 重设图片宽高
- *
- * @param w
- * @param h
- */
- public void resize(int w, int h) {
- float sx = (float) w / (float) showingWidth;
- float sy = (float) h / (float) showingHeight;
- Point center = getCenter();
- matrix.postScale(sx, sy, center.x, center.y);
- showingHeight = h;
- showingWidth = w;
- }
- /**
- * 将图片进行缩放,以图片顶点为中心
- *
- * @param scaleX
- * x轴缩放比例
- * @param scaleY
- * y轴缩放比例
- */
- public void scale(float scaleX, float scaleY) {
- scale(scaleX, scaleY, currentPoint.x, currentPoint.y);
- }
- /**
- * 将图片进行缩放,以图片中心为中心
- *
- * @param scaleX
- * x轴缩放比例
- * @param scaleY
- * y轴缩放比例
- */
- public void scaleC(float scaleX, float scaleY) {
- Point center = getCenter();
- scale(scaleX, scaleY, center.x, center.y);
- }
- /**
- * 将图片进行缩放,以(x,y)为中心
- *
- * @param scaleX
- * x轴缩放比例
- * @param scaleY
- * y轴缩放比例
- * @param x
- * @param y
- */
- public void scale(float scaleX, float scaleY, float x, float y) {
- matrix.postScale(scaleX, scaleY, x, y);
- showingWidth = (int) (scaleX * showingWidth);
- showingHeight = (int) (scaleY * showingHeight);
- }
- /**
- * 获得显示图片的中心点坐标
- *
- * @return
- */
- public Point getCenter() {
- return new Point(currentPoint.x + showingWidth / 2, currentPoint.y
- + showingHeight / 2);
- }
- /**
- * 将图片进行缩放,以图片顶点为中心
- *
- * @param scale
- * 缩放比例
- */
- public void scale(float scale) {
- scale(scale, scale);
- }
- /**
- * 以图片中心为中心旋转图片
- *
- * @param degrees
- */
- public void rotate(float degrees) {
- Point center = getCenter();
- matrix.postRotate(degrees, center.x, center.y);
- }
- /**
- * 重置Matrix
- */
- public void reset() {
- matrix.reset();
- }
- public int getShowingWidth() {
- return showingWidth;
- }
- public void setShowingWidth(int showingWidth) {
- this.showingWidth = showingWidth;
- }
- public int getShowingHeight() {
- return showingHeight;
- }
- public void setShowingHeight(int showingHeight) {
- this.showingHeight = showingHeight;
- }
- public Matrix getMatrix() {
- return matrix;
- }
- public int getHeight() {
- return height;
- }
- public void setPosition(int x, int y) {
- nextPoint.x = x;
- nextPoint.y = y;
- }
- public int getX() {
- return currentPoint.x;
- }
- public int getY() {
- return currentPoint.y;
- }
- }