Glide :Google推荐的图片加载库,专注于流畅的滚动。
Glide常见用法:
Glide.with(mContext)
.load(path)
.asGif()
.override(300,300)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.placeholder(R.drawable.progressbar)
.thumbnail(1f)
.error(R.drawable.error)
.transform(new MyBitmapTransformation(mContext,10f))
.into(iv);
with:传入对象 Context,Activity,Fragment,Glide与当前传入值的生命周期一致,
在onPause方法,onResume 等方法中可适当控件Glide的状态。
Glide.with(mContext).onStop();
Glide.with(mContext).onDestroy();
Glide.with(mContext).pauseRequests();
Glide.with(mContext).resumeRequests();
load对象: String(文件路径,网络地址),File(文件资源),Integer(资源id);
asGif:表示的gif动画,asBitmap:表示静态图
diskCacheStrategy磁盘缓存策略:
DiskCacheStrategy.RESULT:展示小大的图片缓存
DiskCacheStrategy.ALL; 展示在控件中大小图片尺寸和原图都会缓存
DiskCacheStrategy.NONE:不设置缓存
DiskCacheStrategy.SOURCE:原图缓存
override(300,300) 显示图片的width,height
placeholder(R.drawable.progressbar):目标从加载到展示时的控件的显示状态(多用网络加载动画)
error(R,drawable,error):加载失败时,控件显示的图片。
thumbnail :缩略图显示传入值(0-1f)
transform: 图片圆角或圆形显示(继承 BitmapTransformation可行)
into(iv) 展示的控件
实现 GlideModule接口
自定义缓存目录,缓存大小
同时在AndroidManifest.xml中将GlideModule定义为
<meta-data android:name="com.inthecheesefactory.lab.glidepicasso.GlideConfiguration"
android:value="GlideModule"/>
public class GlideConfiguration implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
//设置图片的显示格式ARGB_8888,如果怕图片太耗内存,可以改为DecodeFormat.PREFER_RGB_565
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
//设置磁盘缓存目录
String downloadDirectoryPath=Environment.getExternalStorageDirectory()
//自定义缓存大小
int cacheSize100MegaBytes = 100000000;
glideBuilder.setDiskCache(
new DiskLruCacheFactory(downloadDirectoryPath, cacheSize100MegaBytes)
);
}
@Override
public void registerComponents(Context context, Glide glide) {
// register ModelLoaders here.
}
}
圆形图片显示:
public class GlideCircleTransform extends BitmapTransformation {
public GlideCircleTransform(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
@Override public String getId() {
return getClass().getName();
}
圆角显示:
public class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
public GlideRoundTransform(Context context) {
this(context, 4);
}
/**
* 通过构造方法来确定圆角的大小
* @param context
* @param dp
*/
public GlideRoundTransform(Context context, int dp) {
super(context);
this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return roundCrop(pool, toTransform);
}
private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
canvas.drawRoundRect(rectF, radius, radius, paint);
return result;
}
@Override
public String getId() {
return getClass().getName() + Math.round(radius);
}
ListView中条目有大量gif显示,在快速滑动时,应显示该gif动画的静态图(第一帧动画)有助于增加用户体验。
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState)
{
//滑动
case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
将gif的静态图显示出来
break;
//停止
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
恢复gif动画
break;
//滚动
case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});