android - 平滑进度条动画

标签 android animation progress-bar

我正在尝试为我的 ProgressBar 实现流畅的动画,但是当我增加时间(30 秒)时,动画不再流畅。

以 5 秒为例:

5 seconds

以 30 秒为例:

30 seconds

我的进步背景:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <padding android:top="1dp" />
            <solid android:color="#10444444" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" />
            <solid android:color="#20444444" />
        </shape>
    </item>
    <item>
        <shape>
            <padding android:top="1dp" />
            <solid android:color="#30444444" />
        </shape>
    </item>
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="@color/black_thirty" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#3500D0" />
            </shape>
        </clip>
    </item>
</layer-list> 

我的进度布局:

<ProgressBar
    android:id="@+id/pb_loading"
    android:layout_width="match_parent"
    android:layout_height="8dp"
    android:indeterminate="false"
    android:layout_centerInParent="true"
    android:progress="100"
    android:progressDrawable="@drawable/my_progress_bar" />

我的动画方法:

private void startAnimation(){
    ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.pb_loading);
    ObjectAnimator progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 100, 0);
    progressAnimator.setDuration(30000);
    progressAnimator.setInterpolator(new LinearInterpolator());
    progressAnimator.start();
}

最佳答案

如果您每次将进度值更改 1(例如从 45 到 46),您将看不到动画。您最好将进度更改 100 点(或其他点),为此您只需将最大值乘以 100,并将每个进度值也乘以 100。例如:

    private void setProgressMax(ProgressBar pb, int max) {
        pb.setMax(max * 100);
    }

    private void setProgressAnimate(ProgressBar pb, int progressTo) 
    {
        ObjectAnimator animation = ObjectAnimator.ofInt(pb, "progress", pb.getProgress(), progressTo * 100);
        animation.setDuration(500);
        animator.setAutoCancel(true);
        animation.setInterpolator(new DecelerateInterpolator());
        animation.start();
    }

关于android - 平滑进度条动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30766755/

相关文章:

android - Kotlin - 在 android 中使用 Realm 模块

java - 像在纸板应用程序中一样平滑传感器数据

c# - 使用调度程序更新 WPF 进度条

android - 暂停/恢复 CountDownTimer Android

html - 如何动画圆形虚线边框?

javascript - 使用带有进度条和附加字段的核心 JavaScript 进行 Ajax 文件上传

带有 Kotlin 的 Android - 将数据传回上一个 Activity

java - 如何阻止使用 LayoutInflater().inflate(int resource, ViewGroup root, boolean AttachToRoot) 引发 IllegalStateExceptions?

iphone - 优化 Core Graphics 动画绘图 (iPhone)

javascript - react : Animating a page switch