當前位置:首頁 » 安卓系統 » android優化布局

android優化布局

發布時間: 2022-06-05 17:48:06

㈠ Android中常用的五種布局

Android
布局是應用界面開發的重要一環,在Android中,共有五種布局方式分別是:
線性布局:LinerLayout
表格布局:TableLayout
相對布局:RelativeLayout
絕對布局:AbsoluteLayout
幀布局:FrameLayout

㈡ 如何對Android進行性能優化

不知道你是說對系統優化還是什麼app優化,

系統優化就只能找底層人員的了,我也不是很了解。

app優化的話,大體有以下幾個方面

  1. ui優化,去除累贅的布局,優化初始化的速度,提高apk流暢性。

  2. 網路交互優化,好的網路和數據處理方式決定了app的體驗性能。

  3. 檢查內存是否有泄漏,人們常說的anr詳細。

如何你問的是android手機優化。

平常人只能下載手機管家這種軟體進行清除內存,垃圾,卸載無用的apk,保持android系統的流暢性。

㈢ 怎麼處理安卓布局才能使其在大小屏幕上都能正常顯示,有時在大屏手機上空白太多有時小屏幕又顯示不全,

我的手機桌面上所有的軟體或屏幕插件都可以在手機屏幕大小的允許下隨意調整大小。

㈣ 針對Android的性能優化集中哪些方面

一、概要:

本文主要以Android的渲染機制、UI優化、多線程的處理、緩存處理、電量優化以及代碼規范等幾方面來簡述Android的性能優化

二、渲染機制的優化:

大多數用戶感知到的卡頓等性能問題的最主要根源都是因為渲染性能。

Android系統每隔16ms發出VSYNC信號,觸發對UI進行渲染, 如果每次渲染都成功,這樣就能夠達到流暢的畫面所需要的60fps,為了能夠實現60fps,這意味著程序的大多數操作都必須在16ms內完成。

*關於JobScheler的更多知識可以參考http://hukai.me/android-training-course-in-chinese/background-jobs/scheling/index.html

七、代碼規范

1)for loop中不要聲明臨時變數,不到萬不得已不要在裡面寫try catch。

2)明白垃圾回收機制,避免頻繁GC,內存泄漏,OOM(有機會專門說)

3)合理使用數據類型,StringBuilder代替String,少用枚舉enum,少用父類聲明(List,Map)

4)如果你有頻繁的new線程,那最好通過線程池去execute它們,減少線程創建開銷。

5)你要知道單例的好處,並正確的使用它。

6)多用常量,少用顯式的"action_key",並維護一個常量類,別重復聲明這些常量。

7)如果可以,至少要弄懂設計模式中的策略模式,組合模式,裝飾模式,工廠模式,觀察者模式,這些能幫助你合理的解耦,即使需求頻繁變更,你也不用害怕牽一發而動全身。需求變更不可怕,可怕的是沒有在寫代碼之前做合理的設計。

8)View中設置緩存屬性.setDrawingCache為true.

9)cursor的使用。不過要注意管理好cursor,不要每次打開關閉cursor.因為打開關閉Cursor非常耗時。Cursor.require用於刷cursor.

10)採用SurfaceView在子線程刷新UI,避免手勢的處理和繪制在同一UI線程(普通View都這樣做)

11)採用JNI,將耗時間的處理放到c/c++層來處理

12)有些能用文件操作的,盡量採用文件操作,文件操作的速度比資料庫的操作要快10倍左右

13)懶載入和緩存機制。訪問網路的耗時操作啟動一個新線程來做,而不要再UI線程來做

14)如果方法用不到成員變數,可以把方法申明為static,性能會提高到15%到20%

15)避免使用getter/setter存取field,可以把field申明為public,直接訪問

16)私有內部類要訪問外部類的field或方法時,其成員變數不要用private,因為在編譯時會生成setter/getter,影響性能。可以把外部類的field或方法聲明為包訪問許可權

17)合理利用浮點數,浮點數比整型慢兩倍

18)針對ListView的性能優化,ListView的背景色與cacheColorHint設置相同顏色,可以提高滑動時的渲染性能。ListView中getView是性能是關鍵,這里要盡可能的優化。

getView方法中要重用view;getView方法中不能做復雜的邏輯計算,特別是資料庫操作,否則會嚴重影響滑動時的性能

19)不用new關鍵詞創建類的實例,用new關鍵詞創建類的實例時,構造函數鏈中的所有構造函數都會被自動調用。但如果一個對象實現了Cloneable介面,我們可以調用它的clone()方法。

clone()方法不會調用任何類構造函數。在使用設計模式(Design Pattern)的場合,如果用Factory模式創建對象,則改用clone()方法創建新的對象實例非常簡單。例如,下面是Factory模式的一個典型實現:

20)public static Credit getNewCredit() {
return new Credit();
}
改進後的代碼使用clone()方法,如下所示:
private static Credit BaseCredit = new Credit();
public static Credit getNewCredit() {
return (Credit) BaseCredit.clone();
}
上面的思路對於數組處理同樣很有用。

21)乘法和除法

考慮下面的代碼:

  • for (val = 0; val < 100000; val +=5) { alterX = val * 8; myResult = val * 2; }
    用移位操作替代乘法操作可以極大地提高性能。下面是修改後的代碼:
    for (val = 0; val < 100000; val += 5) { alterX = val << 3; myResult = val << 1; }

  • 22)ViewPager同時緩存page數最好為最小值3,如果過多,那麼第一次顯示時,ViewPager所初始化的pager就會很多,這樣pager累積渲染耗時就會增多,看起來就卡。

    23)每個pager應該只在顯示時才載入網路或資料庫(UserVisibleHint=true),最好不要預載入數據,以免造成浪費

    24)提高下載速度:要控制好同時下載的最大任務數,同時給InputStream再包一層緩沖流會更快(如BufferedInputStream)

    25)提供載入速度:讓服務端提供不同解析度的圖片才是最好的解決方案。還有合理使用內存緩存,使用開源的框架

    引用:Android性能優化的淺談

    ㈤ android 布局優化使用什麼標簽

    android 布局優化主要使用抽象布局標簽

    (1) <include>標簽
    include標簽常用於將布局中的公共部分提取出來供其他layout共用,以實現布局模塊化,這在布局編寫方便提供了大大的便利。
    下面以在一個布局main.xml中用include引入另一個布局foot.xml為例。main.mxl代碼如下:

    java<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
    android:id="@+id/simple_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="@dimen/dp_80" />

    <include layout="@layout/foot.xml" />

    </RelativeLayout>

    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@+id/simple_list_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/dp_80" /> <include layout="@layout/foot.xml" /> </RelativeLayout>
    其中include引入的foot.xml為公用的頁面底部,代碼如下:

    Java<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_40"
    android:layout_above="@+id/text"/>

    <TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="@dimen/dp_40"
    android:layout_alignParentBottom="true"
    android:text="@string/app_name" />

    </RelativeLayout>
    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="@dimen/dp_40" android:layout_above="@+id/text"/> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="@dimen/dp_40" android:layout_alignParentBottom="true" android:text="@string/app_name" /> </RelativeLayout>

    <include>標簽唯一需要的屬性是layout屬性,指定需要包含的布局文件。可以定義android:id和android:layout_*屬性來覆蓋被引入布局根節點的對應屬性值。注意重新定義android:id後,子布局的頂結點i就變化了。(2) <viewstub>標簽
    viewstub標簽同include標簽一樣可以用來引入一個外部布局,不同的是,viewstub引入的布局默認不會擴張,即既不會佔用顯示也不會佔用位置,從而在解析layout時節省cpu和內存。
    viewstub常用來引入那些默認不會顯示,只在特殊情況下顯示的布局,如進度布局、網路失敗顯示的刷新布局、信息出錯出現的提示布局等。
    下面以在一個布局main.xml中加入網路錯誤時的提示頁面network_error.xml為例。main.mxl代碼如下:

    Java<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    ……
    <ViewStub
    android:id="@+id/network_error_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout="@layout/network_error" />

    </RelativeLayout>
    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > …… <ViewStub android:id="@+id/network_error_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout="@layout/network_error" /> </RelativeLayout>

    其中network_error.xml為只有在網路錯誤時才需要顯示的布局,默認不會被解析,示例代碼如下:

    Java<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
    android:id="@+id/network_setting"
    android:layout_width="@dimen/dp_160"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/network_setting" />

    <Button
    android:id="@+id/network_refresh"
    android:layout_width="@dimen/dp_160"
    android:layout_height="wrap_content"
    android:layout_below="@+id/network_setting"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/dp_10"
    android:text="@string/network_refresh" />

    </RelativeLayout>
    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/network_setting" android:layout_width="@dimen/dp_160" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="@string/network_setting" /> <Button android:id="@+id/network_refresh" android:layout_width="@dimen/dp_160" android:layout_height="wrap_content" android:layout_below="@+id/network_setting" android:layout_centerHorizontal="true" android:layout_marginTop="@dimen/dp_10" android:text="@string/network_refresh" /> </RelativeLayout>

    在java中通過(ViewStub)findViewById(id)找到ViewStub,通過stub.inflate()展開ViewStub,然後得到子View,如下:

    Javaprivate View networkErrorView;

    private void showNetError() {
    // not repeated infalte
    if (networkErrorView != null) {
    networkErrorView.setVisibility(View.VISIBLE);
    return;
    }

    ViewStub stub = (ViewStub)findViewById(R.id.network_error_layout);
    networkErrorView = stub.inflate();
    Button networkSetting = (Button)networkErrorView.findViewById(R.id.network_setting);
    Button refresh = (Button)findViewById(R.id.network_refresh);
    }

    private void showNormal() {
    if (networkErrorView != null) {
    networkErrorView.setVisibility(View.GONE);
    }
    }
    private View networkErrorView; private void showNetError() { // not repeated infalte if (networkErrorView != null) { networkErrorView.setVisibility(View.VISIBLE); return; } ViewStub stub = (ViewStub)findViewById(R.id.network_error_layout); networkErrorView = stub.inflate(); Button networkSetting = (Button)networkErrorView.findViewById(R.id.network_setting); Button refresh = (Button)findViewById(R.id.network_refresh);} private void showNormal() { if (networkErrorView != null) { networkErrorView.setVisibility(View.GONE); }}

    在上面showNetError()中展開了ViewStub,同時我們對networkErrorView進行了保存,這樣下次不用繼續inflate。這就是後面第三部分提到的減少不必要的infalte。

    viewstub標簽大部分屬性同include標簽類似。上面展開ViewStub部分代碼

    JavaViewStub stub = (ViewStub)findViewById(R.id.network_error_layout);
    networkErrorView = stub.inflate();
    ViewStub stub = (ViewStub)findViewById(R.id.network_error_layout);networkErrorView = stub.inflate();
    也可以寫成下面的形式

    JavaView viewStub = findViewById(R.id.network_error_layout);
    viewStub.setVisibility(View.VISIBLE); // ViewStub被展開後的布局所替換
    networkErrorView = findViewById(R.id.network_error_layout); // 獲取展開後的布局
    View viewStub = findViewById(R.id.network_error_layout);viewStub.setVisibility(View.VISIBLE); // ViewStub被展開後的布局所替換networkErrorView = findViewById(R.id.network_error_layout); // 獲取展開後的布局
    效果一致,只是不用顯示的轉換為ViewStub。通過viewstub的原理我們可以知道將一個view設置為GONE不會被解析,從而提高layout解析速度,而VISIBLE和INVISIBLE這兩個可見性屬性會被正常解析。


    ㈥ 怎樣android的布局優化載入

    下面介紹幾種切實有效的方式:

    1. merge

      顧名思義,就是合並、融合的意思。使用它可以有效的將某些符合條件的多餘的層級優化掉。使用merge的場合主要有兩處:

      (1)自定義View中使用,父元素盡量是FrameLayout,當然如果父元素是其他布局,而且不是太復雜的情況下也是可以使用的

      (2)Activity中的整體布局,根元素需要是FrameLayout

    2. ViewStub

      (1)ViewStub只能Inflate一次,之後ViewStub對象會被置為空。按句話說,某個被ViewStub指定的布局被Inflate後,就不能夠再通過ViewStub來控制它了。所以它不適用 於需要按需顯示隱藏的情況。
      (2)ViewStub只能用來Inflate一個布局文件,而不是某個具體的View,當然也可以把View寫在某個布局文件中。如果想操作一個具體的view,還是使用visibility屬性吧。
      (3) VIewStub中不能嵌套merge標簽。(前面好像說過)
      不過這些確定都無傷大雅,我們還是能夠用ViewStub來做很多事情。

    3. include

      製造這個標簽純碎是為了布局重用,在項目中通常會存在一些布局公用的部分,比如自義標題,我們不需要把一份代碼Ctrl C, Ctrl V的到處都是,嚴重違背程序簡潔化、模塊兒化的設計思想

    熱點內容
    cad解壓錯誤 發布:2024-03-29 15:01:45 瀏覽:78
    存儲指令集 發布:2024-03-29 14:39:27 瀏覽:649
    資料庫表刪除數據 發布:2024-03-29 14:39:26 瀏覽:367
    出c語言整除 發布:2024-03-29 14:28:22 瀏覽:572
    芬尼壓縮機 發布:2024-03-29 14:24:11 瀏覽:464
    電腦數據實時上傳本地伺服器軟體 發布:2024-03-29 14:07:57 瀏覽:920
    尋秦記源碼 發布:2024-03-29 13:56:17 瀏覽:496
    linux的備份命令 發布:2024-03-29 13:41:22 瀏覽:383
    csgo建議什麼配置 發布:2024-03-29 13:31:44 瀏覽:980
    電腦ftp服務如何禁用 發布:2024-03-29 13:24:48 瀏覽:332