且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

高分辨率图像无法在ImageView Android中显示

更新时间:2023-01-07 08:09:48

您可以从两个方法中解决问题方法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);