Android中如何提高UI的性能

1.减小主线程的阻塞时间

    若一个操作耗时教长(超过5秒 用户无响应5秒 网络和数据库阻塞10秒 广播接收者执行超过10秒会导致ANR),我们应该将其放入后台线程中执行,只在需要修改UI界面时通知主线程进行修改。

    Android已经提供了AsynTask以实现从主线程生成新的异步任务的方法。具体用法参见异步任务。

2.提高Adapter和AdapterView的效率

    (1)重用已生成过的Item View

    (2) 添加ViewHolder

    (3) 缓存Item的数据

    (4)分段显示

3.优化布局文件

   如果我们的布局层次过多,那么在我们用findViewById的时间必然会变多,一个变多可能不要紧,但是有很多调用必然会影响性能。

   (1) 使用观察布局的工具 Hierarchy Viewer

   (2)使用布局优化工具: Layoutopt

   (3)优化布局的层次结构

4.背景图

  某些时候,我们可能希望能够尽可能多的提高Activity哪怕一点点的性能,这时候我们可以考虑优化Activity背景图。

  首先我们必须知道,在android的Activity中,不止有你使用的setContentView时使用的View,还包含一些其它的View。其根View是一个DecorView,你设置的View就包含在其中,id为content。

   (1)使用getWindow().setBackgroundDrawable()

   (2)自定义主题

       创建文件res/vlaues/theme.xml

      <resources>

         <style name="Theme.CustomBackground" parent="android:Theme">

                   <item name="android:windowBackground">@null</item>

         </style>

      </resources>

     可根据需要将windowBackground设置为null或你需要的背景图

    2.在<activity/>或者<application/>标签中添加 android:theme="@style/Theme.CustomBackground"

5.使用ViewStub

    ViewStub是一个看不见,轻量级的View。它没有尺寸,也不会绘制以及以某种形式参与到布局中来。当只有调用Inflate之后其中的View才会被实例化,这意味着用ViewStub保留View层次结构代价是

   (1) 延迟加载不常用的UI控件

         当某些控件只在很少情况下才会使用,我们可以使用ViewStub来延迟加载,以提高UI加载速度及减小内存消耗。

        

[java]  view plain copy
  1. <span style="font-size:18px;">public class DelayLoadActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.delay_load);  
  7.   
  8.         Button btn = (Button) findViewById(android.R.id.button1);  
  9.         btn.setOnClickListener(new OnClickListener() {  
  10.             @Override  
  11.             public void onClick(View v) {  
  12.                 v.setEnabled(false);  
  13.                 ViewStub stub = (ViewStub) findViewById(R.id.stub);  
  14.                 View inflated = stub.inflate();  
  15.             }});  
  16.     }  
  17.   
  18. }  
  19. </span>  

 

   (2)提高改变布局的速度

         需要使用的场景

                     界面需要频繁切换

                     希望能提高切换速度

        使用方法(以横竖换屏为例)

                  1.设置Activity的android:configChanges属性为keyboardHidden|orientation

                  2.为横竖屏分别编写不同的layout

                  3.创建一个layout,并包含两个ViewStub(分别对应横竖屏)

                  4.在横竖屏,通过调用ViewStub.inflate创建当前View并将另外一个设为GONE

                  5.绑定并设置控件的状态

fast_switch.xml

 

[html]  view plain copy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <ViewStub android:id="@+id/portrait_subView"  
  7.               android:layout_width="fill_parent"  
  8.               android:layout_height="fill_parent"  
  9.               android:layout="@layout/portrait"  
  10.               android:inflatedId="@+id/portrait_view"/>  
  11.     <ViewStub android:id="@+id/landscape_subView"  
  12.               android:layout_width="fill_parent"  
  13.               android:layout_height="fill_parent"  
  14.               android:layout="@layout/landscape"  
  15.               android:inflatedId="@+id/landscape_view"/>  
  16.       
  17.   
  18. </LinearLayout></span>  
[html]  view plain copy
  1. <pre class="html" name="code"><span style="font-size:18px;"></span> </pre><br>  
  2. <pre></pre>  
  3. <pre class="html" name="code"><span style="font-size:18px;">public class MainActivity extends Activity {  
  4.       
  5.     private static final String TAG="MainActivity";  
  6.       
  7.     private ViewStub viewStubLandscape;  
  8.     private ViewStub viewStubPortrait;  
  9.       
  10.     private View landscapeView;  
  11.     private View portraitView;  
  12.       
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.fast_switch);  
  17.         viewStubLandscape=(ViewStub) findViewById(R.id.landscape_subView);  
  18.         viewStubPortrait=(ViewStub)findViewById(R.id.portrait_subView);  
  19.         Display display=getWindowManager().getDefaultDisplay();  
  20.         int orientation=display.getOrientation();  
  21.         Log.d(TAG, "orientation="+orientation);  
  22.         setOrientation(orientation);  
  23.     }  
  24.       
  25.     public void setOrientation(int orientation){  
  26.         switch(orientation){  
  27.         case Configuration.ORIENTATION_LANDSCAPE:  
  28.             if(landscapeView==null){  
  29.                 landscapeView=viewStubLandscape.inflate();  
  30.                 if(portraitView!=null){  
  31.                     portraitView.setVisibility(View.GONE);  
  32.                 }  
  33.             }else{  
  34.                 if(portraitView!=null){  
  35.                     portraitView.setVisibility(View.GONE);  
  36.                 }  
  37.                 landscapeView.setVisibility(View.VISIBLE);  
  38.             }  
  39.             break;  
  40.         default:  
  41.             if(portraitView==null){  
  42.                 portraitView=viewStubPortrait.inflate();  
  43.                 if(landscapeView!=null){  
  44.                     landscapeView.setVisibility(View.GONE);  
  45.                 }  
  46.             }else{  
  47.                 portraitView.setVisibility(View.VISIBLE);  
  48.                 if(landscapeView!=null){  
  49.                     landscapeView.setVisibility(View.GONE);  
  50.                 }  
  51.             }  
  52.               
  53.             break;  
  54.         }  
  55.   
  56.     }  
  57.       
  58.     @Override  
  59.     public void onConfigurationChanged(Configuration newConfig) {  
  60.         super.onConfigurationChanged(newConfig);  
  61.         Log.d(TAG, "onConfiguration,orientation="+newConfig.orientation);  
  62.         setOrientation(newConfig.orientation);  
  63.     }  
  64. }</span></pre>  
  65. <p><br>  
  66.  </p>  
  67. <p><span style="font-size:18px">          </span></p>  
  68. <p><span style="font-size:18px"></span> </p>  
  69. <pre></pre>  

你可能感兴趣的:(Android中如何提高UI的性能)