Android ListView優(yōu)化之提高android應(yīng)用效率
ListView是一個(gè)經(jīng)常用到的控件,ListView里面的每個(gè)子項(xiàng)Item可以使一個(gè)字符串,也可以是一個(gè)組合控件。Adapter是listview和數(shù)據(jù)源間的中間人。
當(dāng)每條數(shù)據(jù)進(jìn)入可見(jiàn)區(qū)域時(shí),adapter的getview()會(huì)被調(diào)用,返回代表具體數(shù)據(jù)的視圖。觸摸滾動(dòng)時(shí),頻繁調(diào)用。支持成百上千條數(shù)據(jù)。
下面為顯示每條數(shù)據(jù)的xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal"> <ImageView android:id="@+id/icon" android:layout_width="48dip" android:layout_height="48dip" /> <TextView android:id="@+id/text" android:layout_gravity="center_vertical" android:layout_width="0dip" android:layout_weight="1.0" android:layout_height="wrap_content" /> </LinearLayout>
1。最簡(jiǎn)單的方法,最慢且最不實(shí)用
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
背景和圖像
視圖背景圖像總會(huì)填充整個(gè)視圖區(qū)域
1。圖像尺寸不合適會(huì)導(dǎo)致自動(dòng)縮放
2。避免實(shí)時(shí)縮放
3。最好預(yù)先縮放到視圖大小
originalImage = Bitmap.createScaledBitmap( originalImage, // 縮放圖像 view.getWidth(), // 視圖寬度 view.getHeight(), // 視圖高度 true); // 線性過(guò)濾器
1的效率接近25 frames/second
2的效率接近50 frames/second
默認(rèn)情況下, 窗口有一個(gè)不透明的背景
有時(shí)可以不需要
-最高層的視圖是不透明的
- 最高層的視圖覆蓋整個(gè)窗口
layout_width = fill_parent layout_height = fill_parent
更新看不見(jiàn)的背景是浪費(fèi)時(shí)間
刪除窗口背景:
1。修改編碼
public void onCreate(Bundle icicle){ super.onCreate(icicle); setContentView(R.layout.mainview); // 刪除窗口背景 getWindow().setBackgroundDrawable(null); ... }
2。修改xml
首先確定你的res/values/styles.xml有
<resources> <style name="NoBackgroundTheme" parent="android:Theme"> <item name="android:windowBackground">@null</item> </style> </resources>
然后編輯androidmainfest.xml
<activity android:name="MyApplication" android:theme="@style/NoBackgroundTheme"> ... </activity>
更新請(qǐng)求
當(dāng)屏幕需要更新時(shí),調(diào)用invalidate()方法,簡(jiǎn)單方便,但是更新了整個(gè)視圖,代價(jià)太高。
最好先找到無(wú)效區(qū)域,然后調(diào)用
invalidate(Rect dirty); invalidate(int left, int top, int right, int bottom);
視圖和布局
如果一個(gè)窗口包含很多視圖,啟動(dòng)太慢,繪制時(shí)間長(zhǎng),用戶界面反應(yīng)速度很慢
解決方法:
1。使用textview的復(fù)合drawable減少層次
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello" android:drawableLeft="@drawable/icon"/>
2。使用viewstuf延遲展開(kāi)視圖
在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"/>
在需要展開(kāi)視圖時(shí),
findViewById(R.id.stub_import).setVisibility(View.VISIBLE); // 或者 View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
3。使用<merge>合并中間視圖
默認(rèn)情況下,布局文件的根作為一個(gè)節(jié)點(diǎn),加入到父視圖中,如果使用merge可以避免根節(jié)點(diǎn)
<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"/> <TextView android: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(Canvas canvas) { // 加入你的繪圖編碼 } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 計(jì)算視圖的尺寸 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) { // 計(jì)算子視圖的位置 child.layout(left, top, right, bottom); } } } }
內(nèi)存分配
在性能敏感的代碼里,避免創(chuàng)建java對(duì)象
1。測(cè)量 onmeasure()
2。布局onlayout()
3。繪圖 ondraw() dispatchdraw()
4。事件處理 ontouchevent() dispatchtouchevent()
5。adapter: getview() bindview()
強(qiáng)行限制(適用調(diào)試模式)
int prevLimit = -1; try { prevLimit = Debug.setAllocationLimit(0); // 執(zhí)行不分配內(nèi)存的代碼 } catch (dalvik.system.AllocationLimitError e) { // 如果代碼分配內(nèi)存, Java 虛擬機(jī)會(huì)拋出錯(cuò)誤 Log.e(LOGTAG, e); } finally { Debug.setAllocationLimit(prevLimit); }
管理好對(duì)象:
1。適用軟引用:內(nèi)存緩存的最佳選擇
2。適用弱引用:避免內(nèi)存泄露
內(nèi)存緩存:
private final HashMap<String, SoftReference<T>> mCache; public void put(String key, T value) { mCache.put(key, new SoftReference<T>(value)); } public T get(String key, ValueBuilder builder) { T value = null; SoftReferece<T> reference = mCache.get(key); if (reference != null) { value = reference.get();
相關(guān)文章
Android png透明圖片轉(zhuǎn)jpg時(shí)背景變黑的解決方法
這篇文章主要介紹了Android png透明圖片轉(zhuǎn)jpg時(shí)背景變黑的解決方法,需要的朋友可以參考下2017-12-12Android實(shí)現(xiàn)外部喚起應(yīng)用跳轉(zhuǎn)指定頁(yè)面的方法
這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)外部喚起應(yīng)用跳轉(zhuǎn)指定頁(yè)面的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12基于Android6.0實(shí)現(xiàn)彈出Window提示框
這篇文章主要為大家詳細(xì)介紹了基于Android6.0實(shí)現(xiàn)彈出Window提示框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Android中AlertDialog四種對(duì)話框的最科學(xué)編寫用法(實(shí)例代碼)
這篇文章主要介紹了Android中AlertDialog四種對(duì)話框的最科學(xué)編寫用法,本文通過(guò)代碼講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11Android AlertDialog對(duì)話框用法示例
這篇文章主要介紹了Android AlertDialog對(duì)話框用法,結(jié)合實(shí)例形式分析了AlertDialog對(duì)話框的功能及常見(jiàn)使用技巧,需要的朋友可以參考下2016-06-06android使用ViewPager實(shí)現(xiàn)圖片自動(dòng)切換
這篇文章主要為大家詳細(xì)介紹了android使用ViewPager實(shí)現(xiàn)圖片自動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02Android設(shè)備之間通過(guò)Wifi通信的示例代碼
本篇文章主要介紹了Android設(shè)備之間通過(guò)Wifi通信的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01