平滑进度栏动画

rsi*_*lli 22 animation android progress-bar

我正在尝试为我实现平滑动画ProgressBar,但是当我增加时间(30秒)时,动画不再平滑.

5秒示例:

5秒

30秒示例:

30秒

我的进展背景:

<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> 
Run Code Online (Sandbox Code Playgroud)

我的进度布局:

<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" />
Run Code Online (Sandbox Code Playgroud)

我的动画方法:

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();
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*ell 28

因为你正在使用ofInt你只能以完整的整数移动.换句话说,如果您有一个宽度为1000且进度为0到100的进度条,因为您以整数速度移动,则计算1,2,3,4,这将转换为10px,20px,30px和40px.这解释了你所看到的锯齿状.

要纠正这个问题,您有几个选择.第一个是将整数从0增加到someBigInt这将为动画师提供更多数字.

ObjectAnimator progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 10000, 0);
Run Code Online (Sandbox Code Playgroud)

另一种选择是使用ofFloat与其相同ofInt但使用浮点而不是整数的选项.

ObjectAnimator progressAnimator = ObjectAnimator.ofFloat(mProgressBar, "progress", 100.0, 0.0);
Run Code Online (Sandbox Code Playgroud)

  • 我尝试过这个!但是,当我改为`ObjectAnimator.ofFloat(mProgressBar,"progress",100.0f,0.0f);`时,动画没有启动. (2认同)
  • 不工作,因为progressBar.setProgress(int)不浮动! (2认同)

Pav*_*kin 24

如果每次更改进度值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);
        animation.setInterpolator(new DecelerateInterpolator());
        animation.start();
    }
Run Code Online (Sandbox Code Playgroud)


Shw*_*rei 9

XML

       <ProgressBar
        android:id="@+id/progress_bar"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:indeterminate="false"
        android:progress="0"
        android:max="100"/>
Run Code Online (Sandbox Code Playgroud)

爪哇

@BindView(R.id.progress_bar) ProgressBar progressBar;
ObjectAnimator.ofInt(progressBar, "progress", 79).start();
Run Code Online (Sandbox Code Playgroud)

79

  • 对于此示例,介于 0 和 100 之间的任何数字


and*_*per 9

如果您有 Android N 及以上版本,则可以使用:

progressBar.setProgress(newProgress, true)
Run Code Online (Sandbox Code Playgroud)

文档:

将当前进度设置为指定值,可选择动画当前值和目标值之间的视觉位置。

动画不影响getProgress()的结果,调用该方法后会立即返回目标值。

https://developer.android.com/reference/android/widget/ProgressBar.html#setProgress(int,%20boolean)


Haw*_*ins 7

只是设置 android:max="1000" 和做 ObjectAnimator progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 1000, 0);

在这种情况下,您将在每个步骤上以1/1000为动画,在默认的100%比例下,在10倍的时间内平滑.它看起来好多了