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;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值