Android animation does not repeat
Update: Back in Sep, 2011 an Android engineer fixed this issue for the most part. The attributes that were ignored in XML now work, with the exception of repeatCount
and fillEnabled
which are still ignored (on purpose for some reason). This means it still isn't easy to repeat an AnimationSet
unfortunately.
For details please see the overview in the updated docs (explains which attributes are ignored, which work, and which are passed onto children). And for a deeper understanding of what fillAfter
, fillBefore
, and fillEnabled
actually do, see the engineer's (Chet Haase) blog post about it here.
Original Answer
To expand upon answers by Pavel and others: it is true that the <set>
tag is ridiculously buggy. It can't deal correctly with repeatCount
and a number of other attributes.
I spent a few hours figuring out what it can and can't deal with and have submitted a bug report/issue here: Issue 17662
In summary (this concerns AnimationSet
s):
setRepeatCount() / android:repeatCount
This attribute (as well as repeatMode) does not work in code or XML. This makes repeating an entire set of animations difficult.
setDuration() / android:duration
Setting this on an AnimationSet in code WORKS (overrides all durations of children animations), but not when included in the tag in XML
setFillAfter() / android:fillAfter
This works in both code and XML for the tag. Strangely I have gotten it to also work without the need to set fillEnabled to true.
setFillBefore() / android:fillBefore
Seems to have no effect/ignored in both code and XML
setFillEnabled() / android:fillEnabled
Seems to have no effect/ignored in both code and XML. I can still get fillAfter to work even without including fillEnabled or setting fillEnabled to false.
setStartOffset() / android:startOffset
This works only in code and not XML.
Android Animation doesn't repeat
this code works for me
RotateAnimation rotate = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setRepeatCount(Animation.INFINITE);
rotate.setRepeatMode(Animation.INFINITE);
rotate.setDuration(500);
imageView.startAnimation(rotate);
Android TextView Animation Infinite Repeat Not Working
Try this in the XML code. The java code did not work and I solved it well with this. Just apply this in your code:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<rotate
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="restart"/>
</set>
Add the last two lines of <rotate>
inside your code. In your case, inside the two components <translate>
and remove the equivalent Java code:
animation.setRepeatMode(Animation.INFINITE);
animation.setRepeatCount(Animation.INFINITE);
This code will make an object rotate on itself indefinitely, if someone serves.
Good Luck!!
Repeat android animation
Like this.
animation.setRepeatCount(Animation.INFINITE);
How to repeat an android xml animation
In your question, you are setting mode as count and count as mode.
setRepeatCount(Animation.INFINITE);
setRepeatMode(Animation.RESTART);
// or setRepeatMode(Animation.REVERSE);
EDIT:
It seems that there's some confusion here. You are creating AnimationSet
and not Animation
. Unfortunately, AnimationSet
does not seem to support repetition as well as Animation
.
So you'd need to call animationSet.start()
in onAnimationEnd
callback. For further details, check this answer. How to repeat an AnimationSet with sequentially added animations
In onAnimationEnd
calling view.startAnimation(animationSet)
seems to work as well.
How to repeat Android Animation
You can run multiple SpringAnimation
s on the same View
by repeatedly calling animateToFinalPosition(translation)
with a sequence of translation values.
For example:
startSpringAnimations(findViewById<View>(R.id.imageView1), 300f, 6)
startSpringAnimations(findViewById<View>(R.id.imageView2), -600f, 6)
with a function
/**
* [view] will be moved using [times] SpringAnimations over a distance of abs([totalTranslation])
* If [totalTranslation] is negative, direction will be up, else down
*/
private fun startSpringAnimations(view: View, totalTranslation: Float, times: Int ) {
if(times <= 0){
return
}
val translation = totalTranslation/ times.toFloat()
SpringAnimation(view, DynamicAnimation.TRANSLATION_Y, 0f).apply{
spring.dampingRatio = SpringForce.DAMPING_RATIO_HIGH_BOUNCY
spring.stiffness = SpringForce.STIFFNESS_VERY_LOW
addEndListener(object: DynamicAnimation.OnAnimationEndListener{
private var count = 1
override fun onAnimationEnd(animation1: DynamicAnimation<*>?, canceled: Boolean, value: Float, velocity: Float) {
Log.d("SpringAnimation", "onAnimationEnd: animation $animation1 canceled $canceled value $value velocity $velocity count $count")
if (canceled) return
count++
if(count <= times){
animateToFinalPosition(translation * count)
}
}
})
animateToFinalPosition(translation)
}
}
Why my animation doesn't repeat?
Change your XML to have the repeat mode and count on it:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="0%" android:toXDelta="-18.5%"
android:fromYDelta="0%" android:toYDelta="0%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:duration="1000"/>
</set>
Related Topics
How to Add (Vertical) Divider to a Horizontal Linearlayout
Android: How to Detect When a Scroll Has Ended
How to Automatically Restart a Service Even If User Force Close It
What Is the Meaning of Addtobackstack with Null Parameter
How to Set the Initial Zoom/Width for a Webview
Android L's Ripple Effect - Touch Feedback for Buttons - Using Xml
Camera Preview Image Data Processing with Android L and Camera2 API
Cannot Install Signed APK to Device Manually, Got Error "App Not Installed"
Any Way to Link to the Android Notification Settings for My App
Programmatically Connect to Paired Bluetooth Device
Android Contacts Display Name and Phone Number(S) in Single Database Query
How to Set "Android:Layout_Below" at Runtime Programmatically
Android: How Do Bluetooth Uuids Work
How to Give Hexagon Shape to Imageview
Disable Sound from Notificationchannel