package com.android.chmo.utils; import android.graphics.drawable.Drawable; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.ImageView; import org.xutils.common.Callback; import org.xutils.common.util.DensityUtil; import org.xutils.image.ImageOptions; import org.xutils.x; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; /** * 加载图片工具类 */ public class XUtilsImage { /** * 显示图片(默认情况) * * @param imageView 图像控件 * @param iconUrl 图片地址 */ public static void display(ImageView imageView, String iconUrl) { if (TextUtils.isEmpty(iconUrl)) { Log.d("XUtilsImage", "url is null"); return; } ImageOptions imageOptions = new ImageOptions.Builder() .setImageScaleType(ImageView.ScaleType.CENTER_CROP) .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true .build(); x.image().bind(imageView, iconUrl, imageOptions); } public static void display(ImageView imageView, String iconUrl, int placeholderResId) { if (TextUtils.isEmpty(iconUrl)) { Log.d("XUtilsImage", "url is null"); return; } ImageOptions imageOptions = new ImageOptions.Builder() .setImageScaleType(ImageView.ScaleType.CENTER_CROP) .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true .setFailureDrawableId(placeholderResId) .setLoadingDrawableId(placeholderResId) .build(); x.image().bind(imageView, iconUrl, imageOptions); } /** * 显示图片(默认情况) */ public static void display(ImageView imageView, String iconUrl, ImageView.ScaleType scaleType, int placeholderResId) { if (TextUtils.isEmpty(iconUrl)) { return; } ImageOptions imageOptions = new ImageOptions.Builder() .setImageScaleType(scaleType) .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true .setFailureDrawableId(placeholderResId) .setLoadingDrawableId(placeholderResId) .build(); x.image().bind(imageView, iconUrl, imageOptions); } /** * 显示图片(默认情况) */ public static void display(ImageView imageView, String iconUrl, ImageView.ScaleType scaleType) { if (TextUtils.isEmpty(iconUrl)) { return; } ImageOptions imageOptions = new ImageOptions.Builder() .setImageScaleType(scaleType) .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true .build(); x.image().bind(imageView, iconUrl, imageOptions); } /** * 显示图片(默认情况) */ public static void display(final ImageView imageView, String iconUrl, ImageView.ScaleType scaleType, final View loadingView) { if (TextUtils.isEmpty(iconUrl)) { return; } if (null != loadingView) { loadingView.setVisibility(View.VISIBLE); } ImageOptions imageOptions = new ImageOptions.Builder() .setImageScaleType(scaleType) .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true .build(); x.image().bind(imageView, iconUrl, imageOptions, new Callback.CacheCallback() { Drawable result; @Override public boolean onCache(Drawable result) { imageView.setImageDrawable(result); this.result = result; return true; } @Override public void onSuccess(Drawable result) { this.result = result; imageView.setImageDrawable(result); } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { if (null != loadingView) { loadingView.setVisibility(View.GONE); } if (null != result) { imageView.setImageDrawable(result); } } }); } /** * 显示圆角图片 * * @param imageView 图像控件 * @param iconUrl 图片地址 * @param radius 圆角半径, */ public static void displayRound(ImageView imageView, String iconUrl, int radius) { if (TextUtils.isEmpty(iconUrl)) { return; } ImageOptions imageOptions = new ImageOptions.Builder() .setImageScaleType(ImageView.ScaleType.CENTER_CROP) .setRadius(DensityUtil.dip2px(radius)) .setIgnoreGif(false) .setCrop(true)//是否对图片进行裁剪 // .setFailureDrawableId(R.mipmap.loadfailed) // .setLoadingDrawableId(R.mipmap.loading) .build(); x.image().bind(imageView, iconUrl, imageOptions); } /** * 显示圆形头像,第三个参数为true * * @param imageView 图像控件 * @param iconUrl 图片地址 */ public static void displayCircluar(ImageView imageView, String iconUrl) { if (TextUtils.isEmpty(iconUrl)) { return; } ImageOptions imageOptions = new ImageOptions.Builder() .setImageScaleType(ImageView.ScaleType.CENTER_CROP) .setCircular(true) .setCrop(true) // .setLoadingDrawableId(R.mipmap.ic_launcher) // .setFailureDrawableId(R.mipmap.ic_launcher) .build(); x.image().bind(imageView, iconUrl, imageOptions); } public static void displayCircluar(ImageView imageView, String iconUrl, int failResId) { if (TextUtils.isEmpty(iconUrl)) { return; } ImageOptions imageOptions = new ImageOptions.Builder() .setImageScaleType(ImageView.ScaleType.CENTER_CROP) .setCircular(true) .setCrop(true) .setFailureDrawableId(failResId) .build(); x.image().bind(imageView, iconUrl, imageOptions); } public static void clearImageCache() { x.image().clearCacheFiles(); x.image().clearMemCache(); } /** * 方法说明:保存文件到本地 */ public static boolean saveImage(String urlPath, String savePath, String filename) { File saveFile = new File(savePath); if (!saveFile.exists()) { saveFile.mkdirs(); } try { URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); if (conn.getResponseCode() == 200) { InputStream in = conn.getInputStream(); OutputStream bos = new FileOutputStream(savePath + filename); byte[] buff = new byte[1024]; int len = 0; while ((len = in.read(buff)) != -1) { bos.write(buff, 0, len); } bos.flush(); bos.close(); in.close(); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } public static void displayWithSize(ImageView imageView, String iconUrl, int width, int height) { if (TextUtils.isEmpty(iconUrl)) { Log.d("XUtilsImage", "url is null"); return; } ImageOptions imageOptions = new ImageOptions.Builder() .setSize(width, height) .setImageScaleType(ImageView.ScaleType.CENTER_CROP) .setIgnoreGif(false)//是否忽略gif图。false表示不忽略。不写这句,默认是true .build(); x.image().bind(imageView, iconUrl, imageOptions); } }