1. 程式人生 > >ListView載入速度/效能優化方案分

ListView載入速度/效能優化方案分

Adapter是listview和資料來源間的中間人.

當每條資料進入可見區域時,adapter的getview()會被呼叫,返回代表具體資料的檢視.觸控滾動時,頻繁呼叫.支援成百上千條資料.

下面為顯示每條資料的xml檔案:
1.最簡單的方法,最慢且最不實用

public View getView(int pos, View convertView,
ViewGroup parent){
View item = mInflater.inflate(R.layout.list_item, null);
((TextView) item.findViewById(R.id.text)).
setText(DATA[pos]);
((ImageView) item.findViewButId(R.id.icon)).
setImageBitmap((pos & 1) == 1 ? mIcon1 : mIcon2);
return item;
}

2.利用convertview回收檢視,效率提高200%.

public View getView(int pos, View convertView,
ViewGroup parent){
if (convertView == null) {
convertView = mInflater.inflate(
R.layout.list_item, null);
}
((TextView) convertView.findViewById(R.id.text)).
setText(DATA[pos]);
((ImageView) convertView.findViewButId(R.id.icon)).
setImageBitmap((pos & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}

3.利用viewholder模式,效率在提高50%

static class ViewHolder {
TextView text;
ImageView icon;
}

public View getView(int pos, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(
R.id.text));
holder.icon = (ImageView) convertView.findViewButId(
R.id.icon));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DATA[pos]);
holder.icon.setImageBitmap((pos & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}

adapter更新效率比較:

1的更新不到10 frames/second

2的更新接近30 frames/second

3的更新接近40 frames/second

背景和影象

檢視背景影象總會填充整個檢視區域

1.影象尺寸不合適會導致自動縮放

2.避免實時縮放

3.最好預先縮放到檢視大小

originalImage = Bitmap.createScaledBitmap(
originalImage, // 縮放影象
view.getWidth(), // 檢視寬度
view.getHeight(), // 檢視高度
true); // 線性過濾器

1的效率接近25 frames/second

2的效率接近50 frames/second

預設情況下, 視窗有一個不透明的背景

有時可以不需要

-最高層的檢視是不透明的

-最高層的檢視覆蓋整個視窗

layout_width = fill_parent
layout_height = fill_parent

更新看不見的背景是浪費時間

刪除視窗背景:

1.修改編碼

public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.mainview);
// 刪除視窗背景
getWindow().setBackgroundDrawable(null);
...

2.修改xml

首先確定你的res/values/styles.xml有

然後編輯androidmainfest.xml

...

更新請求

當螢幕需要更新時,呼叫invalidate()方法,簡單方便,但是更新了整個檢視,代價太高.

最好先找到無效區域,然後呼叫

invalidate(Rect dirty);
invalidate(int left, int top, int right, int bottom);

檢視和佈局

如果一個視窗包含很多檢視,啟動太慢,繪製時間長,使用者介面反應速度很慢

解決方法:

1.使用textview的複合drawable減少層次

2.使用viewstuf延遲展開檢視

在xml檔案中定義viewstuf

在需要展開檢視時,

findViewById(R.id.stub_import).setVisibility(View.VISIBLE);
// 或者
View importPanel = ((ViewStub)
findViewById(R.id.stub_import)).inflate();

3.使用合併中間檢視

預設情況下,佈局檔案的根作為一個節點,加入到父檢視中,如果使用merge可以避免根節點
4.使用ralativelayout減少層次

5.使用自定義檢視

class CustomView extends View {
@Override
protected void onDraw(Canvas canvas) {
// 加入你的繪圖編碼
}
@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);
}
}
}

記憶體分配

在效能敏感的程式碼裡,避免建立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.AllocationLimitError e) {
// 如果程式碼分配記憶體, Java 虛擬機器會丟擲錯誤
Log.e(LOGTAG, e);
} finally {
Debug.setAllocationLimit(prevLimit);
}

管理好物件:

1.適用軟引用:記憶體快取的最佳選擇

2.適用弱引用:避免記憶體洩露

記憶體快取:

private final HashMap> mCache;
public void put(String key, T value) {
mCache.put(key, new SoftReference(value));
}
public T get(String key, ValueBuilder builder) {
T value = null;
SoftReferece reference = mCache.get(key);
if (reference != null) {
value = reference.get();