您的位置:首页 > 产品设计 > UI/UE

如何让Android UI使用更快更高效

2012-01-10 11:56 393 查看
之前有谈过如何使用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.使用合并视图

  默认情况下,布局文件的根作为一个借点加入到父视图中,如果使用可以避免根节点。

  如果最外层的布局是FrameLayout,那么可以使用merge替换掉,引用官方说明:

  Obviously, using 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.自定义布局
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: