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(quot;no instancequot;); } private static lt;Tgt; void checkNull(T object, String message) { if (object == null) { throw new NullPointerException(message); } } /** * 檢查傳入的url或者數組中第一個url是不是為空 */ @SuppressWarnings(quot;uncheckquot;) public static boolean isUrlsEmpty(String... urls) { return urls == null || urls.length == 0 || urls[0] == null || urls[0].trim().isEmpty(); } @SuppressWarnings(quot;uncheckquot;) public static String checkAndHandleUrl(String... urls) { if (isUrlsEmpty(urls)) { return quot;empty_urlquot;; } 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); } } }
Tags: 安卓開發
文章來源:http://blog.csdn.net/lpjishu/article/details/54381