Android Material Design-Defining Custom Animations (Custom Animation)-(6), androidmaterial

Source: Internet
Author: User

Android Material Design-Defining Custom Animations (Custom Animation)-(6), androidmaterial

Reprinted please indicate the source: http://blog.csdn.net/bbld_/article/details/40633327

 

When a user interacts with your app, the animation in the material design gives feedback on the user's actions and provides visual consistency (feelings ). The Material topic provides some default animation effects for button and activity transition. In Android 5.0 (API level 21) or later, you can customize these animations, you can also create new animations:

L Touch feedback (Touch feedback)

L Circular Reveal (loop display)

L Activity transitions (Activity transition)

L Curved motion (curve motion)

L View state changes (View state change)

 

Custom touch feedback

When the user interacts with the user interface, the touch feedback in materialdesign provides an instantaneous visual confirmation on the touch point. The default touch feedback animation of the button uses the new RippleDrawable class, which uses the ripple effect to convert between different States.

In most cases, you should use the following method in your layout XML file to specify the view Background:

L? Android: attr/selectableItemBackground for a bounded ripple (bounded ripple)

L? Android: attr/selectableItemBackgroundBorderless for a ripple that extends beyond the view (cross-border ripple)

Note:SelectableItemBackgroundBorderless is a new attribute of API level 21.

Alternatively, you can define a RippleDrawable as the XML resource of the RippleDrawable element.

You can specify a color for the RippleDrawable object. To change the default touch feedback color, use the android: colorControlHighlight attribute of the topic.

For more information, see the RippleDrawable API documentation.

 

Use revealing results

When you display or hide a set of UI elements, the display animation provides you with visual continuity (feeling ). The ViewAnimationUtils. createCircularReveal () method allows you to use animated effects to display or hide a view.

Display (reveal) previously invisible views:

// previously invisible viewView myView = findViewById(R.id.my_view);// get the center for the clipping circleint cx = (myView.getLeft() + myView.getRight()) / 2;int cy = (myView.getTop() + myView.getBottom()) / 2;// get the final radius for the clipping circleint finalRadius = myView.getWidth();// create and start the animator for this view// (the start radius is zero)Animator anim =    ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);anim.start();

Hide a visible View:

// previously visible viewfinal View myView = findViewById(R.id.my_view);// get the center for the clipping circleint cx = (myView.getLeft() + myView.getRight()) / 2;int cy = (myView.getTop() + myView.getBottom()) / 2;// get the initial radius for the clipping circleint initialRadius = myView.getWidth();// create the animation (the final radius is zero)Animator anim =    ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, 0);// make the view invisible when the animation is doneanim.addListener(new AnimatorListenerAdapter() {    @Override    public void onAnimationEnd(Animator animation) {        super.onAnimationEnd(animation);        myView.setVisibility(View.INVISIBLE);    }});// start the animationanim.start();

Transition of custom Activity

In the Activity transition of the materialdesign app, the transition between different States produced by gestures and common elements (both) provides a visual connection. Between activities, you can specify a Custom Animation for the transition between entry, exit, and shared elements.

L oneEnterThe transition (animation) determines how all views in the activity enter the screen. For example, when an explode enters the transition (animation), all views go out of the screen and fly to the center of the screen.

L oneExitThe transition (animation) determines how all views in an activity exit the screen. For example, in an explode exit transition (animation), all views always exit from the center of the screen.

L oneShared ElementTransition (animation) determines the transition between two activities and how to share the views. For example, if two activities have the same image with different positions and sizes, then changeImageTransform ?) The shared element's transition converter box smoothly scales the image.

Figure 1: Transition of shared elements.

 

Android 5.0 (API level 21) supports these transition animations:

L explode (decomposition)-move the view in or out of the ground, from the middle of the screen

L slide-move the view in or out of the ground from the screen edge

L fade (fade out)-Adds or removes a view by changing the opacity of the view on the screen)

Any transition that inherits the Visibility class supports the transition to or from. For more information, see the Transition API documentation.

Android 5.0 (API level 21) also supports the transition of these shared elements:

L changeBounds-change the layout boundaries of the target view with an animation (effect)

L changeconbounds-use an animation to change the cropping boundary of the target view

L changeTransform-scale and rotate the target view with an animation (effect)

L changeImageTransform-adjust the scaling and size of the target image with an animation (effect)

When you enable activity transition (animation) in your app, the entry and exit of activities will display the default cross-gradient transition effect.


Figure 2: screen update with a shared Element


Custom conversion (animation)

FirstWhen you define a theme that inherits the material style, use the android: windowContentTransitions attribute to enable the content conversion (effect) of the window ). You can also specify conversion between inbound, outbound, and shared elements:

<Style name = "BaseAppTheme" parent = "android: Theme. Material"> <! -- Enable content conversion in the window --> <item name = "android: windowContentTransitions"> true </item> <! -- Switch to and from (effect) --> <item name = "android: windowEnterTransition"> @ transition/explode </item> <item name = "android: using wexittransition "> @ transition/explode </item> <! -Specify the conversion (effect) of shared elements --> <item name = "android: windowSharedElementEnterTransition"> @ transition/change_image_transform </item> <item name = "android: windowSharedElementExitTransition "> @ transition/change_image_transform </item> </style>

In this example, change_image_transform is defined as follows:

<!-- res/transition/change_image_transform.xml --><!-- (see also Shared Transitions below) --><transitionSet xmlns:android="http://schemas.android.com/apk/res/android">  <changeImageTransform/></transitionSet>


The changeImageTransform element corresponds to the ChangeImageTransform class. For more information, see the Transition API documentation.

In the code, enable the transition of Window content and call the Window. requestFeature () method:

// In your activity (if you have not enabled conversion in the topic) getWindow (). requestFeature (Window. FEATURE_CONTENT_TRANSITIONS); // sets an exit transition (animation) getWindow (). setExitTransition (new Explode ());

To specify the conversion in the code, call these methods with a Transition object:

L Window. setEnterTransition ()

L Window. setExitTransition ()

L Window. setSharedElementEnterTransition ()

L Window. setSharedElementExitTransition ()

The setExitTransition () method and setSharedElementExitTransition () method define the exit transition (animation) for the activity (to be hidden) that is being called (Enabled ). The setEnterTransition () method and the setSharedElementEnterTransition () method define the transition (animation) for the called (Enabled) activity (the activity to be displayed ).

To get a complete transition effect, you must enable the window content transition in both the converted activities. Otherwise, the activity to be displayed will start to exit the transition animation, but then you will see the transition of a window (such as scaling or gradient ).

To start a transition as soon as possible, use the Window. setAllowEnterTransitionOverlap () method in the activity to be displayed. This gives you a more compelling transition animation.


Enable activity with transition (animation)

If you enable transition animation for an activity and set exit transition animation, the transition animation will be activated when you start another activity:

 

startActivity(intent,             ActivityOptions.makeSceneTransitionAnimation(this).toBundle());


If you set a transition to the second activity, the transition is also activated when the activity is enabled. To disable the transition when another activity is enabled, you can provide a bundle with an empty option.

 

Enable activity with sharing Elements

To enable transition animation between two activities with a shared element:

1. Enable window content transition in your topic

2. Specify the transition of shared elements in your theme Style

3. Define your transition animation as an XML Resource

4. Use the android: transitionName attribute to specify the same name for the shared elements in the two la S.

5. Use ActivityOptions. makeSceneTransitionAnimation () method

// get the element that receives the click eventfinal View imgContainerView = findViewById(R.id.img_container);// get the common element for the transition in this activityfinal View androidRobotView = findViewById(R.id.image_small);// define a click listenerimgContainerView.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View view) {        Intent intent = new Intent(this, Activity2.class);        // create the transition animation - the images in the layouts        // of both activities are defined with android:transitionName="robot"        ActivityOptions options = ActivityOptions            .makeSceneTransitionAnimation(this, androidRobotView, "robot");        // start the new activity        startActivity(intent, options.toBundle());    }});

To generate a shared dynamic View in the code, use the View. setTransitionName () method in the two activities to specify an identical element name.

To reverse the scene transition animation, when you finish the second activity, call Activity. finishAfterTransition () instead of calling Activity. finish.


Enable activity with multiple sharing Elements

To enable two activities to have multiple sharing elements, use the android: transitionName attribute in the two la s to define the sharing element (or use View in the two activities. setTransitionName () method), and create an ActivityOptions object as follows:

ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this,        Pair.create(view1, "agreedName1"),        Pair.create(view2, "agreedName2"));


Use curve Motion

The animation in Material design depends on the curve, which is applicable to the time interpolation tool and the control motion mode. In Android (API level 21) or later versions, you can customize the animation of the regular curve and surface motion modes.

The PathInterpolator class is a new interpolation tool based on the besserx curve or Path object. This interpolation device specifies a square Motion Curve of 1x1, and uses () points and () positioning points and control points as parameters in the constructor. You can also define a path interpolation tool for XML resources:

<pathInterpolator xmlns:android="http://schemas.android.com/apk/res/android"    android:controlX1="0.4"    android:controlY1="0"    android:controlX2="1"    android:controlY2="1"/>

In the materialdesign specification, the system provides three basic curves:

L @ interpolator/fast_out_linear_in.xml

L @ interpolator/fast_out_slow_in.xml

L @ interpolator/linear_out_slow_in.xml

You can pass a PathInterpolator object to the Animator. setInterpolator () method.

The ObjectAnimator class has a new constructor that allows you to use two or more attributes at a time to draw an animation path. For example, the following animation uses a Path object to draw an animation of view X and Y attributes:

ObjectAnimator mAnimator;mAnimator = ObjectAnimator.ofFloat(view, View.X, View.Y, path);...mAnimator.start();


Create an animation for view changes

The StateListAnimator class allows you to define an animation set and work when the view status changes. The following example shows how to define StateListAnimator for an XML resource.

<! -- Animate the translationZ property of a view when pressed --> <! -View to draw an animation on the translationZ property when pressed-> <selector xmlns: android = "http://schemas.android.com/apk/res/android"> <item android: state_pressed = "true"> <set> <objectAnimator android: propertyName = "translationZ" android: duration = "@ android: integer/config_shortAnimTime" android: valueTo = "2dp" android: valueType = "floatType"/> <! -- You cocould have other objectAnimator elements here for "x" and "y", or other properties --> <! -You can also set other objectAnimator elements to x, y, or other elements-> </set> </item> <item android: state_enabled = "true" android: state_pressed = "false" android: state_focused = "true"> <set> <objectAnimator android: propertyName = "translationZ" android: duration = "100" android: valueTo = "0" android: valueType = "floatType"/> </set> </item> </selector>

To attach an animation set for a custom view state change to a view, define an animation created using an XML resource file of the selector element, as in this example, and use android: the stateListAnimator attribute assigns it to your view. Assign a State animation set to your View in code, use the AnimationInflater. loadStateListAnimator () method, and assign the animation to your View using the View. setStateListAnimator () method.

When your topic inherits the material topic, all buttons have a Z animation by default. To avoid this default behavior in your buttons, set the android: stateListAnimator attribute to @ null.

The AnimatedStateListDrawable class allows you to create a drawable resource, which displays an animation when the status of the associated view changes. Some system controls in Android5.0 use these default animations. The following example shows how to define an AnimatedStateListDrawable as an XML Resource:

<! -- Res/drawable/myanimstatedrawable. xml --> <animated-selector xmlns: android = "http://schemas.android.com/apk/res/android"> <! -- Provide a different drawable for each state --> <! -Provide a different drawable-> <item android: id = "@ + id/pressed" android: drawable = "@ drawable/drawableP" android: state_pressed = "true"/> <item android: id = "@ + id/focused" android: drawable = "@ drawable/drawableF" android: state_focused = "true"/> <item android: id = "@ id/default" android: drawable = "@ drawable/drawableD"/> <! -- Specify a transition --> <! -Define a transition-> <transition android: fromId = "@ + id/default" android: toId = "@ + id/pressed"> <animation-list> <item android: duration = "15" android: drawable = "@ drawable/dt1"/> <item android: duration = "15" android: drawable = "@ drawable/dt2"/>... </animation-list> </transition>... </animated-selector>


Create a vector Drawable Animation

Vector drawable (Vector Drawables) is scalable but clear. The AnimatedVectorDrawable class allows you to create an animation for vector resources.

You usually define the animation carrier of vector resources in three XML files:

L <vector> element vector resource, in res/drawable/(folder)

L <animated-vector> element vector resource animation, IN res/drawable/(folder)

L one or more object animations of the <objectAnimator> element, in the res/anim/(folder)

Vector resource animation can create animations for <group> and <path> element attributes. The <group> element defines a group of paths or child groups, and the <path> element defines the path to be drawn.

When you want to create an animation, define vector resources and assign a unique name to the group and path using the android: name attribute, so that you can query them from your animation definition. For example:


<!-- res/drawable/vectordrawable.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android"    android:height="64dp"    android:width="64dp"    android:viewportHeight="600"    android:viewportWidth="600">    <group        android:name="rotationGroup"        android:pivotX="300.0"        android:pivotY="300.0"        android:rotation="45.0" >        <path            android:name="v"            android:fillColor="#000000"            android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />    </group></vector>

The definition of vector resource animation refers to the group and path in the vector resource by their names:

<!-- res/drawable/animvectordrawable.xml --><animated-vector xmlns:android="http://schemas.android.com/apk/res/android"  android:drawable="@drawable/vectordrawable" >    <target        android:name="rotationGroup"        android:animation="@anim/rotation" />    <target        android:name="v"        android:animation="@anim/path_morph" /></animated-vector>

The animation definition indicates the ObjectAnimator or AnimatorSet object. In this example, the first animation is to rotate the target group at 360 degrees:

<!-- res/anim/rotation.xml --><objectAnimator    android:duration="6000"    android:propertyName="rotation"    android:valueFrom="0"    android:valueTo="360" />

The second animation in this example (as shown below) changes the path of the vector resource from one shape to another. The two shapes must be deformation compatible: they must be commands with the same number and the same number of command parameters.

<!-- res/anim/path_morph.xml --><set xmlns:android="http://schemas.android.com/apk/res/android">    <objectAnimator        android:duration="3000"        android:propertyName="pathData"        android:valueFrom="M300,70 l 0,-70 70,70 0,0   -70,70z"        android:valueTo="M300,70 l 0,-70 70,0  0,140 -70,0 z"        android:valueType="pathType" /></set>

For more information, see the description of the AnimatedVectorDrawable API documentation.



Demo effect:



In addition, there is a better example of vector drawable (animated-vector-drawable) on github. Its effect is as follows:



Demo Source: http://download.csdn.net/detail/bbld_/8102797

Animated-vector-drawable source code (Eclipse project): http://download.csdn.net/detail/bbld_/8102935





Can I send a [Android development video tutorial] 02_11_Animations usage (4) seed or download link?

[Android development video tutorial] 02_11_Animations usage (4) seeds:
Thunder: // expires + eahOS9v + eUqO + 8 iOWbm ++ 8iS5tcDQ/expires
Please adopt

[Android development video tutorial] How to Use 02_10_Animations (3? Life Safety

[Android development video tutorial] Using 02_10_Animations (3) seeds:
Thunder: // outputs + eahOS9v + outputs =
Copy link download

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.