一.item 的layout中尽量避免使用wrap_content ,可以使用固定的dp/dip,或者fill_parent/match_parent ; View在Draw的时候分成两个阶段:measure和layout,在measure阶段时主要就是为了计算两个参数:height和width。而且要注意的是,这是个递归的过程,从顶向下,DecorView开始依次调用自己子元素的measure。计算完成这两个参数后就开始layout,最后再是draw的调用。 wrap_content 要计算自己的大小,fill_parent填充父view给的大小,减少了计算。
对于ListView,当然每一个Item都会被调用measure方法,而在这个过程中getView和getCount会被调用,根据操作,可能会有很多次调用。
二.Doesn't show fading edges.
三.注意adapter中getview里面的缓存处理。利用convertview回收视图,效率提高200%。利用viewholder模式,效率在提高50%。
1. ListView需要设置adapter,它的item是通过adapter的方法getView(int position, View convertView, ViewGroup parent)获得的。
2. ListView中只有第一屏的item需要新建,它的引用会被存在RecycleBin对象内,在拖动时后面的item实际上是重从了之前创建的item。
3. 根据上述,ListView在需要显示item时,最开始第一屏时,getView(int position, View convertView, ViewGroupparent )的第二个参数为null,显示第二屏或者回滚显示第一屏时,getView(int position, View convertView, ViewGroupparent )第二个参数是一个原来缓存的item,我们只需要在getView中把它内部数据更新即可。
4. 如果item结构比较复杂,在更新一个已有的item内部数据的时候,查找item内部每一个元素也需要占用不少资源,所以,可以把这些内部元素的引用缓存起来,直接对其赋值,最有效的方法是把这些引用存到对应的item中,比较好的方法是使用setTag()方法。
据上四条,可得出:
1. 在adapter的getView(int position, View convertView, ViewGroup parent)方法中,每次都new一个新的View返回,是性能最差的,浪费了ListView巧妙设计的一片苦心。
2. 如果item内部元素简单,getView中如果传进来的view不为空,直接为其赋值即可。
3. 如果item内部元素复杂,可以使用Google IO 大会中讨论的方法优化(如下):
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
holder = new ViewHolder();
holder.icon1 = (ImageView) convertView.findViewById(R.id.icon1);
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
convertView.setTag(holder);
}
else{
holder = (ViewHolder)convertView.getTag();
}
holder.icon1.setImageResource(R.drawable.icon);
holder.text1.setText(mData[position]);
return convertView;
}
static class ViewHolder {
TextView text1;
ImageView icon1;
}
四.简化视图和布局
如果一个窗口包含很多视图,启动太慢,绘制时间长,用户界面反应速度很慢,解决方法:
1。使用textview的复合drawable减少层次
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello"
android:drawableLeft="@drawable/icon"/>
2。使用viewstuf延迟展开视图
在xml文件中定义viewstuf
<ViewStub android:id = "@+id/stub_import"
android:inflatedId="@+id/panel_import"
android:layout="@layout/progress_overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
在需要展开视图时,
findViewById(R.id.stub_import).setVisibility(View.VISIBLE);
// 或者
View importPanel = ((ViewStub)
findViewById(R.id.stub_import)).inflate();
3。使用<merge>合并中间视图
默认情况下,布局文件的根作为一个节点,加入到父视图中,如果使用merge可以避免根节点
<merge xmlns:android = "http://schemas.android.com/apk/res/android">
<! -- Content -->
</merge>
4。使用ralativelayout减少层次
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ImageView android:id="@+id/icon"
android:layout_width="48dip" android:layout_height="48dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/text_line1"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/icon"/>
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/text_line2"
android:layout_toRightOf="@id/icon"
android:layout_below="@id/text_line1"/>
<Checkbox android:id="@+id/star"
android:layout_width="48dip" android:layout_height="48dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
5.使用自定义视图
class CustomView extends View {
@Override
protected void onDraw(Canvascanvas) {
// 加入你的绘图编码
}
@Override
protected void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
// 计算视图的尺寸
setMeasuredDimension(widthSpecSize, heightSpecSize);
}
}
6 使用自定义布局
class GridLayout extends ViewGroup {
@Override
protected void onLayout(boolean changed, int l, int t,
int r, int b) {
final int count =getChildCount();
for (int i=0; i <count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() !=GONE) {
// 计算子视图的位置
child.layout(left, top, right, bottom);
}
}
}
}
五.更新看不见的背景是浪费时间,可设置相关控件背景为空
1。代码设置
public void onCreate(Bundleicicle){
super.onCreate(icicle);
setContentView(R.layout.mainview);
// 删除窗口背景
getWindow().setBackgroundDrawable(null);
...
}
2。Xml设置
首先确定你的res/values/styles.xml有
<resources>
<style name="NoBackgroundTheme" parent="android:Theme">
<itemname="android:windowBackground">@null</item>
</style>
</resources>
然后编辑androidmainfest.xml
<activity android:name="MyApplication"
android:theme="@style/NoBackgroundTheme">
...
</activity>
六.控制内存分配
在性能敏感的代码里,避免创建java对象:
1。测量 onmeasure()
2。布局onlayout()
3。绘图 ondraw() dispatchdraw()
4。事件处理 ontouchevent() dispatchtouchevent()
5。adapter: getview() bindview()
在调试模式下进行强行限制
int prevLimit = -1;
try {
prevLimit = Debug.setAllocationLimit(0);
// 执行不分配内存的代码
} catch (dalvik.system.AllocationLimitErrore) {
// 如果代码分配内存, Java 虚拟机会抛出错误
Log.e(LOGTAG, e);
} finally {
Debug.setAllocationLimit(prevLimit);
}
管理好对象:
1。适用软引用:内存缓存的最佳选择
2。适用弱引用:避免内存泄露
private final HashMap<String,SoftReference<T>> mCache;
public void put(Stringkey, T value) {
mCache.put(key, new SoftReference<T>(value));
}
public T get(String key, ValueBuilderbuilder) {
T value = null;
SoftReferece<T> reference = mCache.get(key);
if (reference != null) {
value = reference.get();
参考过几篇博文,自己也试用过的一些方法。特此整理,亦可参考http://edu.gamfe.com/tutor/d/30509.html等有样例测试的文章。