glide 使用简单介绍 和 缓存 监听 变化方法

本文详细介绍Glide图片加载库的使用方法,包括模块引入、基本代码示例、ListView使用、缓存处理及图片变换技巧。适用于Android开发中图片加载需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 ,包模块引入。

as使用

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.9.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}

idea maven使用

<dependency>
  <groupId>com.github.bumptech.glide</groupId>
  <artifactId>glide</artifactId>
  <version>4.9.0</version>
</dependency>
<dependency>
  <groupId>com.github.bumptech.glide</groupId>
  <artifactId>compiler</artifactId>
  <version>4.9.0</version>
  <optional>true</optional>
</dependency>

eclipse 使用下载jar包导入即可

https://download.youkuaiyun.com/download/qq_36355271/11145553

二,代码

1,imageview的使用
// For a simple view: 
@Override public void onCreate(Bundle savedInstanceState) {
  ...
  ImageView imageView = (ImageView) findViewById(R.id.my_image_view);

  Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView);
}

2,listview等列表的使用
// For a simple image list:
@Override public View getView(int position, View recycled, ViewGroup container) {
  final ImageView myImageView;
  if (recycled == null) {
    myImageView = (ImageView) inflater.inflate(R.layout.my_image_view, container, false);
  } else {
    myImageView = (ImageView) recycled;
  }

  String url = myUrls.get(position);

  Glide
    .with(myFragment)
    .load(url)
    .centerCrop()
    .placeholder(R.drawable.loading_spinner)
    .into(myImageView);

  return myImageView;
}

 

3,缓存处理等复杂处理 

 RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.ic_launcher_background)//占位图/默认图片
                .error(R.drawable.error)//加载失败 占位图
                .override(200, 100) //设置加载图片大小  override(Target.SIZE_ORIGINAL)加载原始大小
                .skipMemoryCache(true);//禁用内存缓存
                .diskCacheStrategy(DiskCacheStrategy.NONE);//禁用硬盘缓存  默认自动缓存


        Glide.with(this)//建议传入 activity 或者fragment 对象  可以使图片加载和活动生命周期同步
                .load(url)//图片地址 也可以是gif等动图
                .apply(options)//设置占位图
//                .into(imageView);//显示目标view
                .into(simpleTarget);//图片加载监听

        SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() {

            @Override

            public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {

                imageView.setImageDrawable(resource); //返回图片对象

            }

        };

图片变换 的方法 移动 裁剪等

RequestOptions options = new RequestOptions()

        .transforms(...);

RequestOptions options = new RequestOptions()

        .centerCrop();



RequestOptions options = new RequestOptions()

        .fitCenter();



RequestOptions options = new RequestOptions()

        .circleCrop();

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值