[英]Android Bitmap usage Optimization
我一直在閱讀使用位圖的不同方式,每種方式各有優缺點。
因此,我有一個問題尚未真正找到答案。 我的應用程序中發生大量內存泄漏。 因此,我解決了這個問題,並使用了幾個位圖。 一個位圖尤其被不同對象多次使用。 我調用位圖圖像並使用Canvas在其上繪制文本,然后以適合使用它的設備的分辨率壓縮並保存圖像。
當前,每次需要繪制新文本並創建圖像的更新版本時,我都使用BitmapFactory.decodeResource(Resources,ResourceID)獲取位圖。
有沒有更好的方法來獲取圖像? 就像在某個地方緩存它:1)可能嗎? 2)與從可繪制文件夾中進行解碼相比,更好的方法可以多次引用相同的圖像?
注意:此過程是使用AsyncTask完成的,因此根據設備的不同,可以在技術上一次使用映像來一次使用映像。 不知道這是否會導致資源沖突。
謝謝,我覺得答案不需要代碼,但是如果需要,我可以添加它。
如果您正在尋找Android中用於位圖的簡單緩存,則以下實現將同時緩存在內存和文件中。 當它再次加載位圖資源(由圖像文件名標識)時,它將嘗試搜索內存,然后在文件高速緩存中搜索該位圖。
用法:
imageLoader.DisplayImage(“ image_filename_in_resource_folder”,imageView);
public class ImageLoader {
final int stub_id = R.drawable.ic_thumbnail;
private Context mContext;
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
ExecutorService executorService;
private Map<ImageView, String> imageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
this.mContext = context;
}
public void DisplayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
queuePhoto(url, imageView);
imageView.setImageResource(stub_id);
}
}
private void queuePhoto(String url, ImageView imageView) {
PhotoToLoad p = new PhotoToLoad(url, imageView);
executorService.submit(new PhotosLoader(p));
}
//decodes image and scales it to reduce memory consumption
protected Bitmap decodeFile(File f) {
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 150;
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 *= 2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
//Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
Bitmap getBitmap(String imageFileName) {
File f = fileCache.getFile(imageFileName);
//from SD cache
Bitmap b = decodeFile(f);
if (b != null)
return b;
try {
Resources resource = mContext.getResources();
int imageId = resource.getIdentifier(imageFileName, "drawable", mContext.getPackageName());
Bitmap bitmap = null;
InputStream is = resource.openRawResource(imageId);
OutputStream os = new FileOutputStream(f);
IOUtils.copy(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (IOException e) {
return null;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
public void run() {
if (imageViewReused(photoToLoad))
return;
Bitmap bmp = getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if (imageViewReused(photoToLoad))
return;
BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
}
public void run() {
if (imageViewReused(photoToLoad))
return;
if (null != bitmap) {
photoToLoad.imageView.setImageBitmap(bitmap);
}
}
}
}
內存緩存類
public class MemoryCache {
private HashMap<String, SoftReference<Bitmap>> cache = new HashMap<String, SoftReference<Bitmap>>();
public Bitmap get(String id) {
if (!cache.containsKey(id))
return null;
SoftReference<Bitmap> ref = cache.get(id);
return ref.get();
}
public void put(String id, Bitmap bitmap) {
cache.put(id, new SoftReference<Bitmap>(bitmap));
}
public void clear() {
cache.clear();
}
}
文件緩存的類
public class FileCache {
private File cacheDir;
public FileCache(Context context) {
//Find the dir to save cached images
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), "LazyList");
else
cacheDir = context.getCacheDir();
if (!cacheDir.exists())
cacheDir.mkdirs();
}
public File getFile(String url) {
//Identify images by hashcode. Not a perfect solution.
String filename = String.valueOf(url.hashCode());
//Another possible solution, using cypto hashing functions like md5
//String filename = md5(url);
File f = new File(cacheDir, filename);
return f;
}
public void clear() {
File[] files = cacheDir.listFiles();
if (files == null)
return;
for (File f : files)
f.delete();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.