本文標籤:
ListView載入速度最佳化 ,
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();
聲明: 本文由( 魔豆先生 )原創編譯,轉載請保留連結:
ListView載入速度/效能最佳化方案分