1. 程式人生 > >Android中效能分析之TraceView

Android中效能分析之TraceView

複製程式碼
  1 package com.sunzn.app.utils;
  2 
  3 import java.io.File;
  4 import java.io.IOException;
  5 import java.io.InputStream;
  6 import java.lang.ref.SoftReference;
  7 import java.util.ArrayList;
  8 import java.util.HashMap;
  9 
 10 import android.content.Context;
 11 import android.graphics.Bitmap;
12 import android.os.Environment; 13 import android.os.Handler; 14 import android.os.Message; 15 16 public class ImageLoaderTools { 17 18 private HttpTools httptool; 19 20 private Context mContext; 21 22 private boolean isLoop = true; 23 24 private HashMap<String, SoftReference<Bitmap>> mHashMap_caches;
25 26 private ArrayList<ImageLoadTask> maArrayList_taskQueue; 27 28 private Handler mHandler = new Handler() { 29 public void handleMessage(android.os.Message msg) { 30 ImageLoadTask loadTask = (ImageLoadTask) msg.obj; 31 loadTask.callback.imageloaded(loadTask.path, loadTask.bitmap);
32 }; 33 }; 34 35 private Thread mThread = new Thread() { 36 37 public void run() { 38 39 while (isLoop) { 40 41 while (maArrayList_taskQueue.size() > 0) { 42 43 try { 44 ImageLoadTask task = maArrayList_taskQueue.remove(0); 45 46 if (Constant.LOADPICTYPE == 1) { 47 byte[] bytes = httptool.getByte(task.path, null, HttpTools.METHOD_GET); 48 task.bitmap = BitMapTools.getBitmap(bytes, 40, 40); 49 } else if (Constant.LOADPICTYPE == 2) { 50 InputStream in = httptool.getStream(task.path, null, HttpTools.METHOD_GET); 51 task.bitmap = BitMapTools.getBitmap(in, 1); 52 } 53 54 if (task.bitmap != null) { 55 mHashMap_caches.put(task.path, new SoftReference<Bitmap>(task.bitmap)); 56 File dir = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES); 57 if (!dir.exists()) { 58 dir.mkdirs(); 59 } 60 String[] path = task.path.split("/"); 61 String filename = path[path.length - 1]; 62 File file = new File(dir, filename); 63 BitMapTools.saveBitmap(file.getAbsolutePath(), task.bitmap); 64 Message msg = Message.obtain(); 65 msg.obj = task; 66 mHandler.sendMessage(msg); 67 } 68 } catch (IOException e) { 69 e.printStackTrace(); 70 } catch (Exception e) { 71 e.printStackTrace(); 72 } 73 74 synchronized (this) { 75 try { 76 wait(); 77 } catch (InterruptedException e) { 78 e.printStackTrace(); 79 } 80 } 81 82 } 83 84 } 85 86 }; 87 88 }; 89 90 public ImageLoaderTools(Context context) { 91 this.mContext = context; 92 httptool = new HttpTools(context); 93 mHashMap_caches = new HashMap<String, SoftReference<Bitmap>>(); 94 maArrayList_taskQueue = new ArrayList<ImageLoaderTools.ImageLoadTask>(); 95 mThread.start(); 96 } 97 98 private class ImageLoadTask { 99 String path; 100 Bitmap bitmap; 101 Callback callback; 102 } 103 104 public interface Callback { 105 void imageloaded(String path, Bitmap bitmap); 106 } 107 108 public void quit() { 109 isLoop = false; 110 } 111 112 public Bitmap imageLoad(String path, Callback callback) { 113 Bitmap bitmap = null; 114 String[] path1 = path.split("/"); 115 String filename = path1[path1.length - 1]; 116 117 if (mHashMap_caches.containsKey(path)) { 118 bitmap = mHashMap_caches.get(path).get(); 119 if (bitmap == null) { 120 mHashMap_caches.remove(path); 121 } else { 122 return bitmap; 123 } 124 } 125 126 File dir = mContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES); 127 128 File file = new File(dir, filename); 129 130 bitmap = BitMapTools.getBitMap(file.getAbsolutePath()); 131 if (bitmap != null) { 132 return bitmap; 133 } 134 135 ImageLoadTask task = new ImageLoadTask(); 136 task.path = path; 137 task.callback = callback; 138 maArrayList_taskQueue.add(task); 139 140 synchronized (mThread) { 141 mThread.notify(); 142 } 143 144 return null; 145 } 146 147 }
複製程式碼