package com.lq.android.common.util;


import java.lang.ref.ReferenceQueue;

import java.lang.ref.WeakReference;

import java.util.HashMap;

import java.util.LinkedHashMap;


import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.drawable.BitmapDrawable;

import android.os.AsyncTask;

import android.view.View;


import com.lq.android.R;


/**

* 图片加载缓存类

*

* @作者: 刘倩</br>

* @时间: 2014年4月23日 下午9:45:31</br>

* @描述: 图片加载缓存</br>

*/

public class ImageLoader

{

/** 缓存 */

private LruCache<String, Bitmap> cache = new LruCache<String, Bitmap>(20);


/**

* 显示图片的方法

*

* @param url

* 图片的url

* @param activity

* 显示图片的activity

* @param imageView

* 显示图片的控件

*/

@SuppressWarnings("deprecation")

public void displayImage(String url, Activity activity, View imageView)

{

Bitmap bitmap = cache.get(url);

if (bitmap != null)

{

imageView.setBackgroundDrawable(new BitmapDrawable(bitmap));

} else

{

imageView.setBackgroundResource(R.drawable.bg_cover_default);

try

{

new MyAsyncTask(imageView, url).execute();

} catch (Exception e)

{

e.printStackTrace();

}

}

}


/**

* 定义异步类

*

* @作者: 刘倩</br>

* @时间: 2014年4月23日 下午10:24:48</br>

* @描述: 对图片的操作</br>

*/

class MyAsyncTask extends AsyncTask<String, Integer, Bitmap>

{

/** 显示图片的View */

private View imageView;

/** 图片的url */

private String url;


/**

* 有参构造器

*

* @param imageView

* 显示图片的view

* @param url

* 下载图片的url

*/

public MyAsyncTask(View imageView, String url)

{

this.imageView = imageView;

this.url = url;

}


@Override

protected Bitmap doInBackground(String... params)

{

// 定义文件名称

String fileName = url.substring(url.lastIndexOf("/") + 1,

url.length());

// 本地图片

String bookCoverPath = "";

if(Boolean.valueOf(Tools.getSingleBook())){

bookCoverPath = AppConstant.bookSingleCoverPath;

}else{

bookCoverPath = AppConstant.bookCoverPath;

}

Bitmap bitmap = Tools.createBitmapFromSdcardOrData(ToolsDataBase.getFilePath(bookCoverPath+fileName));

if(bitmap == null){

try

{

ToolsDataBase.downLoadFileToSdcard(url, bookCoverPath, fileName);

} catch (Exception e)

{

e.printStackTrace();

}

}

return null;

}


}

}


/**

* 缓存类

*

* @作者: 刘倩</br>

* @时间: 2014年4月23日 下午9:50:36</br>

* @描述: 缓存功能的实现类</br>

*/

class LruCache<K, V>

{

/** 定义HashMap */

private final HashMap<K, V> mLruMap;

/** 定义weakMap */

private final HashMap<K, Entry<K, V>> mWeakMap = new HashMap<K, Entry<K, V>>();

/** 定义队列 */

private ReferenceQueue<V> mQueue = new ReferenceQueue<V>();


/**

* 定义有参构造器

*

* @param capacity

* 容量

*/

@SuppressWarnings("serial")

public LruCache(final int capacity)

{

mLruMap = new LinkedHashMap<K, V>(16, 0.75f, true)

{


@Override

protected boolean removeEldestEntry(Entry<K, V> eldest)

{

return size() > capacity;

}


};

}


/**

* 清除缓存的数据

*/

@SuppressWarnings("unchecked")

private void cleanUpWeakMap()

{

Entry<K, V> entry = (Entry<K, V>) mQueue.poll();

while (entry != null)

{

mWeakMap.remove(entry.mKey);

entry = (Entry<K, V>) mQueue.poll();

}

}


/**

* 定义V

*

* @param key

* 键

* @param value

* 值

* @return 返回自定义的V

*/

public synchronized V put(K key, V value)

{

cleanUpWeakMap();

mLruMap.put(key, value);

Entry<K, V> entry = mWeakMap.put(key, new Entry<K, V>(key, value,

mQueue));

return entry == null ? null : entry.get();

}


/**

* 得到key值

*

* @param key

* 键

* @return 返回键值

*/

public synchronized V get(K key)

{

cleanUpWeakMap();

V value = mLruMap.get(key);

if (value != null)

{

return value;

}

Entry<K, V> entry = mWeakMap.get(key);

return entry == null ? null : entry.get();

}


/**

* 清除数据

*/

public synchronized void clear()

{

mLruMap.clear();

mWeakMap.clear();

mQueue = new ReferenceQueue<V>();

}


/**

* 定义Entry类

*

* @作者: 刘倩</br>

* @时间: 2014年4月23日 下午10:03:55</br>

* @描述: Entry类主要定义一些参数</br>

*/

private static class Entry<K, V> extends WeakReference<V>

{

/** K实例 */

K mKey;


public Entry(K key, V value, ReferenceQueue<V> queue)

{

super(value, queue);

mKey = key;

}

}

}

Tools中的方法:

/**

* 图片的不等比缩放

*

* @param src

* 源图片

* @param destWidth

* 缩放的宽度

* @param destHeigth

* 缩放的高度

* @return

*/

public static Bitmap lessenBitmap(Bitmap src, int destWidth, int destHeigth) {

try {

if (src == null)

return null;


int w = src.getWidth();// 源文件的大小

int h = src.getHeight();

float scaleWidth = ((float) destWidth) / w;// 宽度缩小比例

float scaleHeight = ((float) destHeigth) / h;// 高度缩小比例

Matrix m = new Matrix();// 矩阵

m.postScale(scaleWidth, scaleHeight);// 设置矩阵比例

Bitmap resizedBitmap = Bitmap

.createBitmap(src, 0, 0, w, h, m, true);// 直接按照矩阵的比例把源文件画入进行

return resizedBitmap;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 从sdcard或data文件夹读取图片

*

* @param context

* @param imagePath

* @return

*/

public static Bitmap createBitmapFormSdcardOrData(String imagePath) {

if (null == imagePath) {

return null;

}

InputStream stream = null;

try {

File file = new File(imagePath);

if (!file.exists())

return null;

BitmapFactory.Options o = new BitmapFactory.Options();

o.inJustDecodeBounds = true;

BitmapFactory.decodeStream(new FileInputStream(imagePath), null, o);


final int REQUIRED_SIZE = 70;

int width_tmp = o.outWidth, height_tmp = o.outHeight;

int scale = 1;

while (true) {

if (width_tmp / 2 < REQUIRED_SIZE

|| height_tmp / 2 < REQUIRED_SIZE)

break;

width_tmp /= 2;

height_tmp /= 2;

scale++;

}


BitmapFactory.Options o2 = new BitmapFactory.Options();

o2.inSampleSize = scale;

Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(

imagePath), null, o2);

return getRoundedCornerBitmap(bitmap);

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (stream != null) {

stream.close();

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

return null;

}

ToolsDataBase中的方法:

/**

* 得到当前的路径

*

* @param filePath

* 文件路径

* @return

*/

public static String getFilePath(String filePath) {

try {

if (new File(AppConstant.sdcardRootPath + filePath).exists()) {

return AppConstant.sdcardRootPath + filePath;

} else if (new File(AppConstant.dataRootPath + filePath).exists()) {

return AppConstant.dataRootPath + filePath;

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

具体调用可以如下:

imageLoader.displayImage(coverUrl, activity,coverImage);


更多相关文章

  1. assets点9图加载成NinePatchDrawable
  2. ImageLoader修改后
  3. andriod之ContentProvider读取外部存储图片
  4. 自定义 照相机
  5. 一个图片制作ProgressBar
  6. gridview显示图片
  7. Android(安卓)Bitmap详细介绍
  8. android自定义滑动选择开关
  9. android 设置圆角图片实现代码

随机推荐

  1. Android(安卓)快速获取用户通信录联系人
  2. 除去ScrollView拉到尽头时再拉的阴影效果
  3. UVC系列1-Android盒子控制云台摄像头系列
  4. android 获得焦点(View get focus)
  5. Android 国际化,文本国际化,图片国际化
  6. iOS 和 Android(安卓)测试托管平台 FIR.i
  7. mac Android studio3.2版本安装+环境搭建
  8. Android中文API(131) —— GpsStatus
  9. ScrollView中添加一个android:fillViewpo
  10. 去掉Android app上的Title Bar