我正在调整我创建的视图的 bimap 图像的大小。
Bitmap image = imageCreate( getMeasuredWidth(), getMeasuredHeight() );
image = imageResize( image, 62, 62 );
imageSave(image,"test.png");
调整大小发生在我的自定义视图中。
protected Bitmap imageCreate( int width, int height ) {
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(image);
draw(canvas);
return image;
}
protected Bitmap imageResize(Bitmap image, int newWidth, int newHeight) {
int width = image.getWidth();
int height = image.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// RECREATE THE NEW BITMAP
Bitmap resizedImage = Bitmap.createBitmap(image, 0, 0, width, height, matrix, false);
return resizedImage;
}
最后我保存图像:
protected boolean imageSave( Bitmap image, String filename, Context context ) {
try {
FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
image.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
我的问题是,为什么图像质量如此糟糕!???
图像有点像素化。原始图像太棒了。
还有没有更好的方法?