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

一 ,包模块引入。

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.csdn.net/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();

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章