matrix

本文转自: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)点来进行。


[java]  view plain copy
  1. package cn.demo.matrix;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Matrix;  
  5. import android.graphics.Point;  
  6.   
  7. /** 
  8.  * 图片的矩阵操作 
  9.  *  
  10.  * @author Administrator 
  11.  *  
  12.  */  
  13. public class MatrixOptions {  
  14.     private Matrix matrix;  
  15.     // private Bitmap bitmap;  
  16.     private Point currentPoint;  
  17.     private Point nextPoint;  
  18.     private int width;  
  19.     private int height;  
  20.     private int showingWidth;  
  21.     private int showingHeight;  
  22.   
  23.     public MatrixOptions(Bitmap bitmap) {  
  24.         // this.bitmap = bitmap;  
  25.   
  26.         matrix = new Matrix();  
  27.         currentPoint = new Point();  
  28.         nextPoint = new Point();  
  29.   
  30.         width = bitmap.getWidth();  
  31.         height = bitmap.getHeight();  
  32.         showingHeight = height;  
  33.         showingWidth = width;  
  34.     }  
  35.   
  36.     /** 
  37.      * 根据偏移量移动图片 
  38.      *  
  39.      * @param xOffset 
  40.      * @param yOffset 
  41.      */  
  42.     public void move(float xOffset, float yOffset) {  
  43.         matrix.postTranslate(xOffset, yOffset);  
  44.         currentPoint.x += xOffset;  
  45.         currentPoint.y += yOffset;  
  46.     }  
  47.   
  48.     /** 
  49.      * 移动图片 
  50.      */  
  51.     public void move() {  
  52.         float xOffset = nextPoint.x - currentPoint.x;  
  53.         float yOffset = nextPoint.y - currentPoint.y;  
  54.         matrix.postTranslate(xOffset, yOffset);  
  55.         currentPoint = nextPoint;  
  56.     }  
  57.   
  58.     /** 
  59.      * 移动图片到(x,y) 
  60.      *  
  61.      * @param x 
  62.      * @param y 
  63.      */  
  64.     public void moveTo(int x, int y) {  
  65.         nextPoint.set(x, y);  
  66.         move();  
  67.     }  
  68.   
  69.     /** 
  70.      * 将(x,y)设为图片中心,并据此移动图片 
  71.      *  
  72.      * @param x 
  73.      * @param y 
  74.      */  
  75.     public void centerTo(int x, int y) {  
  76.         nextPoint.set(x - showingWidth / 2, y - showingHeight / 2);  
  77.         move();  
  78.     }  
  79.   
  80.     /** 
  81.      * 重设图片宽高 
  82.      *  
  83.      * @param w 
  84.      * @param h 
  85.      */  
  86.     public void resize(int w, int h) {  
  87.         float sx = (float) w / (float) showingWidth;  
  88.         float sy = (float) h / (float) showingHeight;  
  89.         Point center = getCenter();  
  90.         matrix.postScale(sx, sy, center.x, center.y);  
  91.         showingHeight = h;  
  92.         showingWidth = w;  
  93.     }  
  94.   
  95.     /** 
  96.      * 将图片进行缩放,以图片顶点为中心 
  97.      *  
  98.      * @param scaleX 
  99.      *            x轴缩放比例 
  100.      * @param scaleY 
  101.      *            y轴缩放比例 
  102.      */  
  103.     public void scale(float scaleX, float scaleY) {  
  104.         scale(scaleX, scaleY, currentPoint.x, currentPoint.y);  
  105.     }  
  106.   
  107.     /** 
  108.      * 将图片进行缩放,以图片中心为中心 
  109.      *  
  110.      * @param scaleX 
  111.      *            x轴缩放比例 
  112.      * @param scaleY 
  113.      *            y轴缩放比例 
  114.      */  
  115.     public void scaleC(float scaleX, float scaleY) {  
  116.         Point center = getCenter();  
  117.         scale(scaleX, scaleY, center.x, center.y);  
  118.     }  
  119.   
  120.     /** 
  121.      * 将图片进行缩放,以(x,y)为中心 
  122.      *  
  123.      * @param scaleX 
  124.      *            x轴缩放比例 
  125.      * @param scaleY 
  126.      *            y轴缩放比例 
  127.      * @param x 
  128.      * @param y 
  129.      */  
  130.     public void scale(float scaleX, float scaleY, float x, float y) {  
  131.         matrix.postScale(scaleX, scaleY, x, y);  
  132.         showingWidth = (int) (scaleX * showingWidth);  
  133.         showingHeight = (int) (scaleY * showingHeight);  
  134.     }  
  135.   
  136.     /** 
  137.      * 获得显示图片的中心点坐标 
  138.      *  
  139.      * @return 
  140.      */  
  141.     public Point getCenter() {  
  142.         return new Point(currentPoint.x + showingWidth / 2, currentPoint.y  
  143.                 + showingHeight / 2);  
  144.     }  
  145.   
  146.     /** 
  147.      * 将图片进行缩放,以图片顶点为中心 
  148.      *  
  149.      * @param scale 
  150.      *            缩放比例 
  151.      */  
  152.     public void scale(float scale) {  
  153.         scale(scale, scale);  
  154.     }  
  155.   
  156.     /** 
  157.      * 以图片中心为中心旋转图片 
  158.      *  
  159.      * @param degrees 
  160.      */  
  161.     public void rotate(float degrees) {  
  162.         Point center = getCenter();  
  163.         matrix.postRotate(degrees, center.x, center.y);  
  164.     }  
  165.   
  166.     /** 
  167.      * 重置Matrix 
  168.      */  
  169.     public void reset() {  
  170.         matrix.reset();  
  171.     }  
  172.   
  173.     public int getShowingWidth() {  
  174.         return showingWidth;  
  175.     }  
  176.   
  177.     public void setShowingWidth(int showingWidth) {  
  178.         this.showingWidth = showingWidth;  
  179.     }  
  180.   
  181.     public int getShowingHeight() {  
  182.         return showingHeight;  
  183.     }  
  184.   
  185.     public void setShowingHeight(int showingHeight) {  
  186.         this.showingHeight = showingHeight;  
  187.     }  
  188.   
  189.     public Matrix getMatrix() {  
  190.         return matrix;  
  191.     }  
  192.   
  193.     public int getHeight() {  
  194.         return height;  
  195.     }  
  196.   
  197.     public void setPosition(int x, int y) {  
  198.         nextPoint.x = x;  
  199.         nextPoint.y = y;  
  200.     }  
  201.   
  202.     public int getX() {  
  203.         return currentPoint.x;  
  204.     }  
  205.   
  206.     public int getY() {  
  207.         return currentPoint.y;  
  208.     }  
  209. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值