使用glide要有v4包
[java] view plain copy
- dependencies {
- compile 'com.github.bumptech.glide:glide:3.5.2'
- compile 'com.android.support:support-v4:22.0.0'
- }
简单基础用法
[java] view plain copy
- Glide.with(mContext).load(url).into(imageViewId);
带缓存
[java] view plain copy- Glide.with(mContext).load(url).diskCacheStrategy(DiskCacheStrategy.ALL).into(im);
- Glide.with( context ).load(imageUrl).skipMemoryCache(true).into(imageViewInternet );//跳过内存缓存
- Glide.with( context ).load(imageUrl).diskCacheStrategy(DiskCacheStrategy.NONE).into( imageViewInternet );//跳过硬盘缓存
- 详解 :
DiskCacheStrategy.NONE
什么都不缓存DiskCacheStrategy.SOURCE
仅仅只缓存原来的全分辨率的图像DiskCacheStrategy.RESULT
仅仅缓存最终的图像,即降低分辨率后的(或者是转换后的)DiskCacheStrategy.ALL
缓存所有版本的图像(默认行为)
获取Bitmap设置CircleImageView :
[java] view plain copy
- Glide.with(mContext)
- .load(url)
- .placeholder(R.drawable.loading_spinner)
- .into(new SimpleTarget<Bitmap>(width, height) {
- @Override
- public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
- // setImageBitmap(bitmap) on CircleImageView
- }
- };
加载圆形图片 :
[java] view plain copy
- //圆形图片
- 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();
- }
- }
- Contact GitHub API Training Shop Blog About
[java] view plain copy
- //圆角图片
- 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);
- }
- }
[java] view plain copy
- Glide.with(this).load("https://www.baidu.com/img/bdlogo.png").transform(new GlideRoundTransform(context)).into(imageView);
- Glide.with(this).load("https://www.baidu.com/img/bdlogo.png").transform(new GlideRoundTransform(context, 10)).into(imageView);
- Glide.with(this).load("https://www.baidu.com/img/bdlogo.png").transform(new GlideCircleTransform(context)).into(imageView);
加上图片未能及时显示时等待时的图片
[java] view plain copy
- Glide.with(context).load(imageUrl).placeholder(R.mipmap.ic_launcher).into(imageView);
[java] view plain copy
- Glide.with(context).load(imageUrl).error(R.mipmap.ic_launcher).into(imageView);
[java] view plain copy
- Glide.with(context).load(imageUrl).priority( Priority.HIGH).into( imageView);
图片的适应和缩放 :
1)使用centerCrop是利用图片图填充ImageView设置的大小,如果ImageView的
Height是match_parent则图片就会被拉伸填充
- Glide.with(context).load(imageUrl).centerCrop().into(imageView);
2)使用fitCenter即缩放图像让图像都测量出来等于或小于
ImageView 的边界范围,该图像将会完全显示,但可能不会填满整个ImageView。
- <span style="font-size:32px;">Glide.with(context).load(imageUrl).fitCenter().into(imageView);</span>
- Glide.with(context).load(imageUrl).asGif().into(imageView);
显示本地视频 :
[java] view plain copy- String filePath = "/storage/emulated/0/Pictures/example_video.mp4";
- Glide.with( context ).load( Uri.fromFile( new File( filePath ) ) ).into(imageViewGifAsBitmap );