为了账号安全,请及时绑定邮箱和手机立即绑定

Picasso封装优化-图片加载

标签:
Java Android

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.正常加载图片
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 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 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 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);
    }
  }
}
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消