与4.0.3相比,应用UI操作(设置alpha,设置边距等)在4.4.4上非常慢

时间:2014-11-03 14:53:08

标签: android

我有一个更改ImageView alpha的搜索栏。这在使用android 4.0.3的旧HTC Desire 200上非常流畅。我试图在拥有android 4.4.4的摩托罗拉Moto G 2014上运行相同的代码。摩托罗拉手机的强度是HTC的3倍,但是当我使用搜索栏时,应用程序会断断续续。

问题不在于手机,在Antutu基准测试中,摩托罗拉手机获得了18000分,而HTC获得了6000分。

以下是搜索栏更改事件:

    int counter = 0;
    private void mainSeekBarProgreessChanges(SeekBar seekBar, int progress) {
        float a = progress / 100f;
        imgRight.setAlpha(a);

        ++counter;
        txtMsg.setText(Integer.toString(counter));
    }

口吃仅出现较大的图片(1280x720或更大)。小图片没有口吃。

当我使用搜索栏设置视图的边距时,也会出现断断续续的情况。

以下是该应用的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testprog1"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是活动:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.testprog1.MainActivity" >

    <FrameLayout
        android:id="@+id/layout_top"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/layout_footer"
        android:layout_alignParentLeft="false"
        android:layout_alignParentTop="true" >

        <ImageView
            android:id="@+id/img_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/start_image_left" />

        <ImageView
            android:id="@+id/img_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/start_image_right" />
    </FrameLayout>

    <LinearLayout
        android:id="@+id/layout_footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="false"
        android:orientation="vertical" >

        <SeekBar
            android:id="@+id/seekbar_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="100" />

        <LinearLayout
            android:id="@+id/layout_line1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btn_main"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />

            <TextView
                android:id="@+id/txt_msg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

更新: 如果我在清单中禁用硬件加速,那么性能会有所改善,但仍有明显的断断续续。

更新2: 这是我使用搜索栏设置ImageViews边距时的屏幕截图: When setting margins

这是一个截图,我用setImageAlpha设置了ImageViews的alpha: enter image description here

以下是设置边距的搜索栏更改事件中调用的代码:

FrameLayout.LayoutParams layoutParamLeft = (LayoutParams) layoutLeft.getLayoutParams();
layoutParamLeft.setMargins(layoutParamLeft.leftMargin, layoutParamLeft.topMargin, seekBar.getMax() - progress, layoutParamLeft.bottomMargin);
layoutLeft.setLayoutParams(layoutParamLeft);
layoutLeft.invalidate();

FrameLayout.LayoutParams layoutParamRight = (LayoutParams) layoutRight.getLayoutParams();
layoutParamRight.setMargins(progress, layoutParamRight.topMargin, layoutParamRight.rightMargin, layoutParamRight.bottomMargin);
layoutRight.setLayoutParams(layoutParamRight);
layoutRight.invalidate();

3 个答案:

答案 0 :(得分:3)

众所周知,存在与setAlpha()相关的性能问题。例如见:

由于它是ImageView小部件,您是否尝试过使用setImageAlpha()(API级别16+)?

在Nexus 5(使用Android 5.0)中测试您的示例我得到:

  • setAlpha() - ~45ms
  • setImageAlpha() - ~5ms

但两种方式都没有明显的口吃(并且使SeekBar完全平滑)。

答案 1 :(得分:1)

我的用例是淡出视图的背景。我保留了对视图背景可绘制的引用并使用其Drawable.setAlpha()方法。

我的参考手机(Galaxy S3)与使用View.setAlpha(0-1)(速度较慢的球)和ImageView.setImageAlpha(0-255)的速度非常快,而且{{1}}似乎没有任何影响。< / p>

答案 2 :(得分:0)

我想告诉你这个问题的答案:

ViewCompat.setLayerType(view, LAYER_TYPE_HARDWARE, null)

参考:Setting view alpha in runtime is slow, how to speed it up?

所有信用均属于Nikola Despotoski