I'm trying to implement a smooth animation for my ProgressBar
, but when I increase the time (30 seconds), the animation is no longer smooth.
Example with 5 seconds:
Example with 30 seconds:
My progress background:
<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>
My progress layout:
<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" />
My animation method:
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();
}
Because you are using
ofInt
you can only move at full integers. In other words, if you have a progress bar with a width of 1000 and a progress of 0 to 100 since you are moving at an integer pace you count 1, 2, 3, 4 which translates to 10px, 20px, 30px and 40px. Which explains the jaggedness you are seeing.To correct this you have a few options. The first is to up your integers from 0 to
someBigInt
This will give the animator more numbers to work with.The other option is to use
ofFloat
which does the same thing asofInt
but uses floating points instead of integers.