本例子是基于Glide和Picasso加载本地和网络图片的例子
源代码如下
/* * @description ImageFillUtil * 图片的填充工具类: * 注意Picasso占内存且在加载的时候会有闪烁, * 而Glide不占内存不闪烁,但会引起图片的失真 * 注意:所有的图片尺寸均为 dip * @author Nathaniel-nathanwriting@126.com * @time 2016/8/17-22:45 * @version v1.0.0 */ public class ImageFillUtils { public static final int TYPE_NORMAL = 0; // 正常,不做任何处理 public static final int TYPE_CIRCLE = 1; // 圆形图片 public static final int TYPE_ROUND = 2; // 圆角图片 private static final String TAG = ImageFillUtils.class.getSimpleName(); private static final int IMAGE_DEFAULT_RADIUS = 10; // 默认圆角半径 private static final int IMAGE_DEFAULT_MARGIN = 0; // 默认外边距 private static final int DEFAULT_IMAGE_WIDTH = 320; // 默认宽 private static final int DEFAULT_IMAGE_HEIGHT = 240; // 默认高 @SuppressLint("StaticFieldLeak") private static Builder builder; public static void clearGlide(Context context) { Glide.get(context).clearDiskCache(); Glide.get(context).clearMemory(); } public static void clearGlide(View target) { Glide.clear(target); } public static Builder builder(@NonNull Context context, @NonNull ImageView imageView) { if (builder == null) { builder = new Builder(context, imageView); } return builder; } public static class Builder { private Context context; private ImageView imageView; private int resourceId; private int placeHolder; private int errorResource; private int type; private int radius; private int margin; // dip private boolean gifable; private boolean fitable; private boolean fadeable; private boolean cacheable; private boolean fixable; private boolean resizeable; private String imageUrl; private int resizeX; private int resizeY; private RequestManager glideRequest; private Picasso picassoRequest; private RequestCreator requestCreator; private DrawableTypeRequest drawableTypeRequest; private Builder(Context context, ImageView imageView) { if (context == null || imageView == null) { throw new IllegalArgumentException("IllegalArgumentException in " + TAG); } this.context = context; this.imageView = imageView; } private int dip2px(int dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dipValue * scale + 0.5f); } public Builder setPlaceHolder(int placeHolder) { this.placeHolder = placeHolder; return this; } public Builder setType(int type) { this.type = type; return this; } public Builder setRadius(int radius) { this.radius = radius; return this; } public Builder setMargin(int margin) { this.margin = margin; return this; } public Builder setFixable(boolean fixable) { this.fixable = fixable; return this; } public Builder setGifable(boolean gifable) { this.gifable = gifable; return this; } public Builder setImageUrl(String imageUrl) { this.imageUrl = imageUrl; return this; } public Builder setResourceId(int resourceId) { this.resourceId = resourceId; return this; } public Builder setErrorResource(int errorResource) { this.errorResource = errorResource; return this; } public Builder setResizeable(boolean resizeable) { this.resizeable = resizeable; return this; } public Builder setCacheable(boolean cacheable) { this.cacheable = cacheable; return this; } public Builder setFitable(boolean fitable) { this.fitable = fitable; return this; } public Builder setFadeable(boolean fadeable) { this.fadeable = fadeable; return this; } public Builder resize(int resizeX, int resizeY) { this.resizeX = resizeX; this.resizeY = resizeY; return this; } public void setPicassoImage() { picassoRequest = Picasso.with(context); // 加载本地资源文件 if (resourceId > 0) { requestCreator = picassoRequest.load(resourceId); } // 加载网络资源文件 if (!TextUtils.isEmpty(imageUrl)) { requestCreator = picassoRequest.load(imageUrl); } // 占位符 if (placeHolder > 0) { requestCreator.placeholder(placeHolder); } // 展示类型 switch (type) { case TYPE_CIRCLE: requestCreator.transform(new PicassoCircleTransform()); break; case TYPE_ROUND: if (radius == 0) { radius = IMAGE_DEFAULT_RADIUS; } if (margin == 0) { margin = IMAGE_DEFAULT_MARGIN; } requestCreator.transform(new PicassoRoundTransform(radius, margin)); break; } if (errorResource > 0) { requestCreator.error(errorResource); } if (fixable) { requestCreator.fit(); } if (resizeable) { if (resizeX == 0) { resizeX = DEFAULT_IMAGE_WIDTH; } if (resizeY == 0) { resizeY = DEFAULT_IMAGE_HEIGHT; } requestCreator.resize(dip2px(resizeX), dip2px(resizeY)); } requestCreator.priority(Picasso.Priority.HIGH); requestCreator.into(imageView); } public void setGlideImage() { glideRequest = Glide.with(context); if (resourceId > 0) { drawableTypeRequest = glideRequest.load(resourceId); } if (!TextUtils.isEmpty(imageUrl)) { drawableTypeRequest = glideRequest.load(imageUrl); } switch (type) { case TYPE_CIRCLE: drawableTypeRequest.transform(new GlideCircleTransform(context)); break; case TYPE_ROUND: if (radius == 0) { radius = IMAGE_DEFAULT_RADIUS; } drawableTypeRequest.transform(new GlideRoundTransform(context, radius)); break; } if (cacheable) { drawableTypeRequest.diskCacheStrategy(DiskCacheStrategy.ALL); } if (fadeable) { drawableTypeRequest.crossFade(); } if (resizeable) { if (resizeX == 0) { resizeX = DEFAULT_IMAGE_WIDTH; } if (resizeY == 0) { resizeY = DEFAULT_IMAGE_HEIGHT; } drawableTypeRequest.override(dip2px(resizeX), dip2px(resizeY)); } if (placeHolder > 0) { drawableTypeRequest.placeholder(placeHolder); } if (errorResource > 0) { drawableTypeRequest.error(errorResource); } if (gifable) { drawableTypeRequest.asGif(); } // 根据ImageView缩放 drawableTypeRequest.into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) { @Override public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) { int imageWidth = bitmap.getWidth(); int imageHeight = bitmap.getHeight(); int height = imageView.getWidth() * imageHeight / imageWidth; ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams(); layoutParams.height = height; layoutParams.width = imageView.getWidth(); imageView.setImageBitmap(bitmap); } }); } } }
调用
ImageFillUtils.builder(context, imageView) .setImageUrl(url) .setPlaceHolder(R.drawable.img_transition_default) .setErrorResource(R.drawable.img_transition_default) .setPicassoImage(); // .setGlideImage();