Glide的使用学习
Posted 代码库存
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Glide的使用学习相关的知识,希望对你有一定的参考价值。
MainActivity
1 import android.app.AlertDialog; 2 import android.content.DialogInterface; 3 import android.graphics.Bitmap; 4 import android.graphics.BitmapFactory; 5 import android.graphics.drawable.BitmapDrawable; 6 import android.graphics.drawable.Drawable; 7 import android.net.Uri; 8 import android.os.Bundle; 9 import android.os.Environment; 10 import android.support.annotation.Nullable; 11 import android.support.v7.app.AppCompatActivity; 12 import android.view.View; 13 import android.widget.Button; 14 import android.widget.ImageView; 15 16 import com.bumptech.glide.Glide; 17 import com.bumptech.glide.Priority; 18 import com.bumptech.glide.load.DataSource; 19 import com.bumptech.glide.load.engine.DiskCacheStrategy; 20 import com.bumptech.glide.load.engine.GlideException; 21 import com.bumptech.glide.load.resource.bitmap.RoundedCorners; 22 import com.bumptech.glide.request.RequestListener; 23 import com.bumptech.glide.request.RequestOptions; 24 import com.bumptech.glide.request.target.SimpleTarget; 25 import com.bumptech.glide.request.target.Target; 26 import com.bumptech.glide.request.transition.Transition; 27 import com.example.demonstrate.DemonstrateUtil; 28 29 import java.io.File; 30 31 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 32 33 protected ImageView iv; 34 protected Button btn; 35 protected String item; 36 private String STR_TAG = "--->***"; 37 private String TAG = this.getClass().getSimpleName(); 38 protected String result; 39 40 @Override 41 protected void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 super.setContentView(R.layout.activity_main); 44 45 initView(); 46 } 47 48 private void initView() { 49 iv = (ImageView) findViewById(R.id.iv); 50 btn = (Button) findViewById(R.id.btn); 51 Button btn2 = (Button) findViewById(R.id.btn2); 52 btn.setOnClickListener(MainActivity.this); 53 btn2.setOnClickListener(MainActivity.this); 54 } 55 56 @Override 57 public void onClick(View view) { 58 if (view.getId() == R.id.btn) { 59 show(); 60 } else if (view.getId() == R.id.btn2) { 61 show0(); 62 } 63 } 64 65 private void show0() { 66 String[] items = { 67 "0加载图片来自File", 68 "1加载图片来自资源id", 69 "2加载图片来自uri", 70 "3加载强制转化为gif", 71 "4加载原图的缩略图", 72 "5加载缩略图高级方式", 73 "6加载设置不缓存", 74 "7加载设置优先级", 75 "8加载回调后的图片", 76 "9加载回调后的图片设置尺寸", 77 "10加载图片圆角转换", 78 "11加载图片圆角转换2", 79 }; 80 new AlertDialog.Builder(this) 81 .setTitle("glide操作2") 82 .setItems(items, new DialogInterface.OnClickListener() { 83 @Override 84 public void onClick(DialogInterface dialog, int which) { 85 switch (which) { 86 case 0: 87 selectedA0(); 88 break; 89 case 1: 90 selectedA1(); 91 break; 92 case 2: 93 selectedA2(); 94 break; 95 case 3: 96 selectedA3(); 97 break; 98 case 4: 99 selectedA4(); 100 break; 101 case 5: 102 selectedA5(); 103 break; 104 case 6: 105 selectedA6(); 106 break; 107 case 7: 108 selectedA7(); 109 break; 110 case 8: 111 selectedA8(); 112 break; 113 case 9: 114 selectedA9(); 115 break; 116 case 10: 117 selectedA10(); 118 break; 119 case 11: 120 selectedA11(); 121 break; 122 case 12: 123 break; 124 } 125 } 126 }).create() 127 .show(); 128 } 129 130 private void selectedA11() { 131 //RoundedCorners, 132 //CenterInside 133 Glide.with(this) 134 .load(UC.IMAGE_URL_A11) 135 .apply( 136 new RequestOptions() 137 .override(300) 138 .transform(new RoundedCorners(20)) 139 ) 140 .into(iv); 141 } 142 143 private void selectedA10() { 144 Glide.with(this) 145 .load(UC.IMAGE_URL_A10) 146 .apply(new RequestOptions().transform(new GlideRoundTransform(this))) 147 .into(iv); 148 } 149 150 private void selectedA9() { 151 //强制Glide返回一个Bitmap 152 SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>(300, 300) { 153 @Override 154 public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) { 155 iv.setImageBitmap(resource); 156 } 157 }; 158 Glide.with(this).asBitmap() 159 .load(UC.IMAGE_URL_A9) 160 .into(target 161 ); 162 } 163 164 165 private void selectedA8() { 166 //上面这段代码不要写成匿名内部类的机制,原因就是java的自动垃圾回收机制可能在图片还没有加载好的时候就已经把你的Target回收了 167 //注意.with()里面的参数,Glide的请求是和传进去的Context共存亡的,如果传一个Activity进去,当Activity GG过后,你的请求也就GG了,但是如果这样传:.with(context.getApplicationContext() ).当你的Activity GG过后,请求还是会继续,回调还是会继续。 168 169 170 //强制Glide返回一个Bitmap 171 SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>() { 172 @Override 173 public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) { 174 iv.setImageBitmap(resource); 175 } 176 }; 177 Glide.with(this).asBitmap() 178 .load(UC.IMAGE_URL_A8) 179 .into(target 180 ); 181 } 182 183 private void selectedA7() { 184 Glide.with(this) 185 .load(UC.IMAGE_URL_A7) 186 .apply(new RequestOptions().priority(Priority.LOW)) 187 .into(iv); 188 } 189 190 private void selectedA6() { 191 Glide.with(this) 192 .load(UC.IMAGE_URL_A6) 193 .apply(new RequestOptions().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)) 194 .into(iv); 195 } 196 197 private void selectedA5() { 198 //先加载第一张图片作为缩略图,在加载最终要显示的图片 199 //thumbnail中加载的是缩略图 200 Glide.with(this).load(UC.IMAGE_URL_9) 201 .thumbnail(Glide.with(this).load(UC.IMAGE_URL_A5)).into(iv); 202 } 203 204 private void selectedA4() { 205 //先加载为原图的十分之一,后再加载原图. 206 Glide.with(this) 207 .load(UC.IMAGE_URL_A4) 208 .thumbnail(0.1f) 209 .into(iv); 210 } 211 212 private void selectedA3() { 213 Glide.with(this).asGif().load(UC.GIF_URL_0).into(iv); 214 } 215 216 private void selectedA2() { 217 Glide.with(this) 218 .load(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "d.jpg"))) 219 .into(iv); 220 } 221 222 private void selectedA1() { 223 Glide.with(this) 224 .load(R.mipmap.ic_launcher_round) 225 .into(iv); 226 } 227 228 private void selectedA0() { 229 File file = new File(Environment.getExternalStorageDirectory(), "3.jpg"); 230 Glide.with(this).load(file).into(iv); 231 } 232 233 private void show() { 234 final String[] items = { 235 "0加载图片一般用法", 236 "1加载图片成功监听", 237 "2加载图片失败", 238 "3加载图片ID占位图", 239 "4加载图片Drawable占位图", 240 "5加载图片传入null", 241 "6加载图片后修改宽高", 242 "7加载图片后修改宽高等尺寸", 243 "8加载图片centerCrop", 244 "9加载图片fitCenter", 245 "10加载图片centerInside", 246 "11加载图片circleCrop", 247 "12加载动态图的第一帧", 248 "13加载动态图成功", 249 "14加载本地视频缩略图", 250 251 }; 252 new AlertDialog.Builder(this) 253 .setTitle("glide操作选项") 254 .setItems(items, new DialogInterface.OnClickListener() { 255 @Override 256 public void onClick(DialogInterface dialog, int which) { 257 item = items[which] + STR_TAG; 258 switch (which) { 259 case 0: 260 selected0(); 261 break; 262 case 1: 263 selected1(); 264 break; 265 case 2: 266 selected2(); 267 break; 268 case 3: 269 selected3(); 270 break; 271 case 4: 272 selected4(); 273 break; 274 case 5: 275 selected5(); 276 break; 277 case 6: 278 selected6(); 279 break; 280 case 7: 281 selected7(); 282 break; 283 case 8: 284 selected8(); 285 break; 286 case 9: 287 selected9(); 288 break; 289 case 10: 290 selected10(); 291 break; 292 case 11: 293 selected11(); 294 break; 295 case 12: 296 selected12(); 297 break; 298 case 13: 299 selected13(); 300 break; 301 case 14: 302 selected14(); 303 break; 304 } 305 } 306 }).create() 307 .show(); 308 } 309 310 private void selected14() { 311 //缩略图的加载 312 String name = "v.f40.mp4"; 313 Glide.with(this).load(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), name))) 314 .into(iv); 315 } 316 317 private void selected13() { 318 //GIF加载,URL指向的资源必须是gif,如果是普通图片则不显示。 319 //相反,如果指向正确但没有执行asGif方法,则只是作为普通图片展示 320 Glide.with(this).asGif().load(UC.GIF_URL_0).into(iv); 321 } 322 323 private void selected12() { 324 //可理解为加载动态图的第一帧的Bitmap,比如Gif 325 Glide.with(this).asBitmap(). 326 load(UC.GIF_URL_0).into(iv); 327 } 328 329 private void selected11() { 330 Glide.with(this) 331 .load(UC.IMAGE_URL_11) 332 .apply(new RequestOptions().circleCrop()) 333 .into(iv); 334 } 335 336 private void selected10() { 337 Glide.with(this) 338 .load(UC.IMAGE_URL_10) 339 .apply(new RequestOptions().centerInside()) 340 .into(iv); 341 } 342 343 private void selected9() { 344 Glide.with(this) 345 .load(UC.IMAGE_URL_8) 346 .apply(new RequestOptions().fitCenter()) 347 .into(iv); 348 } 349 350 private void selected8() { 351 RequestOptions options = new RequestOptions() 352 .centerCrop(); 353 Glide.with(this) 354 .load(UC.IMAGE_URL_9) 355 .apply(options) 356 .into(iv); 357 } 358 359 private void selected7() { 360 RequestOptions options = new RequestOptions().override(300).fitCenter(); 361 Glide.with(this) 362 .load(UC.IMAGE_URL_1) 363 .apply(options) 364 .into(iv); 365 } 366 367 private void selected6() { 368 RequestOptions options = new RequestOptions().override(400, 400).fitCenter(); 369 Glide.with(this).load(UC.IMAGE_URL_1) 370 .apply(options) 371 .into(iv); 372 } 373 374 private void selected5() { 375 RequestOptions options = new RequestOptions() 376 .fallback(R.mipmap.ic_launcher) 377 .diskCacheStrategy(DiskCacheStrategy.NONE); 378 Glide.with(this) 379 .load(null) 380 .apply(options) 381 .into(iv); 382 } 383 384 private void selected4() { 385 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.load); 386 Drawable drawable = new BitmapDrawable(getResources(), bitmap); 387 RequestOptions options = new RequestOptions().placeholder(drawable); 388 Glide.with(this) 389 .load(UC.IMAGE_URL_ERROR) 390 .apply(options) 391 .into(iv); 392 } 393 394 private void selected3() { 395 RequestOptions options = new RequestOptions() 396 .placeholder(R.mipmap.load) 397 .error(R.mipmap.fail) 398 .fallback(R.mipmap.ic_launcher) 399 .diskCacheStrategy(DiskCacheStrategy.NONE); 400 Glide.with(this) 401 .load(UC.IMAGE_URL_ERROR) 402 .apply(options) 403 .into(iv); 404 } 405 406 private void selected2() { 407 Glide.with(this) 408 .load(UC.IMAGE_URL_ERROR) 409 .listener(new RequestListener<Drawable>() { 410 @Override 411 public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { 412 DemonstrateUtil.showToastResult(MainActivity.this, "onLoadFailed失败"); 413 return false; 414 } 415 416 @Override 417 public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { 418 DemonstrateUtil.showToastResult(MainActivity.this, "onResourceReady成功"); 419 return false; 420 } 421 }).into(iv); 422 } 423 424 private void selected1() { 425 426 Glide.with(this) 427 .load(UC.IMAGE_URL_1) 428 .listener(new RequestListener<Drawable>() { 429 @Override 430 public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { 431 DemonstrateUtil.showToastResult(MainActivity.this, "onLoadFailed加载失败!"); 432 return false; 433 } 434 435 @Override 436 public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { 437 DemonstrateUtil.showToastResult(MainActivity.this, "onResourceReady加载成功!"); 438 return false; 439 } 440 }) 441 .into(iv); 442 } 443 444 private void selected0() { 445 //默认使用原图加载,内存缓存,磁盘缓存, 446 Glide.with(this).load(UC.IMAGE_URL_0).into(iv); 447 } 448 449 450 }
实现圆角效果类
1 import android.content.Context; 2 import android.content.res.Resources; 3 import android.graphics.Bitmap; 4 import android.graphics.BitmapShader; 5 import android.graphics.Canvas; 6 import android.graphics.Paint; 7 import android.graphics.RectF; 8 9 import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; 10 import com.bumptech.glide.load.resource.bitmap.BitmapTransformation; 11 12 import java.security.MessageDigest; 13 14 15 /** 16 *实现圆角效果. 17 */ 18 19 public class GlideRoundTransform extends BitmapTransformation { 20 private float radius = 0f; 21 22 public GlideRoundTransform(Context context) { 23 this(context, 20); 24 } 25 26 public GlideRoundTransform(Context context, int dp) { 27 super(context); 28 //density,屏幕密度.1.5,1,0.75 29 this.radius = Resources.getSystem().getDisplayMetrics().density * dp; 30 } 31 32 @Override 33 protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { 34 return roundCrop(pool, toTransform); 35 } 36 37 private Bitmap roundCrop(BitmapPool pool, Bitmap source) { 38 if (source == null) return null; 39 //Bitmap.Config.ARGB_8888,图片质量. 40 Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); 41 42 if (result == null) { 43 result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); 44 } 45 Canvas canvas = new Canvas(result); 46 Paint paint = new Paint(); 47 //REPEAT,MIRROR,CLAMP{重复,镜像,拉伸} 48 //设置着色器 49 paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); 50 //设置抗锯齿. 51 paint.setAntiAlias(true); 52 //矩阵,(float left, float top, float right, float bottom) 53 //矩形的左边的x坐标,矩形的顶部的Y坐标,对右侧的矩形的x坐标,矩形的底部的Y坐标 54 RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); 55 //画指定的圆矩形。 56 //drawRoundRect(RectF rect, float rx, float ry, @NonNull Paint paint) 57 //矩阵,圆角,椭圆的x半径,y半径.画笔, 58 canvas.drawRoundRect(rectF, radius, radius, paint); 59 return result; 60 61 } 62 63 @Override 64 public void updateDiskCacheKey(MessageDigest messageDigest) { 65 66 } 67 }
MyTransformation设置Bitmap宽高
1 import android.content.Context; 2 import android.graphics.Bitmap; 3 4 import com.bumptech.glide.Glide; 5 import com.bumptech.glide.load.Transformation; 6 import com.bumptech.glide.load.engine.Resource; 7 import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; 8 import com.bumptech.glide.load.resource.bitmap.BitmapResource; 9 10 import java.security.MessageDigest; 11 12 /** 13 * Created by think on 2017/10/25. 14 */ 15 16 public class MyTransformation implements Transformation<Bitmap> { 17 18 private BitmapPool mBitmapPool; 19 private int mWidth; 20 private int mHeight; 21 22 public MyTransformation(Context context) { 23 this(Glide.get(context).getBitmapPool()); 24 } 25 26 public MyTransformation(BitmapPool pool) { 27 this.mBitmapPool = pool; 28 } 29 30 @Override 31 public Resource<Bitmap> transform(Context context, Resource<Bitmap> resource, int outWidth, int outHeight) { 32 Bitmap source = resource.get(); 33 int size = Math.min(source.getWidth(), source.getHeight()); 34 35 mWidth = (source.getWidth() - size) / 2; 36 mHeight = (source.getHeight() - size) / 2; 37 38 Bitmap.Config config = 39 source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888; 40 Bitmap bitmap = mBitmapPool.get(mWidth, mHeight, config); 41 if (bitmap == null) { 42 bitmap = Bitmap.createBitmap(source, mWidth, mHeight, size, size); 43 } 44 45 return BitmapResource.obtain(bitmap, mBitmapPool); 46 } 47 48 @Override 49 public void updateDiskCacheKey(MessageDigest messageDigest) { 50 51 } 52 }
自定义网络图片
1 public interface UC { 2 String IMAGE_URL_0 = "http://p0.so.qhmsg.com/t010b9ba7c1e568dd3f.jpg"; 3 String IMAGE_URL_1 = "http://img3.a0bi.com/upload/ttq/20160621/1466508726953.jpg"; 4 String IMAGE_URL_8= "http://www.285868.com/uploadfile/2016/1027/20161027102235543.jpg"; 5 String IMAGE_URL_ERROR = "http://p0.so.qhmsg.com/t010b9ba7c1e568dd3f66.jpg"; 6 String IMAGE_URL_9 = "http://img2.3lian.com/2014/f3/51/d/2.jpg"; 7 String IMAGE_URL_10 = "http://www.5djiaren.com/uploads/2015-10/24-175904_894.jpg"; 8 String IMAGE_URL_11 = "http://tupian.enterdesk.com/2013/mxy/0810/1/4.jpg"; 9 String IMAGE_URL_12 = "http://thumb.takefoto.cn/wp-content/uploads/2016/03/201603211236327864.jpg"; 10 11 String GIF_URL_0 = "http://uploads.rayli.com.cn/2016/0823/1471943453795.gif"; 12 String VIDEO_RUL_0 = "http://1251603248.vod2.myqcloud.com/4c9adaa7vodtransgzp1251603248/30e0819d9031868223192061218/v.f40.mp4"; 13 String IMAGE_URL_A4 = "http://www.hinews.cn/pic/0/10/24/62/10246261_856361.jpg"; 14 String IMAGE_URL_A5 = "http://img1.gtimg.com/0/1/168/16805_1200x1000_0.jpg"; 15 String IMAGE_URL_A6 = "http://img.mp.itc.cn/upload/20161104/d5737b849b6442ea9f594ddc3cbbc23a_th.jpeg"; 16 String IMAGE_URL_A7 = "http://5.26923.com/download/pic/000/330/4a9bfbb14275471e5ec956ad416b6194.jpg"; 17 String IMAGE_URL_A8 = "http://pic1.win4000.com/wallpaper/8/55f91ad3a23ea.jpg"; 18 String IMAGE_URL_A9 = "http://www.hinews.cn/pic/0/14/00/69/14006911_715505.jpg"; 19 String IMAGE_URL_A10 = "http://p4.so.qhmsg.com/t0199c5431414d2ea29.jpg"; 20 String IMAGE_URL_A11 = "http://p2.cri.cn/M00/1C/C0/CqgNOlm4oreAHkbcAAAAAAAAAAA69.3200x2000.jpeg"; 21 String IMAGE_URL_A12 = "http://img1.qq.com/ent/pics/10742/10742073.jpg"; 22 }
在清单加入权限
1 <uses-permission android:name="android.permission.INTERNET"/> 2 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 3 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
在build中加入Glide依赖
1 compile ‘com.github.bumptech.glide:glide:4.2.0‘
以上是关于Glide的使用学习的主要内容,如果未能解决你的问题,请参考以下文章