文章大部分数据来源于:https://mrfu.me/2016/02/28/Glide_How_to_Rotate_Images/
十二.异常:调试和错误处理
1、调试
adb shell setprop log.tag.GenericRequest DEBUG
分为五个等级
VERBOSE
DEBUG
INFO
WARN
ERROR
2、错误处理
使用listener(requestListener)
private RequestListener<String, GlideDrawable> requestListener = new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
// todo log exception
Log.e("glide error", e.toString());
// important to return false so the error placeholder can be placed
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
};
十三.自定义转换
为了实践自定义转换,你将需要创建一个新类,它实现了 Transformation 接口。要实现这个方法还是比较复杂的,你必须要有对 Glide 内部架构方面的洞察力才能做的比较棒。如果你只是想要对图片(不是 Gif 和 video)做常规的 bitmap 转换,我们推荐你使用抽象类 BitmapTransformation。它简化了很多的实现,这应该能覆盖 95% 的应用场景啦。
Glide 有两种方式去使用转换。首先是传一个的你的类的实例作为参数给 .transform()。你这里你可以使用任何转换,无论它是否是用于图像还是 Gif。其他选择是使用 .bitmapTransform(),它只能用于 bitmap 的转换。因为我们上面的实现是为 bitmap 设计的,这两者我们都可以用:
1.单个转换
.bitmapTransform(new BlurTransformation(MainActivity.this))
.transform(new BlurTransformation(MainActivity.this))
2.多种转换
transform( new GreyscaleTransformation( context ),
new BlurTransformation( context ) )
//自定义图片转换,不仅仅是格式,大小,透明度等等都可以转换
private RenderScript rs;
public BlurTransformation(Context context) {
super( context );
rs=RenderScript.create(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
Bitmap blurredBitmap = toTransform.copy( Bitmap.Config.ARGB_8888, true );
// Allocate memory for Renderscript to work with
Allocation input = Allocation.createFromBitmap(
rs,
blurredBitmap,
Allocation.MipmapControl.MIPMAP_FULL,
Allocation.USAGE_SHARED
);
Allocation output = Allocation.createTyped(rs, input.getType());
// Load up an instance of the specific script that we want to use.
ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setInput(input);
// Set the blur radius
script.setRadius(10);
// Start the ScriptIntrinisicBlur
script.forEach(output);
// Copy the output to the blurred bitmap
output.copyTo(blurredBitmap);
toTransform.recycle();
return blurredBitmap;
}
@Override
public String getId() {
return "blur";
}
}
十四.用 animate() 自定义动画
animate( animationObject )
1.系统动画,从左到右
2.自定义动画
//自定义动画
ViewPropertyAnimation.Animator animationObject = new ViewPropertyAnimation.Animator() {
@Override
public void animate(View view) {
// if it's a custom view class, cast it here
// then find subviews and do the animations
// here, we just use the entire view for the fade animation
view.setAlpha( 0f );
ObjectAnimator fadeAnim = ObjectAnimator.ofFloat( view, "alpha", 0f, 1f );
fadeAnim.setDuration( 2500 );
fadeAnim.start();
}
};
十五.集成网络栈
OkHttp
dependencies {
// your other dependencies
// ...
// Glide
compile 'com.github.bumptech.glide:glide:3.6.1'
// Glide's OkHttp Integration
compile 'com.github.bumptech.glide:okhttp-integration:1.3.1@aar'
compile 'com.squareup.okhttp:okhttp:2.5.0'
}
Volley
dependencies {
// your other dependencies
// ...
// Glide
compile 'com.github.bumptech.glide:glide:3.6.1'
// Glide's Volley Integration
compile 'com.github.bumptech.glide:volley-integration:1.3.1@aar'
compile 'com.mcxiaoke.volley:library:1.0.8'
}
十六.用Module自定义Glide
Glide module 是一个抽象方法,全局改变 Glide 行为的一个方式。如果你需要访问 GlideBuilder,它要在你要做的地方创建 Glide 实例,这是要做的一种方法。为了定制 Glide,你需要去实现一个GlideModule 接口的公共类。可以声明多个。
public class SimpleGlideModule implements GlideModule {
@Override
public void applyOptions(Context arg0, GlideBuilder arg1) {
// TODO Auto-generated method stub
}
@Override
public void registerComponents(Context arg0, Glide arg1) {
// TODO Auto-generated method stub
}
}
所以你知道要创建一个额外的类去定制 Glide。下一步是要全局的去声明这个类,让 Glide 知道它应该在哪里被加载和使用。Glide 会扫描 AndroidManifest.xml 为 Glide module 的 meta 声明。因此,你必须在 AndroidManifest.xml 的 标签内去声明这个刚刚创建的 Glide module。
在public void applyOptions(Context arg0, GlideBuilder arg1) {
// TODO Auto-generated method stub
}方法中的GlideBuilder有很多核心的方法
setMemoryCache(MemoryCache memoryCache)
.setBitmapPool(BitmapPool bitmapPool)
.setDiskCache(DiskCache.Factory diskCacheFactory)
.setDiskCacheService(ExecutorService service)
.setResizeService(ExecutorService service)
.setDecodeFormat(DecodeFormat decodeFormat)
十八.自定义内存缓存
public class CustomCachingGlideModule implements GlideModule {
@Override public void applyOptions(Context context, GlideBuilder builder) {
MemorySizeCalculator calculator = new MemorySizeCalculator(context);
int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
int defaultBitmapPoolSize = calculator.getBitmapPoolSize();
int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);
builder.setMemoryCache( new LruResourceCache( customMemoryCacheSize );
builder.setBitmapPool( new LruBitmapPool( customBitmapPoolSize );
}
@Override public void registerComponents(Context context, Glide glide) {
// nothing to do here
}
}
2.自定义磁盘缓存
public class CustomCachingGlideModule implements GlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
// set size & external vs. internal
int cacheSize100MegaBytes = 104857600;
builder.setDiskCache(
new InternalCacheDiskCacheFactory(context, cacheSize100MegaBytes)
);
//builder.setDiskCache(
//new ExternalCacheDiskCacheFactory(context, cacheSize100MegaBytes));
}
@Override
public void registerComponents(Context context, Glide glide) {
// nothing to do here
}
}
这两个选项都不让你选一个特点的目录。如果你要让磁盘缓存到指定的目录,你要使用DiskLruCacheFactory:
String downloadDirectoryPath = Environment.getDownloadCacheDirectory().getPath(); builder.setDiskCache( new DiskLruCacheFactory( downloadDirectoryPath, cacheSize100MegaBytes ) );
十九.自定义尺寸从服务器拿到图片
Android 这边计算好了 ImageView 的大小,然后用 链接 URL(就像 ../logo.png?w=400&h=300)做 Glide 请求
二十.使用glide如何旋转图片
为了使它对我们有用,尤其是用在 Glide 中,我们会包裹它作为一个 BitmapTransformation:
public class RotateTransformation extends BitmapTransformation {
private float rotateRotationAngle = 0f;
public RotateTransformation(Context context, float rotateRotationAngle) {
super( context );
this.rotateRotationAngle = rotateRotationAngle;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
Matrix matrix = new Matrix();
matrix.postRotate(rotateRotationAngle);
return Bitmap.createBitmap(toTransform, 0, 0, toTransform.getWidth(), toTransform.getHeight(), matrix, true);
}
@Override
public String getId() {
return "rotate" + rotateRotationAngle;
}
}