首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >平滑进度条动画

平滑进度条动画
EN

Stack Overflow用户
提问于 2015-06-11 04:32:53
回答 9查看 61.2K关注 0票数 33

我试图为我的ProgressBar实现一个平滑的动画,但是当我增加时间(30秒)时,动画就不再平滑了。

5秒的示例:

30秒的示例:

我的进度背景:

代码语言:javascript
运行
复制
<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> 

我的进度布局:

代码语言:javascript
运行
复制
<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" />

我的动画方法:

代码语言:javascript
运行
复制
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();
}
EN

回答 9

Stack Overflow用户

发布于 2016-07-06 17:19:57

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

代码语言:javascript
运行
复制
    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();
    }
票数 40
EN

Stack Overflow用户

发布于 2015-06-11 04:40:45

因为您使用的是ofInt,所以只能在全整数上移动。换句话说,如果你有一个宽度为1000的进度条,进度从0到100,因为你是以整数步速移动的,你会数到1,2,3,4,它可以转换为10px,20px,30px和40px。这就解释了你所看到的锯齿状。

要纠正这一点,您有几个选项。第一个是将你的整数从0增加到someBigInt,这会给动画师提供更多的数字。

代码语言:javascript
运行
复制
ObjectAnimator progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 10000, 0);

另一种选择是使用ofFloat,它与ofInt做同样的事情,但使用浮点而不是整数。

代码语言:javascript
运行
复制
ObjectAnimator progressAnimator = ObjectAnimator.ofFloat(mProgressBar, "progress", 100.0, 0.0);
票数 30
EN

Stack Overflow用户

发布于 2017-10-12 21:08:04

XML

代码语言:javascript
运行
复制
       <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"/>

JAVA

代码语言:javascript
运行
复制
@BindView(R.id.progress_bar) ProgressBar progressBar;
ObjectAnimator.ofInt(progressBar, "progress", 79).start();

79

对于此示例,

  • 为0到100之间的任意数字
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30766755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档