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  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);
    }
  }
}

你可能感兴趣的:(android)