XUtilsImage.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package com.android.chmo.utils;
  2. import android.graphics.drawable.Drawable;
  3. import android.text.TextUtils;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.ImageView;
  7. import org.xutils.common.Callback;
  8. import org.xutils.common.util.DensityUtil;
  9. import org.xutils.image.ImageOptions;
  10. import org.xutils.x;
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.InputStream;
  14. import java.io.OutputStream;
  15. import java.net.HttpURLConnection;
  16. import java.net.URL;
  17. /**
  18. * 加载图片工具类
  19. */
  20. public class XUtilsImage {
  21. /**
  22. * 显示图片(默认情况)
  23. *
  24. * @param imageView 图像控件
  25. * @param iconUrl 图片地址
  26. */
  27. public static void display(ImageView imageView, String iconUrl) {
  28. if (TextUtils.isEmpty(iconUrl)) {
  29. Log.d("XUtilsImage", "url is null");
  30. return;
  31. }
  32. ImageOptions imageOptions = new ImageOptions.Builder()
  33. .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
  34. .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true
  35. .build();
  36. x.image().bind(imageView, iconUrl, imageOptions);
  37. }
  38. public static void display(ImageView imageView, String iconUrl, int placeholderResId) {
  39. if (TextUtils.isEmpty(iconUrl)) {
  40. Log.d("XUtilsImage", "url is null");
  41. return;
  42. }
  43. ImageOptions imageOptions = new ImageOptions.Builder()
  44. .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
  45. .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true
  46. .setFailureDrawableId(placeholderResId)
  47. .setLoadingDrawableId(placeholderResId)
  48. .build();
  49. x.image().bind(imageView, iconUrl, imageOptions);
  50. }
  51. /**
  52. * 显示图片(默认情况)
  53. */
  54. public static void display(ImageView imageView, String iconUrl, ImageView.ScaleType scaleType, int placeholderResId) {
  55. if (TextUtils.isEmpty(iconUrl)) {
  56. return;
  57. }
  58. ImageOptions imageOptions = new ImageOptions.Builder()
  59. .setImageScaleType(scaleType)
  60. .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true
  61. .setFailureDrawableId(placeholderResId)
  62. .setLoadingDrawableId(placeholderResId)
  63. .build();
  64. x.image().bind(imageView, iconUrl, imageOptions);
  65. }
  66. /**
  67. * 显示图片(默认情况)
  68. */
  69. public static void display(ImageView imageView, String iconUrl, ImageView.ScaleType scaleType) {
  70. if (TextUtils.isEmpty(iconUrl)) {
  71. return;
  72. }
  73. ImageOptions imageOptions = new ImageOptions.Builder()
  74. .setImageScaleType(scaleType)
  75. .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true
  76. .build();
  77. x.image().bind(imageView, iconUrl, imageOptions);
  78. }
  79. /**
  80. * 显示图片(默认情况)
  81. */
  82. public static void display(final ImageView imageView, String iconUrl, ImageView.ScaleType scaleType, final View loadingView) {
  83. if (TextUtils.isEmpty(iconUrl)) {
  84. return;
  85. }
  86. if (null != loadingView) {
  87. loadingView.setVisibility(View.VISIBLE);
  88. }
  89. ImageOptions imageOptions = new ImageOptions.Builder()
  90. .setImageScaleType(scaleType)
  91. .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true
  92. .build();
  93. x.image().bind(imageView, iconUrl, imageOptions, new Callback.CacheCallback<Drawable>() {
  94. Drawable result;
  95. @Override
  96. public boolean onCache(Drawable result) {
  97. imageView.setImageDrawable(result);
  98. this.result = result;
  99. return true;
  100. }
  101. @Override
  102. public void onSuccess(Drawable result) {
  103. this.result = result;
  104. imageView.setImageDrawable(result);
  105. }
  106. @Override
  107. public void onError(Throwable ex, boolean isOnCallback) {
  108. }
  109. @Override
  110. public void onCancelled(CancelledException cex) {
  111. }
  112. @Override
  113. public void onFinished() {
  114. if (null != loadingView) {
  115. loadingView.setVisibility(View.GONE);
  116. }
  117. if (null != result) {
  118. imageView.setImageDrawable(result);
  119. }
  120. }
  121. });
  122. }
  123. /**
  124. * 显示圆角图片
  125. *
  126. * @param imageView 图像控件
  127. * @param iconUrl 图片地址
  128. * @param radius 圆角半径,
  129. */
  130. public static void displayRound(ImageView imageView, String iconUrl, int radius) {
  131. if (TextUtils.isEmpty(iconUrl)) {
  132. return;
  133. }
  134. ImageOptions imageOptions = new ImageOptions.Builder()
  135. .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
  136. .setRadius(DensityUtil.dip2px(radius))
  137. .setIgnoreGif(false)
  138. .setCrop(true)//是否对图片进行裁剪
  139. // .setFailureDrawableId(R.mipmap.loadfailed)
  140. // .setLoadingDrawableId(R.mipmap.loading)
  141. .build();
  142. x.image().bind(imageView, iconUrl, imageOptions);
  143. }
  144. /**
  145. * 显示圆形头像,第三个参数为true
  146. *
  147. * @param imageView 图像控件
  148. * @param iconUrl 图片地址
  149. */
  150. public static void displayCircluar(ImageView imageView, String iconUrl) {
  151. if (TextUtils.isEmpty(iconUrl)) {
  152. return;
  153. }
  154. ImageOptions imageOptions = new ImageOptions.Builder()
  155. .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
  156. .setCircular(true)
  157. .setCrop(true)
  158. // .setLoadingDrawableId(R.mipmap.ic_launcher)
  159. // .setFailureDrawableId(R.mipmap.ic_launcher)
  160. .build();
  161. x.image().bind(imageView, iconUrl, imageOptions);
  162. }
  163. public static void displayCircluar(ImageView imageView, String iconUrl, int failResId) {
  164. if (TextUtils.isEmpty(iconUrl)) {
  165. return;
  166. }
  167. ImageOptions imageOptions = new ImageOptions.Builder()
  168. .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
  169. .setCircular(true)
  170. .setCrop(true)
  171. .setFailureDrawableId(failResId)
  172. .build();
  173. x.image().bind(imageView, iconUrl, imageOptions);
  174. }
  175. public static void clearImageCache() {
  176. x.image().clearCacheFiles();
  177. x.image().clearMemCache();
  178. }
  179. /**
  180. * 方法说明:保存文件到本地
  181. */
  182. public static boolean saveImage(String urlPath, String savePath, String filename) {
  183. File saveFile = new File(savePath);
  184. if (!saveFile.exists()) {
  185. saveFile.mkdirs();
  186. }
  187. try {
  188. URL url = new URL(urlPath);
  189. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  190. if (conn.getResponseCode() == 200) {
  191. InputStream in = conn.getInputStream();
  192. OutputStream bos = new FileOutputStream(savePath + filename);
  193. byte[] buff = new byte[1024];
  194. int len = 0;
  195. while ((len = in.read(buff)) != -1) {
  196. bos.write(buff, 0, len);
  197. }
  198. bos.flush();
  199. bos.close();
  200. in.close();
  201. }
  202. return true;
  203. } catch (Exception e) {
  204. e.printStackTrace();
  205. return false;
  206. }
  207. }
  208. public static void displayWithSize(ImageView imageView, String iconUrl, int width, int height) {
  209. if (TextUtils.isEmpty(iconUrl)) {
  210. Log.d("XUtilsImage", "url is null");
  211. return;
  212. }
  213. ImageOptions imageOptions = new ImageOptions.Builder()
  214. .setSize(width, height)
  215. .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
  216. .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true
  217. .build();
  218. x.image().bind(imageView, iconUrl, imageOptions);
  219. }
  220. }