【问题标题】:Loading images from URL partially, just like what is implemented in WhatsApp部分从 URL 加载图像,就像在 WhatsApp 中实现的一样
【发布时间】:2013-11-09 19:28:54
【问题描述】:

WhatsApp 开发人员最近改进了图像加载,其中立即加载图像的某些部分(获取图像的尺寸和图像的某些像素),然后在加载整个图像后,将占位符替换为完整图像:

我的问题是,他们是如何实施的?他们是否通过读取图像的标题(元数据)来读取图像的尺寸?图片内容如何?或者他们在服务器端是否有两个版本的图像,一个是低质量的较小版本,它首先加载,一个较大的版本是完整图像?请注意,如果是第二种方法,那么一旦从发送方接收到图像,他们仍然需要在服务器端提取图像的较小版本。还有其他方法吗?

【问题讨论】:

标签: java android image whatsapp image-loading


【解决方案1】:

彩色占位符解决方案还有另一种选择,即显示缩略图(可能只有 100 X 100 像素)作为占位符,直到真实图像,这比只有彩色占位符更酷:)。

我在Zingoo 中做了这件事,并为此做了一个blog post。使用Picasso,你可以这样做:

Transformation blurTransformation = new Transformation() {
    @Override
    public Bitmap transform(Bitmap source) {
        Bitmap blurred = Blur.fastblur(LiveImageView.this.context, source, 10);
        source.recycle();
        return blurred;
    }

    @Override
    public String key() {
        return "blur()";
    }
};

Picasso.with(context)
    .load(thumbUrl) // thumbnail url goes here
    .placeholder(R.drawable.placeholder)
    .resize(imageViewWidth, imageViewHeight)
    .transform(blurTransformation)
    .into(imageView, new Callback() {
        @Override
        public void onSuccess() {
            Picasso.with(context)
                    .load(url) // image url goes here
                    .resize(imageViewWidth, imageViewHeight)
                    .placeholder(imageView.getDrawable())
                    .into(imageView);
        }

        @Override
        public void onError() {
        }
    });

帖子本身的更多详细信息,包括 Blur 类。

【讨论】:

  • 嗨 AbdelHady 我尝试使用您的方法。但它在以下代码中给了我错误 Bitmap blurred = Blur.fastblur(LiveImageView.this.context, source, 10);似乎“Blur”和“fastblur”类应该是您自己的代码,如果您能澄清我们是否会在 picasso 中找到“Blur”或“fastblur”,那就太好了
  • 哦,我的错 :),我完全忘记了。无论如何,我用这个课程更新了我的博客文章,你也可以在这里找到 github.com/MichaelEvans/EtsyBlurExample/blob/master/app/src/…
【解决方案2】:

旧帖子的另一个最新答案。您可以尝试Fresco 库的渐进式 JPEG 流媒体 功能来实现该效果。

基本上,您需要做的就是在创建ImageRequest 时调用.setProgressiveRenderingEnabled(true)。我已将 Fresco 渐进式 JPEG 流式传输的完整示例添加到我在此 answer 中提到的 demo application 中,您可能想尝试一下,看看它是如何工作的。

对于懒惰的人:使用 Fresco 时,创建一个DraweeController,如下所示:

 ImageRequest imgReq = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url))
                    .setProgressiveRenderingEnabled(true)
                    .build();
 DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setImageRequest(imgReq)
                    .setOldController(yourDrawee.getController())
                    .build();
 yourDrawee.setController(controller);

注意:此方法有一些限制,如 docs 中所述。

【讨论】:

    【解决方案3】:

    经过几次实验,我没有下载整个图像就得到了尺寸:

    String address = "image_url";
    URL url = new URL(address);
    URLConnection urlC = url.openConnection();
    urlC.connect();
    InputStream is = urlC.getInputStream();
    for(int i = 0; i < 92; i++) is.read();
    
    nt byte1 = is.read();
    int byte2 = is.read();
    
    int width = (byte1 << 8) + byte2;
    
    for(int i = 0; i < 10; i++) is.read();
    
    byte1 = is.read();
    byte2 = is.read();
    
    int height = (byte1 << 8) + byte2;
    
    System.out.println("width = " + width + " | height = " + height);
    
    is.close();
    

    【讨论】:

      【解决方案4】:

      使用 Glide 库:

      Glide.with(context)
        .load(url)
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .into(imageView);
      

      【讨论】:

        猜你喜欢
        • 2015-08-17
        • 2020-10-11
        • 2014-01-17
        • 1970-01-01
        • 2012-01-12
        • 2011-09-18
        • 2020-10-12
        • 2018-12-08
        • 2015-12-20
        相关资源
        最近更新 更多