国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android中加載網(wǎng)絡(luò)資源時(shí)的優(yōu)化可使用(線(xiàn)程+緩存)解決

2020-04-11 12:16:27
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
網(wǎng)上關(guān)于這個(gè)方面的文章也不少,基本的思路是線(xiàn)程+緩存來(lái)解決。下面提出一些優(yōu)化:
1、采用線(xiàn)程池
2、內(nèi)存緩存+文件緩存
3、內(nèi)存緩存中網(wǎng)上很多是采用SoftReference來(lái)防止堆溢出,這兒嚴(yán)格限制只能使用最大JVM內(nèi)存的1/4
4、對(duì)下載的圖片進(jìn)行按比例縮放,以減少內(nèi)存的消耗

具體的代碼里面說(shuō)明。先放上內(nèi)存緩存類(lèi)的代碼MemoryCache.java:
復(fù)制代碼 代碼如下:

<SPAN style="FONT-SIZE: 18px"><STRONG>public class MemoryCache {
private static final String TAG = "MemoryCache";
// 放入緩存時(shí)是個(gè)同步操作
// LinkedHashMap構(gòu)造方法的最后一個(gè)參數(shù)true代表這個(gè)map里的元素將按照最近使用次數(shù)由少到多排列,即LRU
// 這樣的好處是如果要將緩存中的元素替換,則先遍歷出最近最少使用的元素來(lái)替換以提高效率
private Map<String, Bitmap> cache = Collections
.synchronizedMap(new LinkedHashMap<String, Bitmap>(10, 1.5f, true));
// 緩存中圖片所占用的字節(jié),初始0,將通過(guò)此變量嚴(yán)格控制緩存所占用的堆內(nèi)存
private long size = 0;// current allocated size
// 緩存只能占用的最大堆內(nèi)存
private long limit = 1000000;// max memory in bytes
public MemoryCache() {
// use 25% of available heap size
setLimit(Runtime.getRuntime().maxMemory() / 4);
}
public void setLimit(long new_limit) {
limit = new_limit;
Log.i(TAG, "MemoryCache will use up to " + limit / 1024. / 1024. + "MB");
}
public Bitmap get(String id) {
try {
if (!cache.containsKey(id))
return null;
return cache.get(id);
} catch (NullPointerException ex) {
return null;
}
}
public void put(String id, Bitmap bitmap) {
try {
if (cache.containsKey(id))
size -= getSizeInBytes(cache.get(id));
cache.put(id, bitmap);
size += getSizeInBytes(bitmap);
checkSize();
} catch (Throwable th) {
th.printStackTrace();
}
}
/**
* 嚴(yán)格控制堆內(nèi)存,如果超過(guò)將首先替換最近最少使用的那個(gè)圖片緩存
*
*/
private void checkSize() {
Log.i(TAG, "cache size=" + size + " length=" + cache.size());
if (size > limit) {
// 先遍歷最近最少使用的元素
Iterator<Entry<String, Bitmap>> iter = cache.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, Bitmap> entry = iter.next();
size -= getSizeInBytes(entry.getValue());
iter.remove();
if (size <= limit)
break;
}
Log.i(TAG, "Clean cache. New size " + cache.size());
}
}
public void clear() {
cache.clear();
}
/**
* 圖片占用的內(nèi)存
*
* @param bitmap
* @return
*/
long getSizeInBytes(Bitmap bitmap) {
if (bitmap == null)
return 0;
return bitmap.getRowBytes() * bitmap.getHeight();
}
}</STRONG></SPAN>

也可以使用SoftReference,代碼會(huì)簡(jiǎn)單很多,但是我推薦上面的方法。
復(fù)制代碼 代碼如下:

public class MemoryCache {

private Map<String, SoftReference<Bitmap>> cache = Collections
.synchronizedMap(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();
}
}

下面是文件緩存類(lèi)的代碼FileCache.java:
復(fù)制代碼 代碼如下:

public class FileCache {
private File cacheDir;
public FileCache(Context context) {
// 如果有SD卡則在SD卡中建一個(gè)LazyList的目錄存放緩存的圖片
// 沒(méi)有SD卡就放在系統(tǒng)的緩存目錄中
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) {
// 將url的hashCode作為緩存的文件名
String filename = String.valueOf(url.hashCode());
// Another possible solution
// String filename = URLEncoder.encode(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();
}
}

最后最重要的加載圖片的類(lèi),ImageLoader.java:
復(fù)制代碼 代碼如下:

public class ImageLoader {
MemoryCache memoryCache = new MemoryCache();
FileCache fileCache;
private Map<ImageView, String> imageViews = Collections
.synchronizedMap(new WeakHashMap<ImageView, String>());
// 線(xiàn)程池
ExecutorService executorService;
public ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(5);
}
// 當(dāng)進(jìn)入listview時(shí)默認(rèn)的圖片,可換成你自己的默認(rèn)圖片
final int stub_id = R.drawable.stub;
// 最主要的方法
public void DisplayImage(String url, ImageView imageView) {
imageViews.put(imageView, url);
// 先從內(nèi)存緩存中查找
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else {
// 若沒(méi)有的話(huà)則開(kāi)啟新線(xiàn)程加載圖片
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));
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
// 先從文件緩存中查找是否有
Bitmap b = decodeFile(f);
if (b != null)
return b;
// 最后從指定的url中下載圖片
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
OutputStream os = new FileOutputStream(f);
CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
// decode這個(gè)圖片并且按比例縮放以減少內(nèi)存消耗,虛擬機(jī)對(duì)每張圖片的緩存大小也是有限制的
private 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 = 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 *= 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;
}
// Task for the queue
private class PhotoToLoad {
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i) {
url = u;
imageView = i;
}
}
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
}
@Override
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);
// 更新的操作放在UI線(xiàn)程中
Activity a = (Activity) photoToLoad.imageView.getContext();
a.runOnUiThread(bd);
}
}
/**
* 防止圖片錯(cuò)位
*
* @param photoToLoad
* @return
*/
boolean imageViewReused(PhotoToLoad photoToLoad) {
String tag = imageViews.get(photoToLoad.imageView);
if (tag == null || !tag.equals(photoToLoad.url))
return true;
return false;
}
// 用于在UI線(xiàn)程中更新界面
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 (bitmap != null)
photoToLoad.imageView.setImageBitmap(bitmap);
else
photoToLoad.imageView.setImageResource(stub_id);
}
}
public void clearCache() {
memoryCache.clear();
fileCache.clear();
}
public static void CopyStream(InputStream is, OutputStream os) {
final int buffer_size = 1024;
try {
byte[] bytes = new byte[buffer_size];
for (;;) {
int count = is.read(bytes, 0, buffer_size);
if (count == -1)
break;
os.write(bytes, 0, count);
}
} catch (Exception ex) {
}
}
}

主要流程是先從內(nèi)存緩存中查找,若沒(méi)有再開(kāi)線(xiàn)程,從文件緩存中查找都沒(méi)有則從指定的url中查找,并對(duì)bitmap進(jìn)行處理,最后通過(guò)下面方法對(duì)UI進(jìn)行更新操作。
復(fù)制代碼 代碼如下:

a.runOnUiThread(...);

在你的程序中的基本用法:
復(fù)制代碼 代碼如下:

<SPAN style="FONT-SIZE: 18px"><STRONG>ImageLoader imageLoader=new ImageLoader(context);
...
imageLoader.DisplayImage(url, imageView);</STRONG></SPAN>

比如你的放在你的ListView的adapter的getView()方法中,當(dāng)然也適用于GridView。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴安盟| 论坛| 桐梓县| 双江| 青河县| 海盐县| 洪湖市| 尼木县| 崇州市| 忻城县| 抚松县| 来宾市| 泾阳县| 西乌珠穆沁旗| 自治县| 乐业县| 田林县| 大姚县| 饶平县| 昌邑市| 罗田县| 舒兰市| 麻栗坡县| 五寨县| 鲁甸县| 河南省| 邮箱| 安吉县| 博爱县| 邯郸市| 抚松县| 台北市| 客服| 遂宁市| 拉孜县| 开原市| 北碚区| 沾益县| 什邡市| 中宁县| 读书|