matrix
2015-11-25 21:57
246 查看
本文转自: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
plaincopy
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;
}
}
Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种,每一种变换在Android的API里都提供了set,post和pre三种操作方式,除了translate,其他三种操作都可以指定中心点。
set是直接设置Matrix的值,每次set一次,整个Matrix的数组都会变掉。
post是后乘,当前的矩阵乘以参数给出的矩阵。可以连续多次使用post,来完成所需的整个变换。
pre是前乘,参数给出的矩阵乘以当前的矩阵。所以操作是在当前矩阵的最前面发生的。
旋转、缩放和倾斜都可以围绕一个中心点来进行,如果不指定,默认情况下,是围绕(0,0)点来进行。
[java] view
plaincopy
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;
}
}
相关文章推荐
- Combination Sum
- JSON之生成JSON字符串和解析
- IOS学习——UIView Animation与CATransition
- 学习日志(1)
- Swift中的Reference Type和 Values Type
- Swift -5 面向对象基础(中)
- ubuntu如何完全卸载Java
- 直线生成算法——中点画线法(Middpoint)
- Struts2_Constant标签认识以及查文件源码和api文档[6]
- Socket 通信原理机制
- Android Studio打包.so文件教程
- 几个主流的Java连接池整理 (转) (2012-01-04 23:48:02)转载▼
- 快速上手php:使用PhpStrom部署项目
- ServletContext对象
- 115.Oracle数据库SQL开发之 PLSQL编程——循环
- MapReduce编程实例之自定义分区
- 交叉编译构建环境搭建
- ubuntu环境下安装R
- BZOJ 3916: [Baltic2014]friends( hash )
- Openstack学习历程_1_视频