[英]Resizing an image to fit on a imageview
I am developing an application in which I need to fit an Bitmap into Imageview with specific dimensions(let's suppose 350dpx50dp - height*width). 我正在开发一个应用程序,在该应用程序中我需要使用特定尺寸(让我们假设350dpx50dp-height * width)将位图安装到Imageview中。
I wanted to do something similar like this: http://gyazo.com/d739d03684e46411feb58d66acea1002 我想做类似的事情: http : //gyazo.com/d739d03684e46411feb58d66acea1002
I have looked here for solutions. 我在这里寻找解决方案。 I found this code for scale the bitmap and fit it into imageview, but the problem is that imageview becomes greater when I add the bitmap into him:
我找到了用于缩放位图并将其适合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);
}
Using this code I can get this : http://gyazo.com/e9871db2130ac33668156fc0cf773594 使用此代码,我可以获得: http : //gyazo.com/e9871db2130ac33668156fc0cf773594
But that's not what I wanted, I want to keep the dimensions of imageview and add the bitmap into imageview without modifying the dimensions of imageview and occupying all the imageview's surface. 但这不是我想要的,我想保留imageview的尺寸并将位图添加到imageview中,而无需修改imageview的尺寸并占用imageview的所有表面。 Like the first image.
像第一张图片。
为什么不只将android:scaleType="fitXY"
到xml的ImageView
中?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.