简体   繁体   English

将大图像适合小图像视图,质量与原始图像一样好?

[英]Fit the large image into small imageview with as good quality as the original image?

Hello I am working on one good android app where I need to fit the large bitmap into small size imageview . 您好我正在开发一个很好的Android应用程序,我需要将大位图适合小尺寸的imageview I am using imageview of size 300dp X 300dp to display large image 1024 X 780 我使用imageview大小300dp X 300dp的显示大图1024 X 780

How can I make the large image into small with as good quality as the original image ? 如何将大图像制作成与原始图像一样好的小图像?

Try this : 尝试这个 :

public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.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 resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);
    return resizedBitmap;

}

Easy to use : 易于使用 :

Bitmap bmResized=getResizedBitmap(yourBitmap,newHeight,newWidth);

And you get Your Resized Image 而且你得到了你的大小调整图像

Note : if you want to resize Resources Image Use this to convert to Bitmap : 注意:如果要调整参考资源图像使用此参数转换为位图:

Bitmap bmOrginal=BitmapFactory.decodeResource(this.getResources(), R.drawble.yourRes);
Bitmap bmResized=getResizedBitmap(bmOrginal,newHeight,newWidth);

Then set the Resized Image : 然后设置调整大小的图像:

image.setImageBitmap(bmResized);

你试过im.setScaleType(ScaleType.FIT_XY)

@Nikola comment answered to my question. @Nikola评论回答了我的问题。 I am writing this in the answer so that it would good for future readers 我在答案中写这篇文章,这对未来的读者有好处

You should make a Thumbnail image of size 300x300 but preserving its aspect ratio. 您应该制作尺寸为300x300的缩略图图像,但保留其纵横比。 You can draw larger images on a canvas centered with some color filling the rest of the space. 您可以在画布上绘制较大的图像,其中一些颜色填充其余空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM