Skip to content

Instantly share code, notes, and snippets.

@Tagakov
Created March 22, 2018 23:10
Show Gist options
  • Save Tagakov/9b06861df01a878b045c3a7802a3de1a to your computer and use it in GitHub Desktop.
Save Tagakov/9b06861df01a878b045c3a7802a3de1a to your computer and use it in GitHub Desktop.
package com.tagakov.avdplayground
import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.os.Bundle
import android.os.Handler
import android.support.graphics.drawable.AnimatedVectorDrawableCompat
import android.support.v7.app.AppCompatActivity
import android.widget.ImageView
class AnimatedVectorProgressActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val avd = findViewById<ImageView>(R.id.img).drawable as AnimatedVectorDrawableCompat
//avd.start()
val realAnimator = getInternalAnimator(avd)
val handler = Handler()
var time = 0
val runnable = object : Runnable {
override fun run() {
if (realAnimator.setPlayTime(time.toLong())) {
time += 16
avd.invalidateSelf()
handler.postDelayed(this, 16 * 1)
}
}
}
runnable.run()
}
private fun getInternalAnimator(animatable: AnimatedVectorDrawableCompat): Animator {
val mAnimatedVectorStateField = animatable.javaClass.getDeclaredField("mAnimatedVectorState")
mAnimatedVectorStateField.isAccessible = true
val mAnimatedVectorState = mAnimatedVectorStateField.get(animatable)
val mAnimatorSetField = mAnimatedVectorState.javaClass.getDeclaredField("mAnimatorSet")
mAnimatorSetField.isAccessible = true
return mAnimatorSetField.get(mAnimatedVectorState) as Animator
}
private fun Animator.setPlayTime(time: Long): Boolean {
val adjustedTime = time - startDelay
if (adjustedTime >= 0) {
if (this is AnimatorSet) {
return childAnimations.any { it.setPlayTime(adjustedTime) }
} else {
val valueAnimator = this as ValueAnimator
if (valueAnimator.repeatCount == ValueAnimator.INFINITE || valueAnimator.duration * valueAnimator.repeatCount >= adjustedTime) {
valueAnimator.currentPlayTime = adjustedTime
return true
}
}
}
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment