Last active
March 30, 2018 11:43
-
-
Save alexstyl/70de450d403de8c8f73af7dcb64d0be4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RadiusTransition private constructor(private val startingRadius: Float, private val endingRadius: Float) : Transition() { | |
override fun captureStartValues(transitionValues: TransitionValues) { | |
if (transitionValues.view is ImageView) { | |
transitionValues.values[PROPNAME_RADIUS] = startingRadius | |
} | |
} | |
override fun captureEndValues(transitionValues: TransitionValues) { | |
if (transitionValues.view is ImageView) { | |
transitionValues.values[PROPNAME_RADIUS] = endingRadius | |
} | |
} | |
override fun createAnimator(sceneRoot: ViewGroup, startValues: TransitionValues?, | |
endValues: TransitionValues?): Animator? { | |
if (startValues == null || endValues == null) { | |
return null | |
} | |
val endImageView = endValues.view as ImageView | |
val start = startValues.values[PROPNAME_RADIUS] as Float | |
val end = endValues.values[PROPNAME_RADIUS] as Float | |
val objectAnimator = ObjectAnimator | |
.ofFloat(endImageView, RADIUS_PROPERTY, start, end) | |
.setDuration(super.getDuration()) | |
objectAnimator.interpolator = super.getInterpolator() | |
return objectAnimator | |
} | |
companion object { | |
private const val PROPNAME_RADIUS = "RadiusTransition:radius" | |
private fun getRadiusFrom(view: ImageView): Float { | |
return (view.drawable as RoundedBitmapDrawable).cornerRadius | |
} | |
private val RADIUS_PROPERTY = object : FloatProperty<ImageView>("radius") { | |
override fun get(imageView: ImageView): Float { | |
val drawable = imageView.drawable as RoundedBitmapDrawable | |
return drawable.cornerRadius | |
} | |
override fun setValue(imageView: ImageView, radius: Float) { | |
val drawable = imageView.drawable as RoundedBitmapDrawable | |
drawable.cornerRadius = radius | |
} | |
} | |
fun toCircle(): RadiusTransition { | |
return RadiusTransition(0F, 1F) | |
} | |
fun toSquare(): RadiusTransition { | |
return RadiusTransition(1F, 0F) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment