Android 网络本地显示图片Picasso优化封装使用

Picasso在使用过程中,经常忘记一些设置,比如图片的加载方式.centerCrop(),图片的加载优化.fit(),以及对图片的url的处理,因此对图片加载进行统一封装,使用相同的加载方式更有利于后期的维护和扩展。

图片的加载来源

图片的加载来源主要是String,file,Resource,我们都有相应的封装,

为了介绍方便

以下仅仅介绍String方式,以下同时有centerCrop和centerInside,同样我只是介绍centerCrop方式。 
示例代码,对应三种加载方式:

public static void loadCenterCrop(@NonNull Context context, String path,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    Picasso.with(context)
        .load(checkAndHandleUrl(path))
        .placeholder(placeholderResId)
        .error(errorResId)
        .centerCrop()
        .fit()
        //.tag(context)
        .into(target);
  }

  public static void loadCenterCrop(@NonNull Context context, @NonNull int resourceId,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    if (resourceId == 0) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(resourceId)
          .placeholder(placeholderResId)
          .error(errorResId)
          .centerCrop()
          .fit()
          .into(target);
    }
  }

  public static void loadCenterCrop(@NonNull Context context, File file,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    if (file == null) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(file)
          .placeholder(placeholderResId)
          .error(errorResId)
          .centerCrop()
          .fit()
          .into(target);
    }
  }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

主要的封装场景

1.正常加载图片

public static void loadCenterCrop(@NonNull Context context, String path,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    Picasso.with(context)
        .load(checkAndHandleUrl(path))
        .placeholder(placeholderResId)
        .error(errorResId)
        .centerCrop()
        .fit()
        //.tag(context)
        .into(target);
  }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

加载带圆角的图片

public static void loaderRoundConnerCenterCrop(@NonNull Context context, String path,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
      @NonNull int radius, @NonNull int margin) {
    if (path == null) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(checkAndHandleUrl(path))
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
          .centerCrop()
          .fit()
          .into(target);
    }
  }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

加载圆形图片(常用于圆形头像)

public static void loaderCircleCenterCrop(@NonNull Context context, String path,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    if (path == null || path.trim().isEmpty()) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(checkAndHandleUrl(path))
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new CropCircleTransformation())
          .centerCrop()
          .fit()
          .into(target);
    }
  }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

这些常用的方式都有封装,如果统一调用更方便代码的扩展和维护。

核心代码如下:


public class NewImageLoader {

  private NewImageLoader() {
    throw new IllegalStateException("no instance");
  }

  private static <T> void checkNull(T object, String message) {
    if (object == null) {
      throw new NullPointerException(message);
    }
  }

  /**
   * 检查传入的url或者数组中第一个url是不是为空
   */
  @SuppressWarnings("uncheck") public static boolean isUrlsEmpty(String... urls) {
    return urls == null || urls.length == 0 || urls[0] == null || urls[0].trim().isEmpty();
  }

  @SuppressWarnings("uncheck") public static String checkAndHandleUrl(String... urls) {
    if (isUrlsEmpty(urls)) {
      return "empty_url";
    }
    return urls[0];
  }

  public static Picasso getInstance(Context context) {
    return Picasso.with(context);
  }

  public static void load(@NonNull Context context, String path, @DrawableRes int placeholderResId,
      @NonNull ImageView target) {
    Picasso.with(context)
        .load(checkAndHandleUrl(path))
        .placeholder(placeholderResId)
        .error(placeholderResId)
        .into(target);
  }

  /**
   * 默认图为{@link R.drawable.img_defult}
   */
  public static void loadCenterCrop(@NonNull Context context, String path,
      @NonNull ImageView target) {
    loadCenterCrop(context, path, R.drawable.img_defult, target);
  }

  public static void loadCenterCrop(@NonNull Context context, String path,
      @DrawableRes int placeholderResId, @NonNull ImageView target) {
    loadCenterCrop(context, path, placeholderResId, placeholderResId, target);
  }

  public static void loadCenterCrop(@NonNull Context context, String path,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    Picasso.with(context)
        .load(checkAndHandleUrl(path))
        .placeholder(placeholderResId)
        .error(errorResId)
        .centerCrop()
        .fit()
        //.tag(context)
        .into(target);
  }

  public static void loadCenterCrop(@NonNull Context context, @NonNull int resourceId,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    if (resourceId == 0) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(resourceId)
          .placeholder(placeholderResId)
          .error(errorResId)
          .centerCrop()
          .fit()
          .into(target);
    }
  }

  public static void loadCenterCrop(@NonNull Context context, File file,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    if (file == null) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(file)
          .placeholder(placeholderResId)
          .error(errorResId)
          .centerCrop()
          .fit()
          .into(target);
    }
  }
  /**
   * 以下三个方法是centerCrop方式加载带圆角的图片
   */
  public static void loaderRoundConnerCenterCrop(@NonNull Context context, String path,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
      @NonNull int radius, @NonNull int margin) {
    if (path == null) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(checkAndHandleUrl(path))
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
          .centerCrop()
          .fit()
          .into(target);
    }
  }
  public static void loaderRoundConnerCenterCrop(@NonNull Context context, int resourceId,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
      @NonNull int radius, @NonNull int margin) {
    if (resourceId == 0) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(resourceId)
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
          .centerCrop()
          .fit()
          .into(target);
    }
  }
  public static void loaderRoundConnerCenterCrop(@NonNull Context context, File file,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
      @NonNull int radius, @NonNull int margin) {
    if (file == null) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(file)
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
          .centerCrop()
          .fit()
          .into(target);
    }
  }
  /**
   * 以下三个方法是centerCrop方式加载圆形的图片
   */
  public static void loaderCircleCenterCrop(@NonNull Context context, String path,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    if (path == null || path.trim().isEmpty()) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(checkAndHandleUrl(path))
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new CropCircleTransformation())
          .centerCrop()
          .fit()
          .into(target);
    }
  }

  public static void loaderCircleCenterCrop(@NonNull Context context, @NonNull int resourceId,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {

    if (resourceId == 0) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(resourceId)
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new CropCircleTransformation())
          .centerCrop()
          .fit()
          .into(target);
    }
  }

  public static void loaderCircleCenterCrop(@NonNull Context context, File file,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    if (file == null) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(file)
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new CropCircleTransformation())
          .centerCrop()
          .fit()
          .into(target);
    }
  }

  /****************************** CenterInside ***************************************/
  public static void loaderCenterInside(@NonNull Context context, String path,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    if (path == null || path.trim().isEmpty()) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(checkAndHandleUrl(path))
          .placeholder(placeholderResId)
          .error(errorResId)
          .centerInside()
          .fit()
          .into(target);
    }
  }

  public static void loaderCenterInside(@NonNull Context context, @NonNull int resourceId,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {

    if (resourceId == 0) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(resourceId)
          .placeholder(placeholderResId)
          .error(errorResId)
          .centerInside()
          .fit()
          .into(target);
    }
  }

  public static void loaderCenterInside(@NonNull Context context, File file,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    if (file == null) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(file)
          .placeholder(placeholderResId)
          .error(errorResId)
          .centerInside()
          .fit()
          .into(target);
    }
  }
  /**
   * 以下三个方法是centerInside方式加载带圆角的图片
   */

  public static void loaderRoundConnerCenterInside(@NonNull Context context, String path,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
      @NonNull int radius, @NonNull int margin) {
    if (path == null) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(checkAndHandleUrl(path))
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
          .centerInside()
          .fit()
          .into(target);
    }
  }
  public static void loaderRoundConnerCenterInside(@NonNull Context context, int resourceId,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
      @NonNull int radius, @NonNull int margin) {
    if (resourceId == 0) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(resourceId)
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
          .centerInside()
          .fit()
          .into(target);
    }
  }
  public static void loaderRoundConnerCenterInside(@NonNull Context context, File file,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target,
      @NonNull int radius, @NonNull int margin) {
    if (file == null) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(file)
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new RoundedCornersTransformation(DensityUtil.dip2px(context, radius), margin))
          .centerInside()
          .fit()
          .into(target);
    }
  }
  /**
   * 以下三个方法是centerInside方式加载圆形的图片
   */
  public static void loaderCircleCenterInside(@NonNull Context context, String path,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    if (path == null || path.trim().isEmpty()) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(path)
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new CropCircleTransformation())
          .centerInside()
          .fit()
          .into(target);
    }
  }

  public static void loaderCircleCenterInside(@NonNull Context context, @NonNull int resourceId,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {

    if (resourceId == 0) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(resourceId)
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new CropCircleTransformation())
          .centerInside()
          .fit()
          .into(target);
    }
  }

  public static void loaderCircleCenterInside(@NonNull Context context, File file,
      @DrawableRes int placeholderResId, @DrawableRes int errorResId, @NonNull ImageView target) {
    if (file == null) {
      target.setImageDrawable(context.getResources().getDrawable(placeholderResId));
    } else {
      Picasso.with(context)
          .load(file)
          .placeholder(placeholderResId)
          .error(errorResId)
          .transform(new CropCircleTransformation())
          .centerInside()
          .fit()
          .into(target);
    }
  }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值