Android中如何提高UI的性能

本文主要探讨了如何优化Android应用的UI性能,包括将耗时操作移至后台线程、优化Adapter和AdapterView、简化布局文件、优化背景图以及使用ViewStub。建议使用AsynTask、ViewHolder、布局优化工具、减少布局层次,以及通过设置窗口背景和使用ViewStub来提高性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 


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加载速度及减小内存消耗。

        

public class DelayLoadActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.delay_load);

        Button btn = (Button) findViewById(android.R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setEnabled(false);
                ViewStub stub = (ViewStub) findViewById(R.id.stub);
                View inflated = stub.inflate();
            }});
    }

}

 

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

         需要使用的场景

                     界面需要频繁切换

                     希望能提高切换速度

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

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

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

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

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

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

fast_switch.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ViewStub android:id="@+id/portrait_subView"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout="@layout/portrait"
              android:inflatedId="@+id/portrait_view"/>
    <ViewStub android:id="@+id/landscape_subView"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout="@layout/landscape"
              android:inflatedId="@+id/landscape_view"/>
    

</LinearLayout>
 


public class MainActivity extends Activity {
	
	private static final String TAG="MainActivity";
	
    private ViewStub viewStubLandscape;
    private ViewStub viewStubPortrait;
    
    private View landscapeView;
    private View portraitView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fast_switch);
        viewStubLandscape=(ViewStub) findViewById(R.id.landscape_subView);
        viewStubPortrait=(ViewStub)findViewById(R.id.portrait_subView);
        Display display=getWindowManager().getDefaultDisplay();
        int orientation=display.getOrientation();
        Log.d(TAG, "orientation="+orientation);
        setOrientation(orientation);
    }
    
    public void setOrientation(int orientation){
    	switch(orientation){
    	case Configuration.ORIENTATION_LANDSCAPE:
    		if(landscapeView==null){
    			landscapeView=viewStubLandscape.inflate();
    			if(portraitView!=null){
    				portraitView.setVisibility(View.GONE);
    			}
    		}else{
    			if(portraitView!=null){
    				portraitView.setVisibility(View.GONE);
    			}
    			landscapeView.setVisibility(View.VISIBLE);
    		}
    		break;
    	default:
    		if(portraitView==null){
    			portraitView=viewStubPortrait.inflate();
    			if(landscapeView!=null){
    				landscapeView.setVisibility(View.GONE);
    			}
    		}else{
    			portraitView.setVisibility(View.VISIBLE);
    			if(landscapeView!=null){
    				landscapeView.setVisibility(View.GONE);
    			}
    		}
    		
    		break;
    	}

    }
    
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    	super.onConfigurationChanged(newConfig);
    	Log.d(TAG, "onConfiguration,orientation="+newConfig.orientation);
    	setOrientation(newConfig.orientation);
    }
}


 

         

 


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值