Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
}
}
@Drjacky

This comment has been minimized.

Copy link

Drjacky commented Nov 19, 2019

Sometimes, we are getting an exception:

Build version: 3.2.5 
Current date: 2019-11-18 15:46:44 
Device: Google Android SDK built for x86 
 
Stack trace:  
java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter view
    at com.presentation.ui.services.food.fragments.restaurantdetails.ChangeOutlineRadiusTransition$Companion$OutlineRadiusProperty.set(Unknown Source:2)
    at com.presentation.ui.services.food.fragments.restaurantdetails.ChangeOutlineRadiusTransition$Companion$OutlineRadiusProperty.set(ChangeOutlineRadiusTransition.kt:34)
    at android.animation.PropertyValuesHolder$IntPropertyValuesHolder.setAnimatedValue(PropertyValuesHolder.java:1236)
    at android.animation.ObjectAnimator.animateValue(ObjectAnimator.java:990)
    at android.animation.ValueAnimator.setCurrentFraction(ValueAnimator.java:674)
    at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:637)
    at android.animation.ValueAnimator.start(ValueAnimator.java:1069)
    at android.animation.ValueAnimator.start(ValueAnimator.java:1088)
    at android.animation.ObjectAnimator.start(ObjectAnimator.java:852)
    at android.transition.Transition.animate(Transition.java:1881)
    at android.transition.Transition.runAnimator(Transition.java:901)
    at android.transition.Transition.runAnimators(Transition.java:879)
    at android.transition.TransitionSet.runAnimators(TransitionSet.java:433)
    at android.transition.TransitionSet.runAnimators(TransitionSet.java:433)
    at android.transition.TransitionSet.runAnimators(TransitionSet.java:433)
    at android.transition.TransitionSet.runAnimators(TransitionSet.java:433)
    at android.transition.Transition.playTransition(Transition.java:1780)
    at android.transition.TransitionManager$MultiListener.onPreDraw(TransitionManager.java:315)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:977)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2474)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
    at android.view.Choreographer.doCallbacks(Choreographer.java:761)
    at android.view.Choreographer.doFrame(Choreographer.java:696)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.