【发布时间】:2015-03-25 17:14:28
【问题描述】:
我正在开发一个应用程序,我需要在其中将位图适合具有特定尺寸的 Imageview(假设 350dpx50dp - 高度*宽度)。
我想做类似这样的事情:http://gyazo.com/d739d03684e46411feb58d66acea1002
我在这里寻找解决方案。我找到了这个用于缩放位图并将其放入 imageview 的代码,但问题是当我将位图添加到他时 imageview 变得更大:
private void scaleImage(Bitmap bitmap, ImageView view)
{
// Get current dimensions AND the desired bounding box
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int bounding = dpToPx(350);
// Determine how much to scale: the dimension requiring less scaling is
// closer to the its side. This way the image always stays inside your
// bounding box AND either x/y axis touches it.
float xScale = ((float) bounding) / width;
float yScale = ((float) bounding) / height;
float scale = (xScale <= yScale) ? xScale : yScale;
// Create a matrix for the scaling and add the scaling data
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
// Create a new bitmap and convert it to a format understood by the ImageView
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
// Apply the scaled bitmap
view.setImageBitmap(scaledBitmap);
}
使用这个代码我可以得到这个:http://gyazo.com/e9871db2130ac33668156fc0cf773594
但这不是我想要的,我想保留 imageview 的尺寸并将位图添加到 imageview 而不修改 imageview 的尺寸并占据所有 imageview 的表面。就像第一张图片一样。
【问题讨论】: