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加载速度及减小内存消耗。
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); }}
更多相关文章
- Android构建音频播放器教程(一)
- Android(安卓)界面滑动实现---Scroller类 从源码和开发文档中学
- Android(安卓)Studio安卓学习笔记(三)Android用户界面的设计布局
- Android(安卓)API Guides---Layouts
- Android中Toast的常用使用方式总结
- 第3.1.3节 排布视图
- 【Android】ColorPickerDialog 自定义颜色选择器的实现
- 【凯子哥带你学Android】Andriod性能优化之列表卡顿——以“简书
- Android多屏幕适配及自适应解决方案