自己创建很容易
在您的布局中包含以下带有特定可绘制对象的ProgressBar(请注意,您应该改为从尺寸中获取宽度)。最大值在这里很重要:
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:max="500"
android:progress="0"
android:progressDrawable="@drawable/circular" />
现在使用以下形状在您的资源中创建可绘制对象。使用半径(您可以使用innerRadius 而不是innerRadiusRatio)和厚度值。
循环(棒棒糖前或 API 级别
<shape
android:innerRadiusRatio="2.3"
android:shape="ring"
android:thickness="3.8sp" >
<solid android:color="@color/yourColor" />
</shape>
循环(>= Lollipop OR API 级别 >= 21)
<shape
android:useLevel="true"
android:innerRadiusRatio="2.3"
android:shape="ring"
android:thickness="3.8sp" >
<solid android:color="@color/yourColor" />
</shape>
useLevel 在 API 级别 21 (Lollipop) 中默认为 "false"。
开始动画
接下来在您的代码中使用ObjectAnimator 为布局的ProgessBar 的进度字段设置动画。
ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", 0, 500); // see this max value coming back here, we animate towards that value
animation.setDuration(5000); // in milliseconds
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
停止动画
progressBar.clearAnimation();
附:与上面的示例不同,它提供了流畅的动画效果。