给定一个图像,我希望能够仅缩放该图像的一部分。比如说,我想扩大图像的一半,这样那一半就占据了整个空间。
这怎么可能?
ImageView fitXY 会起作用吗,因为我认为它仅适用于整个原始图像。
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linearLayout = new LinearLayout(this);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = 640;
int newHeight = 480;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// create the new Bitmap object
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 50, 50, width,
height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
imageView.setImageDrawable(bmd);
imageView.setScaleType(ScaleType.CENTER);
linearLayout.addView(imageView, new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
setContentView(linearLayout);
}
}
仅当在 createBitmap 中,源中第一个像素的 X 和 Y 坐标为 0 时才有效。意思是,我无法获取图像的子集。只能缩放整个图像。但是 createBitmap 旨在对图像进行子集化。
在日志中,当参数不为0时,我得到以下异常: java.lang.IllegalArgumentException: x + width must be <= bitmap.width()
请帮忙