Creating Apps with Material design--defining Custom animations

Source: Internet
Author: User
Tags explode

reprint Please specify Http://blog.csdn.net/eclipsexys translation from developer Android, time haste, there are translation questions please note that, thank you


Defining animations

Provide feedback on their behavior and provide visual continuity when material design animations allow users to interact with your application. The material theme provides some default animation buttons and active transitions, and Android5.0 (API level 21) above, you can customize these animations and create new ones:


Touch Feedback
Notification display
Activity change
Curve motion
View state changes

Customizing Touch Feedback
Touch Feedback in material design provides an instantaneous visual confirmation at the point of contact when the user interacts with the user interface elements. The default touch Feedback animation button uses the new rippledrawable class to achieve transitions between different states and produce a chain reaction animation.

In most cases, you should apply this functionality to XML in the view by specifying the view background:


    Android:attr/selectableitembackground for a bounded ripple    ? android:attr/selectableitembackgroundborderless For a ripple that extends beyond the view


Alternatively, you can define an XML resource that rippledrawable to use a ripple element.

You can specify a color Rippledrawable object. To change the default touch feedback color, use the Android:colorcontrolhighlight property of the theme.


Using Reveal Effect

Reveal animations provide the user with visual continuity when you show or hide a set of UI elements. The Viewanimationutils.createcircularreveal () method lets you set the animation clipping circle to show or hide the view.

To use this effect to display a view that was not previously visible:

previously invisible Viewview MyView = Findviewbyid (R.id.my_view);//Get the Center for the clipping circleint cx = (my View.getleft () + myview.getright ())/2;int cy = (myview.gettop () + myview.getbottom ())/2;//Get the final radius for th  E 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 ();

To use this effect to hide previously seen 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 an im =    viewanimationutils.createcircularreveal (MyView, CX, CY, Initialradius, 0);/make the View invisible when the ANI Mation is Doneanim.addlistener (new Animatorlisteneradapter () {    @Override public    void Onanimationend (Animator Animation) {        super.onanimationend (animation);        Myview.setvisibility (view.invisible);    }}); /Start the Animationanim.start ();


customizing activity Transitions

The activity of the material design application moves through the transitions between different states. You can specify the transition between the entry and exit transitions of a custom animation and the sharing of content between the activity.

Android5.0 (API level 21) supports these transitions for entry and exit:

Exploded-a view moved from the center of the scene.
Slide-Move the view or from the edge of the scene.
Fade-Adds or removes a scene view by changing its transparency.

Transition expands any change in the visibility class to support as an entry or exit transformation. For more information, see the API reference for this conversion class.

Android5.0 (API level 21) also supports these common element transformations:

Changebounds-Animate the target view layout bounds.
Changeclipbounds-Animate the target view area clip.
Changetransform-Animation processing target view zoom and rotation.
Changeimagetransform-Animation processing changes the size and proportions of the target image.

When you enable activity transitions in your app, the default is to cross-ramp the transitions into and out of the activity's startup.



Specifying a custom transformation
First, when you define a style that inherits the material's theme properties, the window content is converted Android:windowcontenttransitions. You can also specify the transitions that enter, exit, and specify the styles that you define to share elements:

<style name= "Baseapptheme" parent= "android:Theme.Material" >  <!--Enable window content transitions-- >  <item name= "android:windowcontenttransitions" >true</item>  <!--specify enter and exit Transitions--  <item name= "android:windowentertransition" > @transition/explode</item>  <item name= "Android:windowexittransition" > @transition/explode</item>  <!--specify GKFX Element Transitions--  <item name= "android:windowsharedelemententertransition" >    @transition/ change_image_transform</item>  <item name= "android:windowsharedelementexittransition" >    @ Transition/change_image_transform</item></style>

In this example, the Change_image_transform transition is defined as follows:

<!--res/transition/change_image_transform.xml--><!--(see also GKFX 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 API reference for conversion.

To ensurewindow Moving animation implementation, needCall the Window.requestfeature () method:

Inside your activity (if you do not enable transitions in your theme) GetWindow (). Requestfeature (window.feature_content _transitions);//Set an Exit Transitiongetwindow (). Setexittransition (New Explode ());

To specify your code conversions, call these methods with the transition object:

Window.setentertransition ()
Window.setexittransition ()
Window.setsharedelemententertransition ()
Window.setsharedelementexittransition ()

The Setexittransition () and Setsharedelementexittransition () methods define the exit transitions for the invoke activity. The Setentertransition () and Setsharedelemententertransition () methods define an input transition called an activity.

In order to get the full effect of a transition, you must enable the conversion of the contents of these two calling and called active windows. Otherwise, the invoke activity initiates the exit transition, but then you see a window transition (such as scale or fade).

Start as soon as possible into the transition, using the Window.setallowentertransitionoverlap () method called activity. This gives you more dramatic transitions into the transition animation.


Start an activity with a transform
If you enable conversion and set the exit transition to activity, when you start another activity, the following transitions are activated:

StartActivity (Intent,              activityoptions.makescenetransitionanimation (this). Tobundle ());

If you set up a second activity that goes into transition, the transition is also activated at the start of the activity. To disable the conversion, when you start another activity, a null option is provided.

To implement a screen transition animation between a common element
Let the content of your theme window be converted.
Specifies the transition of the shared element of your style.
The definition is converted to an XML resource.
Specify a common name for shared elements in two layouts with Android: Transitionname property.
Use the 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 is defined with Android:transiti Onname= "Robot"        activityoptions options = Activityoptions            . Makescenetransitionanimation (This, Androidrobotview, "robot");        Start the new Activity        startactivity (Intent, Options.tobundle ());}    );

For your code generation shared dynamic view, use the View.settransitionname () method to specify a common element name in two activity.

To reverse the scene transition animation, when you have completed the second activity, call the Activity.finishaftertransition () method instead of Activity.finish ().


Activate activity with multiple shared elements
To enable two activities to have multiple shared elements, Defines a scene transition animation between two layouts and the shared elements of Android: the Transitionname property (or the method using View.settransitionname () in both activities), and creates a Activityoptions object as follows:

Activityoptions options = Activityoptions.makescenetransitionanimation (This,        pair.create (View1, "agreedName1") ,        pair.create (View2, "agreedName2"));

Using Curves Motion

The animation in the material design relies on the curve interpolation time and the spatial motion pattern. With Android5.0 (API level 21), you can define the animations for custom timing curves and curve motion modes.

The Pathinterpolator class is based on new interpolation on Bézier curves or path objects. This interpolation specifies the motion curve of a 1x1 square, specified with the (0,0) anchor and the control point using the constructor's parameters. You can also define a path to interpolate as an XML resource:

<pathinterpolator xmlns:android= "http://schemas.android.com/apk/res/android"    android:controlx1= "0.4"    android:controly1= "0"    android:controlx2= "1"    android:controly2= "1"/>

The system provides three basic curves of the material design specification in XML resources:

    @interpolator/fast_out_linear_in.xml    @interpolator/fast_out_slow_in.xml    @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 enables you to use two or more properties at once in a path animation coordinate. For example, the following animator uses the path object to animate the X and Y properties of the View:

Objectanimator Manimator;manimator = objectanimator.offloat (view, view.x, view.y, path); .... Manimator.start ();

View Animation state changes

The Statelistanimator class can define changes in the view state when the animation is run. The following example shows how to define a statelistanimator for an XML resource:

<!-- Animate the Translationz property of a view when pressed--><selector xmlns:android= "http://schemas.android.com/ Apk/res/android "> <item android:state_pressed=" true "> <set> <objectanimator Android:propertynam E= "Translationz" android:duration= "@android: Integer/config_shortanimtime" android:valueto= "2DP" Andro Id:valuetype= "Floattype"/> <!--you could has other objectanimator elements here for "X" and "Y" , or other properties-</set> </item> <item android:state_enabled= "true" Android:state_presse        D= "false" android:state_focused= "true" > <set> <objectanimator android:propertyname= "Translationz" android:duration= "android:valueto=" 0 "android:valuetype=" floattype "/> </set> </it Em></selector> 

To highly customize the view-State animation view, define the animation in this example using the Select element in the XML resource file and assign it to your view with the Android:statelistanimator property. The code assigns a state table animation to a view, using the Animationinflater.loadstatelistanimator () method, and the View and View.setstatelistanimator () method that the animation assigns to you.

When your theme expands on the theme of the material, the buttons are z-animated by default. To avoid this problem with your button, set the Android:statelistanimator property to @null.

The Animatedstatelistdrawable class is used to create animated drawings that display related view state changes. By default, some Android 5.0 system parts use these animations. The following example shows how to define a animatedstatelistdrawable for 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--> <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-<transition Andro Id:fromid= "@+id/default" android:toid= "@+id/pressed" > <animation-list> <item android:duratio N= "android:drawable=" @drawable/dt1 "/> <item android:duration=" "android:drawable=" @drawable/dt2 "/&G            T ... </animation-list> </transition> ...</animated-selector>


Animate Vector Drawables

Vector rendering is scalable without losing clarity. For the Animatedvectordrawable class, you can set the animated vector drawing properties.

You typically define an animated vector to draw objects in three XML files:

    A Vector drawable with the <vector> element in res/drawable/an    animated vector drawable with the <animated-v ector> element in res/drawable/one    or more object animators with the <objectAnimator> element in res/anim/


Vector animated objects can be animated < group> Properties and < path> elements. <GroupThe > element defines a set of paths or groups, and in the < pathThe > element defines the path to draw.

When you define the animation vector you want to draw, use the Android:name property to assign unique names to groups and paths so that you can define them from your animations. For example:

<!--res/drawable/vectordrawable.xml--><vector xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:height=" 64DP "    android:width=" 64DP "    android:viewportheight=" "    android: Viewportwidth= ">    <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>

Animation-drawn vectors are defined as groups and paths drawn by their name vectors:

<!--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 definition of an animation represents a Objectanimator or Animatorset object. In this example, the first animation rotates the target group 360 degrees:

<!--res/anim/rotation.xml--><objectanimator    android:duration= "6000"    android:propertyname= " Rotation "    android:valuefrom=" 0 "    android:valueto="/>

In this embodiment, the second animation of the transformed carrier can stretch the path from one shape to another. Two paths must be morph-compatible: they must have the same number of commands and the same number of parameters for each command.

<!--res/anim/path_morph.xml--><set xmlns:android= "http://schemas.android.com/apk/res/android" >    <objectanimator        android:duration= "android:propertyname="        pathdata "android:valuefrom="        M300 , 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>


Creating Apps with Material design--defining Custom animations

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.