关于Android UI 使用更快更高效方法

作者:简简单单 2012-01-06

一、选择恰当的图像尺寸

视图背景图总是会填充整个视图区域,图像尺寸的不适合会导致图像的自动缩放,为了避免这种情况,我们可以先将图片进行缩放到视图的大小。

 代码如下 复制代码

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里有

 代码如下 复制代码


 

然后在你的manifest.xml里声明


 ......

三、尽可能的使用简单的布局和视图

如果一个窗口包含很多的视图,那么启动时间长、测量时间长、绘制时间长、布局时间长;

如果视图树深度太深,会导致StackOverflowException异常,和用户界面反映会很慢很慢。
解决的方法:

1.使用TextView的复合drawables,减少层次

如有这样的布局:

 代码如下 复制代码

    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
            android:layout_height="wrap_content" android:text="@string/hello" />
            android:layout_height="wrap_content" android:id="@+id/image" android:background="@drawable/icon" />


我们可以这样来取代他,从而来将少层次:

 代码如下 复制代码

        android:layout_height="wrap_content" android:text="@string/hello" android:drawableRight="@drawable/icon"/>

2.使用ViewStub延迟展开视图

默认情况下,使用ViewStub包含的视图是不可见的。

 代码如下 复制代码

这个里面包含的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.

 代码如下 复制代码

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


4.使用RelativeLayout减少层次

5.自定义布局

相关文章

精彩推荐