Android UI 使用更快更高效

來源:互聯網
上載者:User
Android UI 使用更快更高效

之前有談過如何使用adapter更高效的,現在在談談其他的。

一、選擇恰當的映像尺寸

  視圖背景圖總是會填充整個視圖地區,映像尺寸的不適合會導致映像的自動縮放,為了避免這種情況,我們可以先將圖片進行縮放到視圖的大小。

originalImage = Bitmap.createScaledBitmap(originalImage, //被縮放圖view.getWidth(), //視圖寬度view.getHright(), //視圖高度true //雙限行過濾器);

二、去掉不需要的預設視窗背景

  在預設情況下,視窗有一個不透明的背景,有時候我們並不需要他,就可以去掉他。因為更新看不見的視窗是浪費時間的。

去掉的方法:

1.代碼實現:

@Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //刪除視窗背景        getWindow().setBackgroundDrawable(null);    }

2.xml裡實現:

首先去頂你的res/xml/styles.xml裡有

<resources>  <style name="NoBackGroundTheme" parent="android:Theme">       <item name="android:windowBackground">@null</item></style></resources>

然後在你的manifest.xml裡聲明

<activity android:name="MyActivity" android:theme="@style/NoBackGroundTheme"> ......</activity>

三、儘可能的使用簡單的布局和視圖

  如果一個視窗包含很多的視圖,那麼啟動時間長、測量時間長、繪製時間長、布局時間長;

  如果視圖樹深度太深,會導致StackOverflowException異常,和使用者介面反映會很慢很慢。
解決的方法:

1.使用TextView的複合drawables,減少層次

如有這樣的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal" android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TextView android:layout_width="fill_parent"        android:layout_height="wrap_content" android:text="@string/hello" />    <Image android:layout_width="wrap_content"        android:layout_height="wrap_content" android:id="@+id/image" android:background="@drawable/icon" /></LinearLayout>

我們可以這樣來取代他,從而來將少層次:

<TextView android:layout_width="fill_parent"        android:layout_height="wrap_content" android:text="@string/hello" android:drawableRight="@drawable/icon"/>

2.使用ViewStub延遲展開視圖

預設情況下,使用ViewStub包含的視圖是不可見的。

<ViewStub android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/vs" android:layout="@layout/main"/>

這個裡麵包含的main視圖是不會展現出來的,如果需要展現出來需要代碼的處理

findViewById(R.id.vs).setVisibility(View.VISIBLE);

或者

findViewById(R.id.vs).inflate();

3.使用<merge>合并視圖
  預設情況下,布局檔案的根作為一個借點加入到父視圖中,如果使用<merge>可以避免根節點。

  如果最外層的布局是FrameLayout,那麼可以使用merge替換掉,引用官方說明:

Obviously, using <merge /> works in this case because the parent of an activity's content view is always a FrameLayout. You could not apply this trick if your layout was using a LinearLayout as its root tag for instance.

<merge  xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent">    .....</merge>

4.使用RelativeLayout減少層次

5.自訂布局

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.