ImageUtils(bitmapUtils封装)

package com.example.newzimtang.tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.view.WindowManager;
import android.widget.ImageView;

import com.dandelion.AppContext;
import com.example.newzimtang.R;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;

/**
* Created by cuiying on 2016/4/25.
*/
public class ImageUtils {
/**
* 单例对象实例
*/
private static ImageUtils imageUtils;

private static BitmapUtils bitmapUtil;

public static ImageUtils getInstance(Context context) {
if (context == null)
return null;
if (imageUtils == null) {
imageUtils = new ImageUtils();

String discCachePath = context.getCacheDir().getAbsolutePath();
bitmapUtil = new BitmapUtils(context, discCachePath).configMemoryCacheEnabled(true).configDefaultBitmapConfig(Bitmap.Config.RGB_565)
.configDefaultLoadingImage(R.drawable.bg_lunbotu)
.configDefaultLoadFailedImage(R.drawable.bg_lunbotu).configThreadPoolSize(5);
}
return imageUtils;
}

/**
* load image to show
*
* @param url
* @param img
*/
public void loadImage(String url, ImageView img) {
if (bitmapUtil == null)
return;
bitmapUtil.display(img, url);
}

public void loadImage(String url, ImageView img, int defID) {
if (bitmapUtil == null)
return;
bitmapUtil.configDefaultLoadingImage(defID);
bitmapUtil.configDefaultLoadFailedImage(defID);
bitmapUtil.display(img, url);
}

public void loadImageOptions(String url, ImageView img, int defID, Bitmap.Config config) {
if (bitmapUtil == null)
return;
bitmapUtil.configDefaultLoadingImage(defID);
bitmapUtil.configDefaultLoadFailedImage(defID);
bitmapUtil.configDefaultBitmapConfig(config);
bitmapUtil.display(img, url);
}

public void loadImage(String url, ImageView img, BitmapLoadCallBack callback) {
if (bitmapUtil == null)
return;
bitmapUtil.display(img, url, callback);
}

public void loadImageViewType(String url, ImageView imageView, ImageView.ScaleType type) {
if (bitmapUtil == null)
return;
imageView.setScaleType(type);
bitmapUtil.display(imageView, url);
}

//原图显示图片
public void loadImageViewYuan(String url, ImageView imageView, boolean shifouyuantu) {
if (bitmapUtil == null)
return;
WindowManager wm = (WindowManager) AppContext.getApplication().getSystemService(
Context.WINDOW_SERVICE);

final int width = wm.getDefaultDisplay().getWidth();

imageView.setScaleType(ImageView.ScaleType.FIT_XY);
bitmapUtil.display(imageView, url);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (shifouyuantu) {
// Drawable drawable = imageView.getDrawable();
// Bitmap bitmap = drawableToBitmap(drawable);
// imageView.getLayoutParams().height = (int) (1f
// * width
// * bitmap.getHeight() / bitmap.getWidth());
// imageView.getLayoutParams().width = width;

// drawable = null;
// bitmap = null;
} else {
imageView.getLayoutParams().height = width;
imageView.getLayoutParams().width = width;
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
}

public Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成bitmap
{
int width = drawable.getIntrinsicWidth();// drawable的长宽
int height = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ALPHA_8 : Bitmap.Config.ALPHA_8;// drawable的颜色格式
Bitmap bitmap = Bitmap.createBitmap(width, height, config);// 建立对应bitmap
Canvas canvas = new Canvas(bitmap);// 建立对应bitmap的画布
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);// drawable内容画到画布中
return bitmap;
}

}

你可能感兴趣的:(ImageUtils(bitmapUtils封装))