簡體   English   中英

由於性能不佳,android datepicker動畫抽搐

[英]android datepicker animation jerking because of poor performance

我用scrollView創建了一個活動,該活動包含幾個按鈕和textviews。 在將DatePicker添加到scrollView之前,FPS可以保持60fps。 但是我不明白為什么添加DatePicker時FPS會急劇下降。 而且我還沒有發現其他小部件具有如此糟糕的性能。

為什么我擔心此DatePicker的性能? 因為我想在帶有彈出動畫的彈出窗口中顯示此DatePicker。 由於DatePicker的性能不佳,動畫一直都在晃動。 我真的很想改善它。 請給我一些暗示。

這是我添加了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超過60FPS的GPU渲染配置文件(由於信譽不足,我無法發布此圖片)具有DatePicker不到60FPS的GPU渲染配置文件

DatePicker小於60FPS的GPU渲染配置文件

使用DatePicker進行層次視圖分析,測量時間超過1毫秒,繪制時間超過15毫秒,當然不能達到60FPS。

使用DatePicker進行層次結構視圖分析

而且我認為我的手機並沒有那么慢,因為從支持很多文本視圖和圖像視圖的支持v7示例運行Palette示例時,它很容易獲得60 FPS。 這是屏幕截圖。(由於信譽欠佳,我無法發布此圖片)

這是我自己的解決方案

漸隱邊緣效應會減慢日期選擇器的性能。 詳細信息請參考

為什么衰落的邊緣變慢?

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

可繪制對象將創建從白色到透明的漸變。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM