0

I have crated an activity with scrollView which holds several buttons and textviews. Before I add the DatePicker into my scrollView, the FPS can keep 60fps. But I don't understand why the FPS drops dramaticly when I added the DatePicker. And I have not find out other widget have such bad performance.

Why I am concerning about the performance of this DatePicker? Because I want to show this DatePicker in a popup window with popup animation. The animation jerking all the time because of the poor performance of the DatePicker. I really want improve it. Pease give me some hint about that.

Here is my xml file which I have added the DatePicker.

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

And here is how I init the 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);

GPU rendering profile without DatePicker over 60FPS(I cannot post this picture because of short of reputation) GPU rendering profile with DatePicker less than 60FPS

GPU rendering profile with DatePicker less than 60FPS

Hierarchy View analysis with DatePicker, measure more than 1ms, draw more than 15 ms, and of course it cannot reach 60FPS.

Hierarchy View analysis with DatePicker

And I think my phone is not that slow, as it could easily got 60 FPS when running the Palette example from support v7 example which holds many textviews and imageviews. Here is the screenshot.(I cannot post this picture because of short of reputation)

Chanson
  • 411
  • 4
  • 8

1 Answers1

0

Here comes the solution by myself

It is the fading edge effect that slow down the performance of the date picker. Detail information please refer to

Why are fading edges slow?

and

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

For using date picker with good performance, simply disable the fading edge effect would work.

android:fadingEdge="none"

For other widget which enable the fading edge effect, it will slow down the draw process.

How to make own custom fading edge effect?

Draw a fading edge drawable at your own view like this:

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);

The drawable will create a gradient from color white to transparent.

Community
  • 1
  • 1
Chanson
  • 411
  • 4
  • 8