Android Performance 性能提升

1. 经常变动的字符串要用 StringBuilder,然后每次变动用 append 方法。而不应该每次创建新的 String。

2. 使用 static final 变量。

3. It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly. 只对公共的 interface 用 getters and setters。而对于普通的类,应该直接使用 public 属性。

4. 数组遍历的方法。

static class Foo {
    int mSplat;
}

Foo[] mArray = ...

public void zero() {
    int sum = 0;
    for (int i = 0; i < mArray.length; ++i) {
        sum += mArray[i].mSplat;
    }
}

public void one() {
    int sum = 0;
    Foo[] localArray = mArray;
    int len = localArray.length;

    for (int i = 0; i < len; ++i) {
        sum += localArray[i].mSplat;
    }
}

public void two() {
    int sum = 0;
    for (Foo a : mArray) {
        sum += a.mSplat;
    }
}

最后一个函数最优秀,倒数第二个略次之。第一个函数是绝对错误的。

5. you can avoid the overhead by declaring fields and methods accessed by inner classes to have package access, rather than private access. Unfortunately this means the fields can be accessed directly by other classes in the same package, so you shouldn't use this in public API. 尽量避免使用 private inner class。可见性应该尽量用包可见(什么也不加)而不是私有(private)。

6. 能用 int 就尽量不用浮点数,因为成本更高。double 和 float 有同样的速度。

7. 调用 HashMap 的方法比 Map 快。

 

************** 界面方面 ***********************************************************

8. 不要嵌套使用 weight 属性。因为要进行很多计算。尤其对于界面经常变化的组件如 ListView GridView

9. 使用组合控件比分别使用多个控件效率更高。比如一个自定义的组件包含一个 TextView 和一个 ImageView。

10. 避免使用太多曾嵌套的 layout

11. 使用 <include/><merge/> 来重用 layout。

12. 对于 ListView,使用 ViewHolder 来存放控件以避免重复的进行 findViewById。

 

转载于:https://www.cnblogs.com/davesuen/p/3816937.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值