Creating Apps with Material design--defining Custom animations

Source: Internet
Author: User

reprint Please specify Http://blog.csdn.net/eclipsexys translation from developer Android, time haste, there are translation questions please leave a message to point out. Thanks


Defining animations

Provide feedback for their behavior when material design animations let users interact with your application. and provide visual continuity.

The subject of the material provides some default animated button and active transitions, and Android5.0 (API level 21) above, you can define these animations yourself and create new ones:


Touch Feedback
Notification display
Activity change
Curve motion
View state changes

Define your own 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, using the new Rippledrawable class, implements transitions between different states and produces a chain reaction animation.



In most cases, you should specify the view background by specifying it. This functionality is applied in the XML in the view:


    ? 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. You can 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 ();


Define Activity Transitions yourself

The activity of the material design application moves through the transitions between different states.

You can specify that you define the transitions between the entry and exit of animations and the sharing of content between 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-Add or remove scene views by changing its transparency.

Transition expands the visibility class regardless of what changes are supported as entry or exit transitions. For more information, please refer to 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.



Specify a transformation of your own definition
First of all. When you define a style that inherits the material's theme properties, the form content is converted Android:windowcontenttransitions. You can also specify entry. Exits, and specifies the transformation of the style shared element that you define:

<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 definition of the Change_image_transform transition is 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 a lot of other information, please refer to the conversion API.

To ensureThe form moves the animation realization, needsCall 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.



To get the full effect of a transition. You must enable the conversion of the content in these two called and called Active forms. Otherwise. The invoke activity initiates the exit transition, but then you see a transition of the form (such as scale or fade).

Start as soon as possible into the transition, using the Window.setallowentertransitionoverlap () method called activity. This gives you a lot of other dramatic transitions into transition animations.


Start an activity with a transform
Assume that the transition is enabled and set to the activity's exit transitions when you start another activity. For example, the following transitions are activated:

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

Suppose you set up a second activity that goes into transition, which is also activated when the activity starts.

To disable the conversion, when you start there is an activity that provides a null option.



To implement a screen transition animation between a common element
Make the content of your theme form convertible.
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 ());}    );

A shared dynamic view for your code generation. Use the View.settransitionname () method to specify a common element name in two activity.

To reverse the scene transition animation, when you're finished with the second activity, call the Activity.finishaftertransition () method. Rather than 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 with the shared elements of Android: the Transitionname property (or use View.settransitionname () in both active methods), and creates a Activityoptions object such as the following:

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 your own animations that define timing curves and curve motion patterns.

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. Use the (0,0) Anchor Point and (the) and control points to specify the parameters using the constructor.

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's Animator.setinterpolator () method.



The Objectanimator class has a new constructor. Enables you to use two or more attributes at the same time, at once in the path animation coordinates. 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 is able to define the change in view state when the animation is executed.

The following demo sample demonstrates 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 define your own view-state animation view, define the animations in this sample using the XML resource file to select the elements and assign them 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 subject of the material, the button is the Z animation 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 demo sample demonstrates 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 Animation can animate the < of 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 a unique name to the group and path. This way you will be able to define them from your animations. Like what:

<!--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 vector 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 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>

Ps:path draw, write more complex paths, need to use SVG editing

Creating Apps with Material design--defining Custom animations

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.