ImageLoaderOptions.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package com.example.zhpan.circleviewpager.imageloader;
  2. import android.content.Context;
  3. import android.support.annotation.DimenRes;
  4. import android.support.annotation.DrawableRes;
  5. import android.widget.ImageView;
  6. import com.zhpan.idea.utils.DensityUtils;
  7. /**
  8. * <pre>
  9. * Created by zhangpan on 2019-08-12.
  10. * Description:
  11. * </pre>
  12. */
  13. public class ImageLoaderOptions {
  14. private ImageView mImageView;
  15. private String url;
  16. private @DrawableRes
  17. int resource;
  18. private @DrawableRes
  19. int placeholder;
  20. private @DrawableRes
  21. int errorDrawable;
  22. private boolean asGif;
  23. private boolean isCrossFade;
  24. private boolean skipMemoryCache;
  25. private float roundCorner;
  26. private boolean blurImage;
  27. // 高斯模糊参数,越大越模糊
  28. private int blurValue;
  29. private ScaleType mScaleType;
  30. private ImageSize mImageSize;
  31. private ImageDiskCacheStrategy mDiskCacheStrategy;
  32. private ImageLoaderOptions(Builder builder) {
  33. this.mImageView = builder.mImageView;
  34. this.url = builder.url;
  35. this.resource = builder.resource;
  36. this.placeholder = builder.placeholder;
  37. this.errorDrawable = builder.errorDrawable;
  38. this.asGif = builder.asGif;
  39. this.isCrossFade = builder.isCrossFade;
  40. this.skipMemoryCache = builder.skipMemoryCache;
  41. this.mDiskCacheStrategy = builder.mDiskCacheStrategy;
  42. this.roundCorner = builder.roundCorner;
  43. this.blurImage = builder.blurImage;
  44. this.blurValue = builder.blurValue;
  45. this.mScaleType = builder.mScaleType;
  46. this.mImageSize = builder.mImageSize;
  47. }
  48. public ImageView getImageView() {
  49. return mImageView;
  50. }
  51. public String getUrl() {
  52. return url;
  53. }
  54. public int getResource() {
  55. return resource;
  56. }
  57. public int getPlaceholder() {
  58. return placeholder;
  59. }
  60. public int getErrorDrawable() {
  61. return errorDrawable;
  62. }
  63. public boolean isAsGif() {
  64. return asGif;
  65. }
  66. public boolean isCrossFade() {
  67. return isCrossFade;
  68. }
  69. public boolean isSkipMemoryCache() {
  70. return skipMemoryCache;
  71. }
  72. public float getRoundCorner() {
  73. return roundCorner;
  74. }
  75. public boolean isBlurImage() {
  76. return blurImage;
  77. }
  78. public int getBlurValue() {
  79. return blurValue;
  80. }
  81. public ScaleType getScaleType() {
  82. return mScaleType;
  83. }
  84. public ImageSize getImageSize() {
  85. return mImageSize;
  86. }
  87. public ImageDiskCacheStrategy getDiskCacheStrategy() {
  88. return mDiskCacheStrategy;
  89. }
  90. public static class Builder {
  91. private ImageView mImageView;
  92. private String url;
  93. private @DrawableRes
  94. int resource = -1;
  95. private @DrawableRes
  96. int placeholder = -1;
  97. private @DrawableRes
  98. int errorDrawable = -1;
  99. private boolean asGif;
  100. private boolean isCrossFade = true;
  101. private boolean skipMemoryCache;
  102. private float roundCorner;
  103. private boolean blurImage;
  104. private int blurValue; // 高斯模糊参数,越大越模糊
  105. private ScaleType mScaleType = ScaleType.DEFAULT;
  106. private ImageSize mImageSize;
  107. private ImageDiskCacheStrategy mDiskCacheStrategy = ImageDiskCacheStrategy.DEFAULT;
  108. public Builder into(ImageView imageView) {
  109. this.mImageView = imageView;
  110. return this;
  111. }
  112. public Builder load(String url) {
  113. this.url = url;
  114. return this;
  115. }
  116. public Builder load(@DrawableRes int resource) {
  117. this.resource = resource;
  118. return this;
  119. }
  120. public Builder placeHolder(@DrawableRes int placeholder) {
  121. this.placeholder = placeholder;
  122. return this;
  123. }
  124. public Builder isCrossFade(boolean isCrossFade) {
  125. this.isCrossFade = isCrossFade;
  126. return this;
  127. }
  128. public Builder isSkipMemoryCache(boolean skipMemoryCache) {
  129. this.skipMemoryCache = skipMemoryCache;
  130. return this;
  131. }
  132. public Builder asGif(boolean asGif) {
  133. this.asGif = asGif;
  134. return this;
  135. }
  136. public Builder error(@DrawableRes int errorDrawable) {
  137. this.errorDrawable = errorDrawable;
  138. return this;
  139. }
  140. public Builder diskCacheStrategy(ImageDiskCacheStrategy mDiskCacheStrategy) {
  141. this.mDiskCacheStrategy = mDiskCacheStrategy;
  142. return this;
  143. }
  144. public Builder setRoundCorner(Context context, float roundCornerDp) {
  145. this.roundCorner = DensityUtils.dp2px(context, roundCornerDp);
  146. return this;
  147. }
  148. public Builder setRoundCorner(Context context, @DimenRes int cornerRes) {
  149. this.roundCorner = context.getResources().getDimension(cornerRes);
  150. return this;
  151. }
  152. public Builder setBlurImage(int blurValue) {
  153. this.blurImage = true;
  154. this.blurValue = blurValue;
  155. return this;
  156. }
  157. public Builder setScaleType(ScaleType scaleType) {
  158. mScaleType = scaleType;
  159. return this;
  160. }
  161. public Builder override(int imageWidth, int imageHeight) {
  162. this.mImageSize = new ImageSize(imageWidth, imageHeight);
  163. return this;
  164. }
  165. public ImageLoaderOptions build() {
  166. return new ImageLoaderOptions(this);
  167. }
  168. }
  169. public static class ImageSize {
  170. private int imageWidth;
  171. private int imageHeight;
  172. public ImageSize(int imageWidth, int imageHeight) {
  173. this.imageWidth = imageWidth;
  174. this.imageHeight = imageHeight;
  175. }
  176. public int getImageWidth() {
  177. return imageWidth;
  178. }
  179. public int getImageHeight() {
  180. return imageHeight;
  181. }
  182. }
  183. // 对应磁盘缓存策略
  184. public enum ImageDiskCacheStrategy {
  185. All, NONE, SOURCE, RESULT, DEFAULT
  186. }
  187. }