57

I did some digging in Android code, and saw the use of in the indeterminate progress bar. after trying to create my own drawable with this tag:

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_pia"
    android:pivotX="50%"
    android:pivotY="50%"
    android:framesCount="12"
    android:frameDuration="100" />

I get an error: "No resource identifier found for attribute 'frameDuration' in package 'android'" - which means that frameDuration is a private attribute. Is there a way to use this "animate-rotate" feature?

My task is to replace the system's default indeterminate progress bar. I'd like to do it with as little code as possible (just change few attributes if possible). Using the ProgressBar view, setting:

android:indeterminateOnly="true"
android:indeterminateBehavior="cycle"
android:indeterminateDuration="3500"
android:indeterminateDrawable="@drawable/pia_sivuvator"

and point "@drawable/pia_sivuvator" to that object would've make my task as elegant as they come but I'm stuck on those private attributes.

help?

gkrogers
  • 8,126
  • 3
  • 29
  • 36
oriharel
  • 10,418
  • 14
  • 48
  • 57
  • 1
    Have the same problem. Excluding parameters (`framesCount` and `frameDuration`) does not helps much. Animation works, but does not look good for me (animation is not smooth, like a low framerate). Created an issue on this problem http://code.google.com/p/android/issues/detail?id=19248 – Andrei Lupsa Aug 14 '11 at 18:35

5 Answers5

62

I ran into the exact same issue. You can exclude those parameters (framesCount and frameDuration), and it may work for you. I tried just excluding them and it animated fine, but the width/height I was setting were not being respected, so I ended up creating a simple rotation animation and an ImageView to apply it to. Here's the animation file (res/anim/clockwise_rotation.xml):

<?xml version="1.0" encoding="utf-8"?>
<rotate
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromDegrees="0"
  android:interpolator="@android:anim/linear_interpolator"
  android:toDegrees="360"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="1000"
  android:startOffset="0"
/>

Then you just inflate your Animation, set repeat count, and start it from the View

Animation rotation = AnimationUtils.loadAnimation(this, R.anim.clockwise_rotation);
rotation.setRepeatCount(Animation.INFINITE);
myView.startAnimation(rotation);
Ian G. Clifton
  • 9,349
  • 2
  • 33
  • 34
  • 28
    use `android:repeatCount="infinite"` in the xml instead of hard code – pleerock Oct 16 '12 at 04:44
  • if you want to use framesCount parameter see http://stackoverflow.com/questions/3760381/rotating-image-animation-list-or-animated-rotate-android/14996762#14996762 – vokilam Feb 21 '13 at 07:34
11

Instead of creating an animation (more code required, not only XML configuration), use layer-list as drawable resource. It is quite interesting that layer-list is way more fluid than animated-rotate.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <rotate
        android:drawable="@drawable/spinner_loading"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="360"/>
</item>
</layer-list>

Then of course use it in the styles as Mario Lenci wrote:

<style name="YourProgressBarStyle" parent="@android:style/Widget.ProgressBar">
    <item name="android:indeterminateDrawable">@drawable/progress_bar_indeterminate</item>
</style>
android developer
  • 114,585
  • 152
  • 739
  • 1,270
rwozniak
  • 1,326
  • 15
  • 15
9

I don't know how to work around the private attributes, I have the same problem.

By the way if you want to change those attributes of the ProgressBar:

android:indeterminateOnly="true"
android:indeterminateBehavior="cycle"
android:indeterminateDuration="3500"
android:indeterminateDrawable="@drawable/pia_sivuvator"

you can do it easily with the Styles framework defining in the values/styles.xml file a ProgressBar style extending the standard android one:

<style name="YourProgressBarStyle" parent="@android:style/Widget.ProgressBar">
        <item name="android:indeterminateDrawable">@drawable/progress_bar_indeterminate</item>
</style>

and then applying it to the progress bar in the xml layout file.

...
<ProgressBar
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     style="@style/YourProgressBarStyle"/>
...
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
Mario Lenci
  • 10,422
  • 5
  • 39
  • 50
5

I solved this by using this drawable xml. Although it only seems to be smooth on newer versions of Android:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_pia"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="1080" />
Intrications
  • 16,782
  • 9
  • 50
  • 50
  • Can you please explain what you do from this RotateDrawable? http://stackoverflow.com/questions/5872257/how-do-i-use-rotatedrawable – rds Oct 31 '12 at 21:26
4

Here is the Simple Explanation of the Rotation Animation try this this will help you

http://androidtutorials60.blogspot.in/2013/09/simple-rotate-animation-in-android.html

<rotate xmlns:android="”http://schemas.android.com/apk/res/android”">
  android:duration="4000"
  android:fromdegrees="0"
  android:pivotx="50%"
  android:pivoty="50%"
  android:todegrees="360"
  android:toyscale="0.0"
</rotate>
Slampy
  • 326
  • 1
  • 7
Bhushan Shirsath
  • 258
  • 3
  • 12