ImageUtil.java 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package com.yc.ycvideoplayer;
  2. import android.content.Context;
  3. import android.graphics.Bitmap;
  4. import android.widget.ImageView;
  5. import androidx.core.graphics.drawable.RoundedBitmapDrawable;
  6. import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
  7. import com.bumptech.glide.Glide;
  8. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  9. import com.bumptech.glide.request.target.BitmapImageViewTarget;
  10. /**
  11. * ================================================
  12. * 作 者:杨充
  13. * 版 本:1.0
  14. * 创建日期:2017/3/14
  15. * 描 述:图片加载工具类
  16. * 修订历史:
  17. * ================================================
  18. */
  19. public class ImageUtil {
  20. /**
  21. * 将gif图转换为静态图
  22. * @param context
  23. * @param url
  24. * @param resId
  25. * @param imageView
  26. */
  27. public static void display(Context context , String url, int resId ,ImageView imageView) {
  28. if(imageView==null){
  29. return;
  30. }
  31. if(url!=null && url.length()>0){
  32. Glide.with(context)
  33. .asBitmap()
  34. .load(url)
  35. .placeholder(resId)
  36. .error(resId)
  37. .into(imageView);
  38. }else {
  39. Glide.with(context)
  40. .asBitmap()
  41. .load(resId)
  42. .placeholder(resId)
  43. .error(resId)
  44. .into(imageView);
  45. }
  46. }
  47. /**
  48. * 加载带有圆角的矩形图片 用glide处理
  49. *
  50. * @param path 路径
  51. * @param round 圆角半径
  52. * @param resId 加载失败时的图片
  53. * @param target 控件
  54. */
  55. public static void loadImgByPicassoWithRound(final Context activity, String path, final int round, int resId, final ImageView target) {
  56. if (path != null && path.length() > 0) {
  57. Glide.with(activity)
  58. .asBitmap()
  59. .load(path)
  60. .placeholder(resId)
  61. .error(resId)
  62. //设置缓存
  63. .diskCacheStrategy(DiskCacheStrategy.ALL)
  64. .into(new BitmapImageViewTarget(target) {
  65. @Override
  66. protected void setResource(Bitmap resource) {
  67. super.setResource(resource);
  68. RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(activity.getResources(), resource);
  69. //设置圆角弧度
  70. circularBitmapDrawable.setCornerRadius(round);
  71. target.setImageDrawable(circularBitmapDrawable);
  72. }
  73. });
  74. }
  75. }
  76. }