王学岗android开发之性能优化

如何进行性能优化?
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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值