简体   繁体   English

如何加载高分辨率图像

[英]How to load high resolution image

I hava a high resolution(1188 * 17617 2.35MB) image and I want to load it in my Android APP. 我有一个高分辨率(1188 * 17617 2.35MB)图像,我想将其加载到我的Android APP中。 If I use WebView, the image can display. 如果使用WebView,则可以显示图像。 But it is indistinct. 但这并不清楚。 I open it with browser in Android OS, it also indistinct. 我在Android OS中使用浏览器打开它,但也不清楚。 If I use ImageView, it just display nothing. 如果我使用ImageView,它什么也不显示。

You can scale down your bitmaps before loading them into the memory if they are too large. 如果位图太大,则可以缩小比例,然后再将其加载到内存中。 You can accomplish that by using BitmapFactory.Options and scale down as per your requirement. 您可以使用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;
   }

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

It deals with everything for you including downloading, caching and most importantly for you downsampling via a resize function. 它为您处理一切,包括下载,缓存,最重要的是通过调整大小功能对您进行下采样。 It also works with a standard ImageView with very little code: 它也可以用很少的代码与标准ImageView一起使用:

Picasso.with(context) .load(url) .resize(50, 50) .centerCrop() .into(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