高分辨率图像无法在ImageView Android中显示 [英] High resolution image not display in ImageView Android

查看:46
本文介绍了高分辨率图像无法在ImageView Android中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以全屏模式显示图像,但是每当要显示大图像时,图像视图中都不会显示任何内容.下面的代码尝试调整位图的大小,但空白图像视图的结果相同.

I am displaying an image in full screen mode,but whenever going to display large image then nothing display in the image view. Below code try to resize bitmap but got same result blank imageview.

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;
    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;
}

推荐答案

您可以从两个方法中解决问题方法1

You solve your problem from two mehtods Method 1

调用此方法(功能)

public Bitmap decodeImage(int resourceId) {
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), resourceId, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE = 100; // you are free to modify size as your requirement

        // Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeResource(getResources(), resourceId, o2);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;    
}

在适配器之前将其添加

picture.setImageBitmap((decodeImage(item.drawableId));

代替

 picture.setImageResource(item.drawableId);

方法2

您需要调整图像尺寸.更好的方法是将图像解码为位图,然后将位图设置为ImageView.例如:

You need to adjust your image size. The better way is to decode the image to a bitmap, and set the bitmap to the ImageView. For example:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), item.drawableId, opts);
picture.setImageBitmap (bitmap);

这篇关于高分辨率图像无法在ImageView Android中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆