android 由于性能不佳,日期选择器动画抖动

android datepicker animation jerking because of poor performance

我用 scrollView 创建了一个 activity,它包含多个按钮和文本视图。在我将 DatePicker 添加到我的 scrollView 之前,FPS 可以保持 60fps。但是我不明白为什么添加 DatePicker 后 FPS 会急剧下降。而且我还没有发现其他小部件具有如此糟糕的性能。

为什么我担心这个 DatePicker 的性能? 因为我想在带有弹出动画的弹出 window 中显示此 DatePicker。由于 DatePicker 的性能不佳,动画一直在抖动。我真的很想改进它。 Pease 给我一些提示。

这是我添加了 DatePicker 的 xml 文件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/common_white"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="test_db" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <Button
                    android:id="@+id/backup_database"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="backup_database" />
                <Button
                    android:id="@+id/restore_database"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="restore_database" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/test_result"
                android:text="hello_world"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <DatePicker
                android:id="@+id/test_picker"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></DatePicker>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

下面是我如何初始化 DatePicker

    Calendar today = Calendar.getInstance();
    int year = today.get(Calendar.YEAR);
    int month = today.get(Calendar.MONTH) + 1;
    int day = today.get(Calendar.DAY_OF_YEAR);
    DatePicker picker = (DatePicker) findViewById(R.id.test_picker);
    picker.setCalendarViewShown(false);
    picker.updateDate(year, month, day);

没有 DatePicker 的 GPU 渲染配置文件超过 60FPS(我不能 post 这张照片,因为信誉不足) DatePicker 的 GPU 渲染配置文件低于 60FPS

GPU rendering profile with DatePicker less than 60FPS

用DatePicker进行Hierarchy View分析,测量1ms以上,绘制15ms以上,当然达不到60FPS。

Hierarchy View analysis with DatePicker

而且我认为我的 phone 并没有那么慢,因为当 运行 来自包含许多文本视图和图像视图的支持 v7 示例的调色板示例时,它很容易达到 60 FPS。这是截图。(我不能post这张图片因为信誉不足)

我的解决方案来了

减慢日期选择器性能的是淡化边缘效应。详细信息请参考

Why are fading edges slow?

http://www.curious-creature.com/2008/12/22/why-is-my-list-black-an-android-optimization/

要使用性能良好的日期选择器,只需禁用淡化边缘效果即可。

android:fadingEdge="none"

对于其他启用淡化边缘效果的小部件,它会减慢绘制过程。

如何制作自定义渐变边缘效果?

像这样在您自己的视图中绘制可绘制的渐变边缘:

private final PaintDrawable mGradientDrawable;
private final ShapeDrawable.ShaderFactory mShaderFactory;

mShaderFactory = new ShapeDrawable.ShaderFactory() {
    @Override
    public Shader resize(int width, int height) {
        LinearGradient lg = new LinearGradient(0, 0, 0, height,
                new int[]{
                        0xffffffff,
                        0x00ffffff,
                        0x00ffffff,
                        0xffffffff,},
                new float[]{0, 0.3f, 0.5f, 1},
                Shader.TileMode.REPEAT);
        return lg;
    }
};
mGradientDrawable = new PaintDrawable();
mGradientDrawable.setShape(new RectShape());
mGradientDrawable.setShaderFactory(mShaderFactory);

可绘制对象将创建从白色到透明的渐变。