Complete parsing of the latest Android animation Framework (2) -- Transitions Framework (Transitions Framework)

Source: Internet
Author: User
Tags getcolor

Complete parsing of the latest Android animation Framework (2) -- Transitions Framework (Transitions Framework)

The previous article explains the Android animation Animator, but I don't know if you have found that all the animations described above are for a certain Object, although we can add an animation effect to the entire Layout, we first regard the entire layout as a whole, and then add the animation effect to the whole. What should we do when we want to add animation effects to multiple objects at the same time?

Let's take a look at the effect.

Why do you want to use Transitions: For ViewGroup-level animation effects, you only need to determine the starting and ending state of the animation to complete the entire animation. Commonly Used animations that can be used directly support Resource) callback functions are available in the loading animation lifecycle to better control the animation effect Scenes.

A Scene stores the shape of all elements in a ViewGroup. He also has a reference to the parent ViewGroup of this ViewGroup. This parent ViewGroup is called scene root.

Transitions

The animation information is included in a Transition object. Use TransitionManager to use an animation in Transition. The Transitions frame can be animated between two different Scene or different elements of the same Scene.

Limiting API Level 19 (Android 4.4.2) to SurfaceView may cause errors
SurfaceView is updated on a non-UI thread, so the animation of other elements may not be synchronized. Using TextureView may result in errors in the View inherited from AdapterView. For example, an error may occur on ListView. When you use an animation for TextView, the text in it may run elsewhere before the animation ends. Create Scene
Scene mAScene;Scene mAnotherScene;// Create the scene root for the scenes in this appmSceneRoot = (ViewGroup) findViewById(R.id.scene_root);// Create the scenesmAScene = Scene.getSceneForLayout(mSceneRoot, R.layout.a_scene, this);mAnotherScene =    Scene.getSceneForLayout(mSceneRoot, R.layout.another_scene, this);
Create Scene Actions

The Transitions framework allows you to customize the actions when you enter and exit Scene. Define custom actions as Runnable objects and pass them as parameters to Scene. setExitAction () or Scene. setEnterAction. The system will call these two methods when entering and exiting.

Create a Transition create a Transition from the resource file

The procedure is as follows:

1. Add res/transition/directory to the Project
2. Create an XML file in the directory
Res/transition/fade_transition.xml


  

3. Load in Activity

Transition mFadeTransition =        TransitionInflater.from(this).        inflateTransition(R.transition.fade_transition);
Dynamically add Transition in the code
Transition mFadeTransition = new Fade();
Call Transition
TransitionManager.go(mEndingScene, mFadeTransition);

With this statement, the View in scene root changes from the initial state to the end state based on the Transition.

Use Transition for a specific View

Since the Transition framework does not apply to all objects (such as ListView), sometimes we need to specify objects that use Transition.
Each object using Transition is called a target. Of course, this object must be in Scene. You can call the removeTarget () method before the transition to remove unsupported objects or call addTarget () to add objects.

Use transitionSet

TransitionSet is similar to the Set in Animation and is an Animation Set. Defined in XML, as follows:


      
       
        
     
    
   
  

Call TransitionInflater. from () in the Activity to load the TransitionSet. TransitionSet is inherited from Transition. You can use TransitionSet wherever you can use Transition.

Custom Transitions inherit the Transition class
public class CustomTransition extends Transition {    @Override    public void captureStartValues(TransitionValues values) {}    @Override    public void captureEndValues(TransitionValues values) {}    @Override    public Animator createAnimator(ViewGroup sceneRoot,                                   TransitionValues startValues,                                   TransitionValues endValues) {}}
Override captureStartValues ()

The Framework calls the captureStartValues () method for each object in the Start Scene. The method parameter is the TransitionValues object. This object contains a reference corresponding to this View and a Map instance, this Map instance is used to save the required attribute values. To ensure that the Key of the attribute value does not conflict with other TransitionValues keys, the following naming rules are recommended.

package_name:transition_name:property_name

The following is an example of rewriting captureStartValues:

public class CustomTransition extends Transition {    // Define a key for storing a property value in    // TransitionValues.values with the syntax    // package_name:transition_class:property_name to avoid collisions    private static final String PROPNAME_BACKGROUND =            "com.example.android.customtransition:CustomTransition:background";    @Override    public void captureStartValues(TransitionValues transitionValues) {        // Call the convenience method captureValues        captureValues(transitionValues);    }    // For the view in transitionValues.view, get the values you    // want and put them in transitionValues.values    private void captureValues(TransitionValues transitionValues) {        // Get a reference to the view        View view = transitionValues.view;        // Store its background property in the values map        transitionValues.values.put(PROPNAME_BACKGROUND, view.getBackground());    }    ...}
CaptureEndValues ()
@Overridepublic void captureEndValues(TransitionValues transitionValues) {    captureValues(transitionValues);}

Similar to captureStartValues (), the end value is put into the Map object of TransitionValues. The Map object in captureEndValues () is not the same object as the Map object in captureStartValues (), put () please rest assured ~

CreateAnimator ()

Create an animation or to take charge of the animation effect from the initial state to the end state, and return the animation or. The following is an example of changing the background color.

 // Create an animation for each target that is in both the starting and ending Scene. For each    // pair of targets, if their background property value is a color (rather than a graphic),    // create a ValueAnimator based on an ArgbEvaluator that interpolates between the starting and    // ending color. Also create an update listener that sets the View background color for each    // animation frame    @Override    public Animator createAnimator(ViewGroup sceneRoot,                                   TransitionValues startValues, TransitionValues endValues) {        // This transition can only be applied to views that are on both starting and ending scenes.        if (null == startValues || null == endValues) {            return null;        }        // Store a convenient reference to the target. Both the starting and ending layout have the        // same target.        final View view = endValues.view;        // Store the object containing the background property for both the starting and ending        // layouts.        Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND);        Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND);        // This transition changes background colors for a target. It doesn't animate any other        // background changes. If the property isn't a ColorDrawable, ignore the target.        if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) {            ColorDrawable startColor = (ColorDrawable) startBackground;            ColorDrawable endColor = (ColorDrawable) endBackground;            // If the background color for the target in the starting and ending layouts is            // different, create an animation.            if (startColor.getColor() != endColor.getColor()) {                // Create a new Animator object to apply to the targets as the transitions framework                // changes from the starting to the ending layout. Use the class ValueAnimator,                // which provides a timing pulse to change property values provided to it. The                // animation runs on the UI thread. The Evaluator controls what type of                // interpolation is done. In this case, an ArgbEvaluator interpolates between two                // #argb values, which are specified as the 2nd and 3rd input arguments.                ValueAnimator animator = ValueAnimator.ofObject(new ArgbEvaluator(),                        startColor.getColor(), endColor.getColor());                // Add an update listener to the Animator object.                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {                    @Override                    public void onAnimationUpdate(ValueAnimator animation) {                        Object value = animation.getAnimatedValue();                        // Each time the ValueAnimator produces a new frame in the animation, change                        // the background color of the target. Ensure that the value isn't null.                        if (null != value) {                            view.setBackgroundColor((Integer) value);                        }                    }                });                // Return the Animator object to the transitions framework. As the framework changes                // between the starting and ending layouts, it applies the animation you've created.                return animator;            }        }        // For non-ColorDrawable backgrounds, we just return null, and no animation will take place.        return null;    }
Summary

Now we have finished all the basic knowledge about Android Animation. To achieve the Animation effect of a single object, the Transitions framework can simultaneously achieve the Animation effect of multiple objects. In the actual project, you need to select according to your specific needs. Although the basic knowledge has been completed, how to achieve beautiful results is still a good test of the art skills, for example, the blogger is a typical failure example/(ㄒ o workshop )/~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.