Android高级编程

一、提高UI性能

1、提高Adapter和AdapterView的效率

重用已生成的Item View

添加ViewHolder,避免重复查找需要修改的控件

缓存Item数据

分段现实

实例:

class OptAdapter extends BaseAdapter{

	List<String> list;
	LayoutInflater inflater;
	ViewHolder viewHolder;
        ...
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null){
			convertView = inflater.inflate(R.layout.optitem, parent, false);
			TextView textView1 = (TextView) convertView.findViewById(R.id.tv1);
			TextView textView2 = (TextView) convertView.findViewById(R.id.tv2);
			viewHolder = new ViewHolder();
			viewHolder.setTextView1(textView1);
			viewHolder.setTextView2(textView2);
			viewHolder.setString(list.get(position).toString());
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
			viewHolder.setString(list.get(position).toString());
		}
		viewHolder.getTextView1().setText(viewHolder.getString());
		viewHolder.getTextView2().setText(viewHolder.getString());
		return convertView;
	}
	
	public static class ViewHolder{
		TextView textView1,textView2;
		String string;
		public TextView getTextView1() {
			return textView1;
		}
		public void setTextView1(TextView textView1) {
			this.textView1 = textView1;
		}
		public TextView getTextView2() {
			return textView2;
		}
		public void setTextView2(TextView textView2) {
			this.textView2 = textView2;
		}
		public String getString() {
			return string;
		}
		public void setString(String string) {
			this.string = string;
		}
		
	}
	
}

2、布局优化

使用观察布局工具 Hierarchy View

使用布局优化工具 lint

优化布局的层次结构

3、优化背景图

动态设置getWindow.setBackgroundDrawableResource(id)

静态设置,自定义主题

res/values/theme.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Theme.testTheme" parent="android:Theme">
        <item name="android:windowBackground">@drawable/ic_launcher</item>
    </style>
</resources>
调用android:theme = "@style/Theme.testTheme"

4、使用ViewStub

延迟加载不常用控件

提高改变布局速度

使用场景

界面频繁切换

界面高速切换

使用方法


你可能感兴趣的:(Android高级编程)