Android的Matrix利用数学原理实现图片平移、旋转等操作详解
很庆幸自己的线性代数学的还可以,理解Matrix的矩阵变换完全没有问题。Matrix矩阵实现图片的平移旋转等操作涉及到线性代数问题,感兴趣它的具体实现的话可以参考一下上面的博客,非常详细,非常好。如果不想深究也可以简单的理解为BitMap的像素点通过某种方式(Matrix)移动了相应的位置,这样就实现了图片的变换。不管怎样,先学会如何使用它就可以了。
注:我们所使用的Matrix变换必须有一个Bitmap基础,因此,代码中需要先获得Bitmap。
平移变换
//参数:移动到的位置的放大倍数
matrix.postTranslate(mBitmapwidth*2,mBitmapheight*2);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
旋转变换
matrix.postRotate(180);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
缩放变换
//参数:缩放倍数
matrix.postScale(2, 2);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
错切变换
//x轴不变,y轴进行拉伸
matrix.postSkew(0, 1);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
对称(关于x轴对称 倒影)
float[] matrix_value=new float[]{1f,0f,0f,0f,-1f,0f,0f,0f,1f};
matrix.setValues(matrix_value);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
对称(关于Y轴对称 镜像)
float[] matrix_value1=new float[]{-1f,0f,0f,0f,1f,0f,0f,0f,1f};
matrix.setValues(matrix_value1);
matrix.postTranslate(mBitmapwidth*2, mBitmapheight*2);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
关于它的使用我们也是需要自定义一个class继承View,在布局中类名加包名进行添加布局。
//原图加平移
matrix.reset();
canvas.drawBitmap(mbitmap, matrix, mpaintline);
//平移
matrix.reset();
matrix.postTranslate(mBitmapwidth*2,mBitmapheight*2);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
//原图加旋转加平移
matrix.reset();
matrix.postRotate(180);
matrix.postTranslate(mBitmapwidth*3,mBitmapheight* 2);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
matrix.reset();
matrix.postScale(2, 2);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
matrix.reset();
//将原图绘制出来
canvas.drawBitmap(mbitmap, matrix, mpaintline);
matrix.reset();
matrix.postSkew(0, 1);
matrix.postTranslate(mBitmapwidth*3,mBitmapheight*3);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
//对称 关于x轴对称 倒影
matrix.reset();
float[] matrix_value=new float[]{1f,0f,0f,0f,-1f,0f,0f,0f,1f};
matrix.setValues(matrix_value);
matrix.postTranslate(0, mBitmapheight*3);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
2、关于Y轴对称
//对称 关于y轴 镜像
matrix.reset();
float[] matrix_value1=new float[]{-1f,0f,0f,0f,1f,0f,0f,0f,1f};
matrix.setValues(matrix_value1);
matrix.postTranslate(mBitmapwidth*2, mBitmapheight*2);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
public class MyBitmapView extends View{
private int width;
private int height;
private Paint mpaintline;
private Bitmap mbitmap;
private int mBitmapwidth;
private int mBitmapheight;
private Matrix matrix;
public MyBitmapView(Context context) {
super(context);
}
public MyBitmapView(Context context, AttributeSet attrs) {
super(context, attrs);
mpaintline = new Paint();
mpaintline.setColor(Color.RED);
mpaintline.setStrokeWidth(10);
mpaintline.setAntiAlias(true);
mbitmap=BitmapFactory.decodeResource(getResources(), R.drawable.yourimg);
mBitmapwidth=mbitmap.getWidth();
mBitmapheight=mbitmap.getHeight();
Log.d("图片信息","高:"+ mbitmap.getHeight()+"宽:"+mbitmap.getWidth());
matrix=new Matrix();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawBitmap(mbitmap, 20, 20, mpaintline);
//放缩
matrix.reset();
matrix.postScale(2, 2);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
matrix.reset();
canvas.drawBitmap(mbitmap, matrix, mpaintline);
//平移
matrix.reset();
matrix.postTranslate(mBitmapwidth*2,mBitmapheight*2);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
//旋转
matrix.reset();
matrix.postRotate(180);
matrix.postTranslate(mBitmapwidth*3,mBitmapheight* 2);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
//错切
matrix.reset();
matrix.postSkew(0, 1);
matrix.postTranslate(mBitmapwidth*3,mBitmapheight*3);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
//对称 关于x轴对称 倒影
matrix.reset();
float[] matrix_value=new float[]{1f,0f,0f,0f,-1f,0f,0f,0f,1f};
matrix.setValues(matrix_value);
matrix.postTranslate(0, mBitmapheight*3);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
//对称 关于y轴 镜像
matrix.reset();
float[] matrix_value1=new float[]{-1f,0f,0f,0f,1f,0f,0f,0f,1f};
matrix.setValues(matrix_value1);
matrix.postTranslate(mBitmapwidth*2, mBitmapheight*2);
canvas.drawBitmap(mbitmap,matrix, mpaintline);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
}
}