Android面试刨根问底之常用源码篇(一),Android开发教程


大致分为四点去回答。快、稳、小、省

1. 快

启动快,加载快,避免卡顿

基本操作

  • 主线程不做耗时操作

  • application里对必要的三方库延迟初始化(延迟加载,异步加载,分布加载)

  • 启动白屏优化

View优化

  • View 布局(viewstub,include,merge,层级深)

  • 复杂页面细分优化

  • 过度绘制的优化

  • xml中无用的背景不设置

  • 控件无用属性删除

内存优化

  • 页面切换,前后台切换

  • fragment的懒加载

  • 必要的缓存

  • 空间换时间

  • 四大引用的合理使用

  • 减小不必要的内存开销

  • 数据bean的合理定义

  • ArrayList、HashMap的使用

  • 线程池、bitmap、view的复用

  • 不用的大对象主动设置null

代码优化

  • for循环内不定义对象

  • 使用文件IO代替数据库

  • 自定义Drawable不在draw()里面创建对象操作

  • 类中没有使用到成员变量的方法可以设置static

2. 稳

稳定不崩溃,减小crash,避免anr

  • 主线程不做耗时操作

  • activity 5秒、broadcast 10秒、service 20秒

  • 资源对象及时关闭(Cursor,File)

  • Handler的处理

  • 避免内存泄露

  • crash上传机制

  • WebView的内存泄露

3. 小

安装包小

  • 代码混淆(proguard)

  • 资源优化(lint)

  • 图片优化(mipmap/webp)

4. 省

省电省流量

  • 接口定义

  • 接口缓存

**性能分析工具:**MAT/TracView/LeakCanary/blockCanary/MemoryMonitor/HeapViewer

HashMap分析


  • 基础知识
  1. 可以接受null键和值,而Hashtable则不能

  2. 非synchronized,所以很快

  3. 存储的是键值对

  4. 使用数组+链表的方式存储数据

  5. 对key进行hash(散列)算法,所以顺序不固定

  6. 实际使用Node存储

  • 常量&变量

// public class HashMap extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable {}

/**

  • The default initial capacity - MUST be a power of two.

默认数组长度

*/

static final int DEFAULT_INITIAL_CAPACITY = 1 << 4;

/**

  • The maximum capacity, used if a higher value is implicitly specified

  • by either of the constructors with arguments.

  • MUST be a power of two <= 1<<30.

  • 数组最大长度

*/

static final int MAXIMUM_CAPACITY = 1 << 30;

/**

  • The load factor used when none specified in constructor.

  • 默认装填因子

*/

static final float DEFAULT_LOAD_FACTOR = 0.75f;

static class Node<K,V> implements Map.Entry<K,V> {

final int hash;

final K key;

V value;

Node<K,V> next;

}

/**

  • The number of key-value mappings contained in this map.

*/

transient int size;

/**

  • 阈值

  • The next size value at which to resize (capacity * load factor).

  • @serial

*/

// (The javadoc description is true upon serialization.

// Addit

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值