如何进行性能优化?
1、避免死循环
a,递归调用
public static void start(){
System.out.println("--------------");
start();
}
b,for循环
for(;;){
//忘记break;
}
c,while循环
while(true){
//忘记break;
}
2、数组大小分配
3、能够写成局部变量的尽量写成局部变量(有时则相反)
4、不用的Cursor、File需要关闭
5、不良代码
如下列代码
for(int i=0;i<list.size();i++){
}
应写为:
int len=list.size();
for(int i=0;i<len;i++){
}
6、Adapter缓存
7、线程
8、Bitmap
package com.example.acer.oo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
/**
* @author w_x_g
* @time 2016/10/26 13:36
* @note ${TODO}
*/
public class BitmapUtils {
public static Bitmap scaleBitmap(Context context) {
Bitmap bitmap = null;
BitmapFactory.Options opts = new BitmapFactory.Options();
//设置为true只会返回图片的尺寸,而不加载图片本身
opts.inJustDecodeBounds = true;
//这里的bitmap是空
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.gang, opts);
//options.outWidth 和 options.outHeight就是我们想要的宽和高了
int outWidt = opts.outWidth;
int outHeight=opts.outHeight;
//设置图片缩放的倍数
opts.inSampleSize=5;
opts.inJustDecodeBounds=false;
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.gang, opts);
return bitmap;
}
}