2

GLide加载图片还能这样干——基于Glide4.0完美封装

 2 years ago
source link: http://www.androidchina.net/9087.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

GLide加载图片还能这样干——基于Glide4.0完美封装 – Android开发中文站

你的位置:Android开发中文站 > Android开发 > 开发进阶 > GLide加载图片还能这样干——基于Glide4.0完美封装

一个基于GLide加载图片的封装开源框架。可以监听加载图片时的进度 ,可以设置图片的圆角、边框。可加载成圆形。

来自孙福生一个开源框架。项目地址为:https://github.com/sfsheng0322/GlideImageView

该库是基于Glide V4.0设计的,实现如下特性:

  • 1、通过提供的属性可以设置图片的圆角、边框。
  • 2、可以设置点击触摸图片时的颜色、透明度。
  • 3、一行代码加载来自网络、res、SDCard中的图片,可加载成圆形。
  • 4、可以监听加载图片时的进度。

话不多说,上我们的我们的效果图:

项目预览

加载不同形式的图片:

public void loadLocalImage(@DrawableRes int resId, int placeholderResId) {
            load(resId, requestOptions(placeholderResId));
        }

        public void loadLocalImage(String localPath, int placeholderResId) {
            load(FILE + localPath, requestOptions(placeholderResId));
        }

        public void loadCircleImage(String url, int placeholderResId) {
            load(url, circleRequestOptions(placeholderResId));
        }

        public void loadLocalCircleImage(int resId, int placeholderResId) {
            load(resId, circleRequestOptions(placeholderResId));
        }

        public void loadLocalCircleImage(String localPath, int placeholderResId) {
            load(FILE + localPath, circleRequestOptions(placeholderResId));
        }

加载不同风格的图片

一些关键的类:

  • GlideView 对ImageView进一步的封装
  • GlideImageLoader 对Glide.load的进一步封装
  • CircleProgressView和ShapeImageView 封装加载图片的进度条

CircleProgressView和ShapeImageView是自定义ImageView,其中封装了一些自定义属性
可以在可以在代码中设置图片的一些属性, 当然这些属性也可以在GlideImageView上面设置。

eg:

// 设置边框颜色
public void setBorderColor(@ColorRes int id) {
    this.borderColor = getResources().getColor(id);
    invalidate();
}

// 设置边框宽度
public void setBorderWidth(int borderWidth) {
    this.borderWidth = DisplayUtil.dip2px(getContext(), borderWidth);
    invalidate();
}

// 设置图片按下颜色透明度
public void setPressedAlpha(float pressAlpha) {
    this.pressedAlpha = pressAlpha;
}

// 设置图片按下的颜色
public void setPressedColor(@ColorRes int id) {
    this.pressedColor = getResources().getColor(id);
    pressedPaint.setColor(pressedColor);
    pressedPaint.setAlpha(0);
    invalidate();
}

具体属性如下

Attribute 属性 Description 描述

siv_border_color 边框颜色

siv_border_width 边框宽度

siv_pressed_color 触摸图片时的颜色

siv_pressed_alpha 触摸图片时的颜色透明度: 0.0f – 1.0f

siv_radius 圆角弧度

siv_shape_type 两种形状类型:默认是0:rectangle、1:circle

 image41.load(cat_thumbnail, requestOptions).listener(new OnGlideImageViewListener() {
            @Override
            public void onProgress(int percent, boolean isDone, GlideException exception) {
                if (exception != null && !TextUtils.isEmpty(exception.getMessage())) {
                    Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
                }
                progressView1.setProgress(percent);
                progressView1.setVisibility(isDone ? View.GONE : View.VISIBLE);
            }
        });

加载gif:

加载gif小图片

GLide的缓存策略:

让我们想象一个非常简单的请求,从网络中加载图片到 ImageView。

Glide  
    .with( context )
    .load( eatFoodyImages[0] )
    .skipMemoryCache( true )
    .into( imageViewInternet );

调用了 .skipMemoryCache(true) 去明确告诉 Glide 跳过内存缓存。这意味着 Glide 将不会把这张图片放到内存缓存中去。这里需要明白的是,这只是会影响内存缓存!Glide 将会仍然利用磁盘缓存来避免重复的网络请求

跳过磁盘缓存

图片在这段代码片段中将不会被保存在磁盘缓存中。然而,默认的它将仍然使用内存缓存!为了把这里两者都禁用掉,两个方法一起调用:

Glide  
    .with( context )
    .load( eatFoodyImages[0] )
    .diskCacheStrategy( DiskCacheStrategy.NONE )
    .skipMemoryCache( true )
    .into( imageViewInternet );

自定义磁盘缓存行为

Picasso 仅仅缓存了全尺寸的图像。然而 Glide 缓存了原始图像,全分辨率图像和另外小版本的图像。比如,如果你请求的一个图像是 1000×1000 像素的,但你的 ImageView 是 500×500 像素的,Glide 将会把这两个尺寸都进行缓存。

现在你将会理解对于 .diskCacheStrategy() 方法来说不同的枚举参数的意义:

  • DiskCacheStrategy.NONE 什么都不缓存,就像刚讨论的那样
  • DiskCacheStrategy.SOURCE 仅仅只缓存原来的全分辨率的图像。在我们上面的例子中,将会只有一个 1000×1000 像素的图片
  • DiskCacheStrategy.RESULT 仅仅缓存最终的图像,即,降低分辨率后的(或者是转换后的)
  • DiskCacheStrategy.ALL 缓存所有版本的图像(默认行为)

用 DiskCacheStrategy.SOURCE 去告诉 Glide 仅仅保存原始图片:

Glide  
    .with( context )
    .load( eatFoodyImages[2] )
    .diskCacheStrategy( DiskCacheStrategy.SOURCE )
    .into( imageViewFile );        

在项目中提供了GlideImageLoader类加载图片,比如这样加载图片:先加载缩略图再加载高清图片,并监听加载的进度

private void loadImage(String image_url_thumbnail, String image_url) {
    RequestOptions requestOptions = glideImageView.requestOptions(R.color.black)
            .centerCrop()
            .skipMemoryCache(true) // 跳过内存缓存
            .diskCacheStrategy(DiskCacheStrategy.NONE); // 不缓存到SDCard中

    glideImageView.getImageLoader().setOnGlideImageViewListener(image_url, new OnGlideImageViewListener() {
        @Override
        public void onProgress(int percent, boolean isDone, GlideException exception) {
            progressView.setProgress(percent);
            progressView.setVisibility(isDone ? View.GONE : View.VISIBLE);
        }
    });

    glideImageView.getImageLoader().requestBuilder(image_url, requestOptions)
            .thumbnail(Glide.with(ImageActivity.this) // 加载缩略图
                    .load(image_url_thumbnail)
                    .apply(requestOptions))
            .transition(DrawableTransitionOptions.withCrossFade()) // 动画渐变加载
            .into(glideImageView);
}

加载进度时效果如下:

加载时进度条监听

项目Github链接地址

https://github.com/androidstarjack/MyCircleWaveDiverge

下载慢?CSDN下载链接:

http://download.csdn.net/detail/androidstarjack/9892366

转载请注明:Android开发中文站 » GLide加载图片还能这样干——基于Glide4.0完美封装


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK