Android 使用Glide加载图片

Glide是一个用来加载图片的工具,使用Glide加载图片有很多好处,比如大像素的图片Glide会自动进行压缩,不需要担心内存溢出的问题。

使用步骤

  • 加载Glide依赖:

最新版可以访问Glide的github主页:https://github.com/bumptech/glide

  • 使用Glide类调用静态with()方法获得一个RequestManager实例,这个方法有以下重载。
    1. public static RequestManager with(@NonNull Context context)
    2. public static RequestManager with(@NonNull Activity activity)
    3. public static RequestManager with(@NonNull View view)
  • 用RequestManager实例调用load()方法加载图片,并获得一个RequestBuilder的实例,这个方法有以下重载。
    1. public RequestBuilder<Drawable> load(@RawRes @DrawableRes @Nullable Integer resourceId) //用这个方法可以直接传入项目中图片的id了
    2. public RequestBuilder<Drawable> load(@Nullable Bitmap bitmap)
    3. public RequestBuilder<Drawable> load(@Nullable Uri uri)
    4. public RequestBuilder<Drawable> load(@Nullable URL url)
    5. public RequestBuilder<Drawable> load(@Nullable File file)
  • 用RequestBuilder的实例调用into()方法为布局中的组件设置图片

public ViewTarget<ImageView, TranscodeType> into(@NonNull ImageView view)

例子

猜你喜欢

转载自blog.csdn.net/Sacredness/article/details/82729014