Glide等比例动态加载网络图片

来源:互联网 发布:分数统计软件 编辑:程序博客网 时间:2024/06/06 03:27

Glide等比例动态加载网络图片

方法1

1.1设置ImageView布局

 <ImageView      android:id="@+id/load_image"      android:layout_width="wrap_content"      android:layout_height="wrap_content" />

1.2获取图片宽和高

Glide.with(mContext).asBitmap().load(url)       .into(new SimpleTarget<Bitmap>() {        @Override      public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {               int width = resource.getWidth();               int height = resource.getHeight();             Log.d(TAG, "网络图片--->宽:" + width + " 高:" + height);         }  });

1.3等比例缩放设置ImageView宽和高

  private final int MAX_WIDTH = 1000;//图片的最大宽度  private final int MAX_HEIGHT = 500;//图片的最大高度  //2.等比例缩放,最高和最宽不能超过指定的宽和高  if (width > MAX_WIDTH) {   imageWidth = MAX_WIDTH;   imageHeight = imageWidth * height / width;   } else if (height > MAX_HEIGHT) {   imageHeight = MAX_HEIGHT;   imageWidth = width * imageHeight / height;   } else {   imageWidth = width;   imageHeight = height;   }   LinearLayout.LayoutParams lp =       new LinearLayout.LayoutParams(imageWidth, imageHeight);   load_image.setLayoutParams(lp);   Log.d(TAG, "ImageView--->宽:" + imageWidth + " 高:" + imageHeight);

(1)比较宽是否大于指定的宽度,然后等比例计算出对应的高度
(2)比较高度是否大于指定的高度,然后等比例计算出对应的宽度
(3)如果宽度和高度都在指定的范围内,那么就按照图片的实际大小指定ImageView的宽度

特殊情况:就是指定宽度为屏幕宽度,那么等比例计算出高度,这样夹杂图片回事宽度填充屏幕,高度自适应

if (width > 0 && height > 0) {     imageHeight = imageWidth * height / width;   }

1.4加载网络图片,并且监听加载状态

Glide.with(mContext).asBitmap().load(url)    .listener(new RequestListener<Bitmap>() {      @Override    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {    Log.d(TAG, "图片加载失败-->listener:onException");       return false;      }      @Override    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {     Log.d(TAG, "图片加载成功" + "-->listener:onResourceReady(" + resource.getWidth() + ":" + resource.getHeight() + ")");          return false;     }       })     .apply(new RequestOptions().error(R.mipmap.icon_failure))     .into(load_image);

这里写图片描述

10-31 19:30:39.080 31276-31276/demo.chat.com.imageloaddemo D/MainActivity: 网络图片--->宽:1024 高:76810-31 19:30:39.081 31276-31276/demo.chat.com.imageloaddemo D/MainActivity: ImageView--->宽:1000 高:75010-31 19:30:39.110 31276-31276/demo.chat.com.imageloaddemo D/MainActivity: 图片加载成功-->listener:onResourceReady(1920:1440)


请注意:
使用上面方法加载的长和宽比较大时候,个别的时候显示图片的时候会比较卡,(例如:htc A9,华为系列相关手机(ImageView的高度大于8192的时候),因为加载图片的时候,限制了图片宽和高)
获取了图片的大小,设置ImageView的大小,Glid加载的时候,当ImageView的长和宽的大小很大的时候,导致加载图片会卡或者直接不限图片
(先设置ImageView大小,然后加载图片)

方法2

2.1布局设置ImageView属性

 <ImageView                android:id="@+id/load_image"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:adjustViewBounds="true"                android:maxHeight="200dp"                android:maxWidth="200dp" />

设置了最大的宽度和高度,设置adjustViewBounds属性为true,自适应长和宽.
如果设置了layout_width=”match_parent”,layout_height=”wrap_content”,那么就是指定了宽度,高度为自适应.
同理,相反也会自适应

2.2加载网络图片

  String url = "http://fmn.rrfmn.com/fmn076/20171026/1825/original_DoHe_f0bf0000a4741e84.jpg";  RequestOptions options = new RequestOptions()       .placeholder(R.mipmap.icon_loading)        .error(R.mipmap.icon_failure); Glide.with(mContext).load(url)    // .apply(options)    .into(load_image);

这里写图片描述

这里写图片描述


这种先加载了图片,在ImageView显示,然后再进行等比例缩放ImageView,(先加载图片,然后再等比例缩放ImageView)

总结


当需要知道了动态的获取网络图片的宽和高的话,建议用第一种方法,然后设置合理的最大的宽和高
当只是进行自适应的时候,建议用第二种方法,当宽和高无限制的时候,可以在xml上取消maxWidth和maxHeight属性.
在没有动态的设置ImageView的宽和高时候,如果设置了加载中图片,当加载成功后,会按照加载中图片的尺寸进行显示图片,所以根据实际情况是否设置加载中图片.


源码下载