Android中内存优化是重要的一环,图片优化占很大一部分,volley是个很好的图片处理工具
一、注意图片使用bitmap图
当使用listview和gridview等控件时需要对图片进行缓存优化
例如可以使用 import com.nostra13.universalimageloader.core.ImageLoader; 中的ImageLoader的方法
public void loadImageWithDefault(String imageUrl, ImageView imageView, int defaultDrawable) {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(defaultDrawable)
.showImageOnLoading(defaultDrawable)
.showImageOnFail(defaultDrawable)
.imageScaleType(ImageScaleType.EXACTLY)
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
String imageUri = imageUrl;
if(!URLUtil.isHttpUrl(imageUrl) && imageUrl != null)
imageUri = "drawable://" + context.getResources().getIdentifier(imageUri, "drawable","com.sursendoubi");
ImageLoader.getInstance().displayImage(imageUri, imageView, options);
}
给imageview图片时,尽量使用setImageBitmap或者setBackgroundDrawable等加载
BitmapFactory使用进行缓存,取样优化(分为两种情况 1、使用本地图片资源(可以是uri地址,也可以是file地址)
2、从网络上获取图片的输入流
/**
* 通过Uri图片路径转化成BitMap进行图片优化
* @param context
* @param resId
* @return
*/
public static Bitmap getImageBitMapWithUri(Context context,int resId){
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}
/**
* 从网络上获取图片资源
*/
private void showGuideImage(){
String imageUrl="http://img.doubicall.com/vod/start_page.png";
final ImageView guideImageView=(ImageView)findViewById(R.id.guide_image);
final File guideImageFile=new File(getExternalFilesDir("guideImageFile"),"guideImage");
if(guideImageFile.exists()){
Bitmap bitmap=BitmapFactory.decodeFile(guideImageFile.getAbsolutePath());
guideImageView.setImageBitmap(bitmap);
}
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder()
.url(imageUrl)
.build();
Call call=client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onResponse(Call arg0, okhttp3.Response response) throws IOException {
final Bitmap bitmap=BitmapFactory.decodeStream(response.body().byteStream());
if(guideImageFile != null && bitmap != null){//容易空指针异常
BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream(guideImageFile,false));
if(outputStream != null){
bitmap.compress(Bitmap.CompressFormat.PNG,100 , outputStream);
outputStream.flush();
outputStream.close();
}
bitmap.recycle();
}
}
@Override
public void onFailure(Call arg0, IOException arg1) {
}
});
}
// 还用注意bitmap确定不用时,需要及时的释放
// 如果图片还没有回收,强制回收
if (!bitmap1.isRecycled()) {
bitmap1.recycle();
}
另外还有就是在xml属性中有这个节点bitmap,可以直接引用(当图片过大时)也可以起到优化的作用
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:src="@drawable/general_head_fang"
android:tileMode="disabled" ><!-平铺模式->
</bitmap>
二、把不用的对象 进行关闭处理,最重要的是最后设置为null
三、当从网络上下文件时,需要对输入和输出流进行优化
需要关闭输入流和输出流(输出流是需要清除缓存BufferedOutputStream flush)(代码示例如上)