Glide笔记

183 阅读1分钟

Glide加载图片中遇到的问题,项目中需要加载原图接口并没有返回图片尺寸。Glide提供的方法可以是我们获取到图片的尺寸。这样我们就可以加载原图保证了图片不失真。


Glide.with(this)
        .load(url)
        .asBitmap()
        .placeholder(R.drawable.img)
        .error(R.drawable.img)
        .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
               //计算图片等比宽高
                int height = (int)((double)width/resource.getWidth()*resource.getHeight());
                //打印获取的图片宽高
              System.out.println(resource.getWidth() + "-" + resource.getHeight() + ":" + width +"-"+height);
            
                img.setLayoutParams(new LinearLayout.LayoutParams(width, height));
                img.setImageBitmap(resource);
            }
        });
      
      width是手机屏幕的宽。  

附上大神翻译的Glide文章:http://mrfu.me/2016/02/27/Glide_Getting_Started/