繁体   English   中英

如何加载高分辨率图像

[英]How to load high resolution image

我有一个高分辨率(1188 * 17617 2.35MB)图像,我想将其加载到我的Android APP中。 如果使用WebView,则可以显示图像。 但这并不清楚。 我在Android OS中使用浏览器打开它,但也不清楚。 如果我使用ImageView,它什么也不显示。

如果位图太大,则可以缩小比例,然后再将其加载到内存中。 您可以使用BitmapFactory.Options完成此操作,然后根据需要缩小比例。

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
                int reqHeight) {

       final BitmapFactory.Options options = new BitmapFactory.Options();
       options.inJustDecodeBounds = true;
       BitmapFactory.decodeFile(path, options);

       options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile(path, options);
        return bmp;
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
             int reqWidth, int reqHeight) {

        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                    inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                    inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
     return inSampleSize;
   }

使用Picasso库。 http://square.github.io/picasso/

它为您处理一切,包括下载,缓存,最重要的是通过调整大小功能对您进行下采样。 它也可以用很少的代码与标准ImageView一起使用:

Picasso.with(context).load(url).resize(50,50).centerCrop().into(imageView)

暂无
暂无

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

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