Last active
May 22, 2024 09:07
-
-
Save rcmkt/6187f432fadead124212dd2141051a62 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 ChangeOutlineRadiusTransition( | |
private val startRadius: Int, | |
private val endRadius: Int | |
) : Transition() { | |
private companion object { | |
/** | |
* Unique key for start and end values to be kept in [TransitionValues] [TransitionValues] | |
*/ | |
private const val RADIUS = "ChangeOutlineRadiusTransition:outlineRadius" | |
/** | |
* The properties from [TransitionValues] | |
*/ | |
private val PROPERTIES = arrayOf(RADIUS) | |
/** | |
* Animator property which will set a rounded outline through a [ViewOutlineProvider] | |
*/ | |
private object OutlineRadiusProperty : Property<View, Int>(Int::class.java, "outlineRadius") { | |
var offset = 0 | |
override fun get(view: View) = 0 | |
override fun set(view: View, value: Int) { | |
view.outlineProvider = object : ViewOutlineProvider() { | |
override fun getOutline(view: View, outline: Outline) { | |
outline.setRoundRect(0, 0, view.width, view.height + offset, value.toFloat()) | |
} | |
} | |
} | |
} | |
} | |
override fun getTransitionProperties(): Array<String> { | |
return PROPERTIES | |
} | |
override fun captureStartValues(transitionValues: TransitionValues) { | |
val view = transitionValues.view | |
if (view !is ImageView || view.getVisibility() != View.VISIBLE) { | |
return | |
} | |
transitionValues.values[RADIUS] = startRadius | |
} | |
override fun captureEndValues(transitionValues: TransitionValues) { | |
val view = transitionValues.view | |
if (view !is ImageView || view.getVisibility() != View.VISIBLE) { | |
return | |
} | |
transitionValues.values[RADIUS] = endRadius | |
} | |
override fun createAnimator(sceneRoot: ViewGroup, startValues: TransitionValues?, endValues: TransitionValues?): Animator { | |
val view = endValues?.view | |
view?.clipToOutline = true | |
val startRadius = startValues?.values?.get(RADIUS) as? Int ?: 0 | |
val endRadius = endValues?.values?.get(RADIUS) as? Int ?: 0 | |
val property = OutlineRadiusProperty.apply { offset = if (startRadius > 0) startRadius else endRadius } | |
return ObjectAnimator.ofInt(view, property, startRadius, endRadius) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sometimes, we are getting an exception: