|
| 1 | +/* |
| 2 | + * Copyright 2023-2025 wintmain |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.wintmain.wBasis.transition.util; |
| 18 | + |
| 19 | +import android.animation.Animator; |
| 20 | +import android.animation.ArgbEvaluator; |
| 21 | +import android.animation.ValueAnimator; |
| 22 | +import android.graphics.drawable.ColorDrawable; |
| 23 | +import android.graphics.drawable.Drawable; |
| 24 | +import android.transition.Transition; |
| 25 | +import android.transition.TransitionValues; |
| 26 | +import android.view.View; |
| 27 | +import android.view.ViewGroup; |
| 28 | +import androidx.annotation.NonNull; |
| 29 | + |
| 30 | +public class ChangeColor extends Transition { |
| 31 | + /** Key to store a color value in TransitionValues object */ |
| 32 | + private static final String PROPNAME_BACKGROUND = "customtransition:change_color:background"; |
| 33 | + |
| 34 | + @Override |
| 35 | + public void captureStartValues(TransitionValues transitionValues) { |
| 36 | + captureValues(transitionValues); |
| 37 | + } |
| 38 | + |
| 39 | + // Capture the value of the background drawable property for a target in the ending Scene. |
| 40 | + @Override |
| 41 | + public void captureEndValues(TransitionValues transitionValues) { |
| 42 | + captureValues(transitionValues); |
| 43 | + } |
| 44 | + |
| 45 | + // BEGIN_INCLUDE (capture_values) |
| 46 | + /** |
| 47 | + * Convenience method: Add the background Drawable property value |
| 48 | + * to the TransitionsValues.value Map for a target. |
| 49 | + */ |
| 50 | + private void captureValues(TransitionValues values) { |
| 51 | + // Capture the property values of views for later use |
| 52 | + values.values.put(PROPNAME_BACKGROUND, values.view.getBackground()); |
| 53 | + } |
| 54 | + // END_INCLUDE (capture_values) |
| 55 | + |
| 56 | + // BEGIN_INCLUDE (create_animator) |
| 57 | + // Create an animation for each target that is in both the starting and ending Scene. For each |
| 58 | + // pair of targets, if their background property value is a color (rather than a graphic), |
| 59 | + // create a ValueAnimator based on an ArgbEvaluator that interpolates between the starting and |
| 60 | + // ending color. Also create an update listener that sets the View background color for each |
| 61 | + // animation frame |
| 62 | + @Override |
| 63 | + public Animator createAnimator(@NonNull ViewGroup sceneRoot, |
| 64 | + TransitionValues startValues, TransitionValues endValues) { |
| 65 | + // This transition can only be applied to views that are on both starting and ending scenes. |
| 66 | + if (null == startValues || null == endValues) { |
| 67 | + return null; |
| 68 | + } |
| 69 | + // Store a convenient reference to the target. Both the starting and ending layout have the |
| 70 | + // same target. |
| 71 | + final View view = endValues.view; |
| 72 | + // Store the object containing the background property for both the starting and ending |
| 73 | + // layouts. |
| 74 | + Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND); |
| 75 | + Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND); |
| 76 | + // This transition changes background colors for a target. It doesn't animate any other |
| 77 | + // background changes. If the property isn't a ColorDrawable, ignore the target. |
| 78 | + if (startBackground instanceof ColorDrawable startColor && endBackground instanceof ColorDrawable endColor) { |
| 79 | + // If the background color for the target in the starting and ending layouts is |
| 80 | + // different, create an animation. |
| 81 | + if (startColor.getColor() != endColor.getColor()) { |
| 82 | + // Create a new Animator object to apply to the targets as the transitions framework |
| 83 | + // changes from the starting to the ending layout. Use the class ValueAnimator, |
| 84 | + // which provides a timing pulse to change property values provided to it. The |
| 85 | + // animation runs on the UI thread. The Evaluator controls what type of |
| 86 | + // interpolation is done. In this case, an ArgbEvaluator interpolates between two |
| 87 | + // #argb values, which are specified as the 2nd and 3rd input arguments. |
| 88 | + ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(), |
| 89 | + startColor.getColor(), endColor.getColor()); |
| 90 | + // Add an update listener to the Animator object. |
| 91 | + animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 92 | + @Override |
| 93 | + public void onAnimationUpdate(ValueAnimator animation) { |
| 94 | + Object value = animation.getAnimatedValue(); |
| 95 | + // Each time the ValueAnimator produces a new frame in the animation, change |
| 96 | + // the background color of the target. Ensure that the value isn't null. |
| 97 | + if (null != value) { |
| 98 | + view.setBackgroundColor((Integer) value); |
| 99 | + } |
| 100 | + } |
| 101 | + }); |
| 102 | + // Return the Animator object to the transitions framework. As the framework changes |
| 103 | + // between the starting and ending layouts, it applies the animation you've created. |
| 104 | + return animator; |
| 105 | + } |
| 106 | + } |
| 107 | + // For non-ColorDrawable backgrounds, we just return null, and no animation will take place. |
| 108 | + return null; |
| 109 | + } |
| 110 | + // END_INCLUDE (create_animator) |
| 111 | +} |
0 commit comments