Glide3.0新增
1.GIF
Animated GIF decoding - Just use the same Glide.with(...).load(...) call and if the image you load is an animated GIF, Glide will load and display a custom animated drawable containing the gif. For more control you can use Glide.with(context).load(...).asBitmap() to always load a static image, or Glide.with(context).load(...).asGif() to fail unless the image is an animated gif.
2.Local Video
Local video stills - In addition to decoding GIFs, Glide can also now decode stills from videos on your device. Just like with GIFs, the same Glide.with(...).load(...) call will work for any local video Android can decode.
3.ThumbnailSupport
Thumbnail support - You can now load multiple images into the same view at the same time so you can minimize the amount of time your users spend looking at loading spinners without sacrificing quality. To first load a thumbnail at 1/10th the size of your view and then load the full image on top, use: Glide.with(yourFragment).load(yourUrl).thumbnail(0.1f).into(yourView). For more control you can also pass in a completely new request into the .thumbnail() call.
4.LifeCycleIntegration
Lifecycle integration - Requests are now paused automatically in onStop and restarted in onStart. Animated GIFs are also paused in onStop to avoid draining battery in the background. In addition when the device's connectivity status changes, all failed requests are automatically restarted to make sure no request permanently fails because of transient connectivity problems.
5.Transcoding
Transcoding - In addition to decoding resources, Glide's .toBytes() and .transcode() methods allow you to fetch, decode, and transform an image in the background as normal, but also in the same call, transcode the image into some more useful format. For example, to upload the bytes of a 250px by 250px profile photo for a user:
Glide.with(context)
.load(“/user/profile/photo/path”)
.asBitmap()
.toBytes()
.centerCrop()
.into(new SimpleTarget<byte[]>(250, 250) {
@Override
public void onResourceReady(byte[] data, GlideAnimation anim) {
// Post your bytes to a background thread and upload them here.
}
});
6.Animations
- Glide 3.x adds support for cross fades (.crossFade()), and view property animations (.animate(ViewPropertyAnimation.Animator)), in addition to the original view animations introduced in Glide 2.0.
7.OkHttp and Volley Support
- You can now choose to use either OkHttp, or Volley, or Glide's HttpUrlConnection default as your network stack. OkHttp and Volley can be included by adding a dependency on the corresponding integration library and registering the corresponding ModelLoaderFactory. See the ReadMe for more details.
使用
String url = "http://avatar.csdn.net/3/D/3/1_lvwenbo0107.jpg";
Glide
.with(mContext)
.load(url)
// .transform(new GlideRoundTransform(mContext, 28))
.transform(new GlideCircleTransform(mContext))
// .centerCrop() //这个会影响transform
.placeholder(R.drawable.ic_done)
.error(R.drawable.ic_discuss)
.crossFade()
.into(viewHolder.mImageView);
GIF
Glide
.with(mContext)
.load(R.drawable.yellow) .into(viewHolder.mImageView);
圆形图
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);
}
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);
}
}
if (glideRequest == null) {
glideRequest = Glide.with(AppContext.getInstance());}
glideRequest.load("file://" + headvision[position]).transform(new GlideRoundTransform(AppContext.getInstance(), 8))
.placeholder(R.drawable.common_circle_bg)
.crossFade()
.error(R.drawable.common_defalut_photo_loading).into(serviceGridViewHold.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);
}
// 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)
.crossFade()
.into(myImageView);
return myImageView;
}