android中,有一个类Martix类,可对图像进行变换,所以我这里记录下如何操作这个类。
Matrix可以对图像进行放大,缩小,旋转,饱和度调整,对比度调整等等。
旋转操作:
Matrix matrix = new Matrix();
1.
matrix.reset();
matrix.setRotate(180); //把图像正向旋转180度,如为负数则逆向旋转
picBitmap= Bitmap.createBitmap(picBitmap,0,0, picBitmap.getWidth(), picBitmap.getHeight(),matrix, true);
2.
martix.reset();
matrix.setRotate(180,picBitmap.getWidth()/2,picBitmap.getHeight()/2); //第二,第三个参数为旋转中心点(x,y)
Bitmap createBmp = Bitmap.createBitmap(picBitmap.getWidth(), picBitmap.getHeight(), picBitmap.getConfig());
Canvas canvas = new Canvas(createBmp);
Paint paint = new Paint();
canvas.drawBitmap(picBitmap, matrix, paint);
以上两种方式都在把图像顺时针绕图像中心旋转180度
放大操作:
Matrix matrix = new Matrix();
matrix.reset();
//第一个参数为x轴方向上的缩放系数,第二个参数为y轴方向上的缩放系数,
matrix.setScale(2,2, picBitmap.getWidth()/2, picBitmap.getHeight()/2); // 带缩放点(x, y)
matrix.setScale(2,2); // 不带缩放点
picBitmap= Bitmap.createBitmap(picBitmap,0,0, picBitmap.getWidth(), picBitmap.getHeight(),matrix, true);
缩小操作:
Matrix matrix = new Matrix();
matrix.reset();
//第一个参数为x轴方向上的缩放系数,第二个参数为y轴方向上的缩放系数,
matrix.setScale(0.5, 0.5, picBitmap.getWidth()/2, picBitmap.getHeight()/2); // 带缩放点(x, y)
matrix.setScale(0.5, 0.5); // 不带缩放点
picBitmap= Bitmap.createBitmap(picBitmap,0,0, picBitmap.getWidth(), picBitmap.getHeight(),matrix, true);
对比度调整操作:
ColorMatrix cm = new ColorMatrix();
float brightness = -25; //亮度
float contrast = 2; //对比度
cm.set(new float[] {
contrast, 0, 0, 0, brightness,
0, contrast, 0, 0, brightness,
0, 0, contrast, 0, brightness,
0, 0, 0, contrast, 0
});
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
Matrix matrix = new Matrix();
Bitmap createBmp = Bitmap.createBitmap(picBitmap.getWidth(), picBitmap.getHeight(), picBitmap.getConfig());
Canvas canvas = new Canvas(createBmp);
canvas.drawBitmap(picBitmap, matrix, paint);
饱和度调整操作:
//设置饱和度 0表示灰度图像 大于1饱和度增加 0-1饱和度减小
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(saturation);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
Matrix matrix = new Matrix();
Bitmap createBmp = Bitmap.createBitmap(picBitmap.getWidth(), picBitmap.getHeight(), picBitmap.getConfig());
Canvas canvas = new Canvas(createBmp);
canvas.drawBitmap(picBitmap, matrix, paint);
-_- @ a little a day