Created
July 11, 2019 10:19
-
-
Save m4xp1/a9813e663bccfe9e0520ebc83e300acb to your computer and use it in GitHub Desktop.
ResizeText transition animation
This file contains hidden or 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
| package com.example.android.basictransition | |
| import android.animation.Animator | |
| import android.animation.AnimatorListenerAdapter | |
| import android.animation.ValueAnimator | |
| import android.content.Context | |
| import android.content.res.Resources | |
| import android.graphics.Bitmap | |
| import android.graphics.Canvas | |
| import android.graphics.Rect | |
| import android.graphics.drawable.BitmapDrawable | |
| import android.graphics.drawable.Drawable | |
| import android.transition.Transition | |
| import android.transition.TransitionValues | |
| import android.util.AttributeSet | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import android.widget.TextView | |
| class ResizeText : Transition { | |
| companion object { | |
| private const val LAYOUT_WIDTH = "ResizeText:layout_width" | |
| private const val LAYOUT_HEIGHT = "ResizeText:layout_height" | |
| private const val OVERLAY_DRAWABLE = "ResizeText:overlay_drawable" | |
| private val PROPERTIES = arrayOf(LAYOUT_WIDTH, LAYOUT_HEIGHT) | |
| } | |
| private val resources: Resources | |
| constructor(context: Context) { | |
| resources = context.resources | |
| addTarget(TextView::class.java) | |
| } | |
| constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { | |
| resources = context.resources | |
| addTarget(TextView::class.java) | |
| } | |
| override fun getTransitionProperties(): Array<String> { | |
| return PROPERTIES | |
| } | |
| override fun captureStartValues(transitionValues: TransitionValues) { | |
| captureValues(transitionValues) | |
| val view = transitionValues.view | |
| if (view is TextView) { | |
| transitionValues.values[OVERLAY_DRAWABLE] = obtainDrawableFromView(view) | |
| } | |
| } | |
| override fun captureEndValues(transitionValues: TransitionValues) { | |
| captureValues(transitionValues) | |
| } | |
| private fun captureValues(transitionValues: TransitionValues) { | |
| val view = transitionValues.view | |
| if (view is TextView) { | |
| transitionValues.values[LAYOUT_WIDTH] = view.layoutParams.width | |
| transitionValues.values[LAYOUT_HEIGHT] = view.layoutParams.height | |
| } | |
| } | |
| override fun createAnimator( | |
| sceneRoot: ViewGroup, | |
| startValues: TransitionValues, | |
| endValues: TransitionValues | |
| ): Animator? { | |
| val overlayDrawable = startValues.values[OVERLAY_DRAWABLE] as Drawable | |
| val view = endValues.view as TextView | |
| return ValueAnimator.ofFloat(0f, 1f).apply { | |
| interpolator = [email protected] | |
| startDelay = [email protected] | |
| duration = [email protected] | |
| addUpdateListener { | |
| overlayDrawable.setBounds(0, 0, view.width, view.height) | |
| } | |
| addListener(object : AnimatorListenerAdapter() { | |
| override fun onAnimationStart(animation: Animator?) = | |
| view.overlay.add(overlayDrawable) | |
| override fun onAnimationEnd(animation: Animator?) = | |
| view.overlay.remove(overlayDrawable) | |
| override fun onAnimationCancel(animation: Animator?) = | |
| view.overlay.remove(overlayDrawable) | |
| }) | |
| } | |
| } | |
| private fun obtainDrawableFromView(view: View): Drawable { | |
| val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888).apply { | |
| val canvas = Canvas(this) | |
| view.layout(view.left, view.top, view.right, view.bottom) | |
| view.draw(canvas) | |
| } | |
| return BitmapDrawable(resources, bitmap).apply { | |
| bounds = Rect(0, 0, view.width, view.height) | |
| setAntiAlias(false) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment