官网地址:https://developer.android.com/intl/zh-tw/training/material/animations.html

动画在Material设计中,为用户与app交互反馈他们的动作行为和提供了视觉上的连贯性。Material主题为Buttons和Activity的过渡提供了一些默认的动画。在android5.0(api21)及以上,同意自己定义这些动画:

· Touch feedback  触摸反馈

· Circular Reveal  循环显示

· Activity transitions  活动过渡

· Curved motion       曲线运动

· View state changes  视图状态变化

Customize Touch Feedback  自己定义触摸反馈动画

在Material设计中。触摸反馈提供了一种在用户与UI进行交互时 即时可视化的确认接触点。关于buttons默认的触摸反馈动画。使用了RippleDrawable类。用一个波纹(涟漪)效果在两种不同的状态间过渡。

在多数情况下,你须要在view的xml定义中,定义它的背景:

?

android:attr/selectableItemBackground                              有界限的波纹

?android:attr/selectableItemBackgroundBorderless             延伸到view之外的波纹     note:该属性为api21加入

或者,你能够用xml定义一个RippleDrawable类型的资源,并使用波纹属性。

你能够指定一个颜色给RippleDrawable对象,以改变它的默认触摸反馈颜色,使用主题的android:colorControlHighlight属性。

Use the Reveal Effect  使用展现效果

ViewAnimationUtils.createCircularReveal()方法使您可以激活一个循环显示或隐藏一个视图。

显示:

  1. // previously invisible view
  2. View myView = findViewById(R.id.my_view);
  3.  
  4. // get the center for the clipping circle
  5. int cx = (myView.getLeft() + myView.getRight()) / 2;
  6. int cy = (myView.getTop() + myView.getBottom()) / 2;
  7.  
  8. // get the final radius for the clipping circle
  9. int finalRadius = myView.getWidth();
  10.  
  11. // create and start the animator for this view
  12. // (the start radius is zero)
  13. Animator anim =
  14.     ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius);
  15. anim.start();

隐藏

  1. // previously visible view
  2. final View myView = findViewById(R.id.my_view);
  3.  
  4. // get the center for the clipping circle
  5. int cx = (myView.getLeft() + myView.getRight()) / 2;
  6. int cy = (myView.getTop() + myView.getBottom()) / 2;
  7.  
  8. // get the initial radius for the clipping circle
  9. int initialRadius = myView.getWidth();
  10.  
  11. // create the animation (the final radius is zero)
  12. Animator anim =
  13.     ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, 0);
  14.  
  15. // make the view invisible when the animation is done
  16. anim.addListener(new AnimatorListenerAdapter() {
  17.     @Override
  18.     public void onAnimationEnd(Animator animation) {
  19.         super.onAnimationEnd(animation);
  20.         myView.setVisibility(View.INVISIBLE);
  21.     }
  22. });
  23.  
  24. // start the animation
  25. anim.start();

Customize Activity Transitions  定义Activity的过渡动画

·一个enter transition表示。Activity的进入场景。比方一个explode enter transition,表示Views的进入场景:飞快的从外部向屏幕中心移动。

·一个exit transition表示,Activity的离开场景。比方一个explode exit transition,表示Views的离开场景:从屏幕中心散开。

·一个share transition表示,在两个Activity间共享它们的activity transtion。

比方,两个Activity有一个同样的图片,而位置和尺寸不同,使用changeImageTransform这个共享元素,能在Activity间平稳的转换和缩放图片。

android5.0(api21)及以上,支持这些效果的transition(过渡):

爆炸——移动视图或从场景中心。class Explode

滑行——移动视图或从一个场景的边缘。class Slide

淡入淡出——加入或从场景中删除视图通过改变其透明度。

class Fade

也支持这些共享元素(都有相应的class)转换:

  changeBounds ——View的布局的边界变化。

  changeClipBounds——View的裁剪边界变化。

  changeTransform——View的旋转、缩放边界变化

  changeImageTransform——目标图像的尺寸和缩放变化。

  当启用活动在你的应用程序转换,默认同一时候淡出淡入之间的过渡是激活进入和退出活动。

Specify custom transitions 自己定义过渡动画

首先须要在定义主题的style中,使用android:windowContentTransitions属性,声明使用transitions。也能够定义使用的Transitions:

<?xmlversion="1.0"encoding="utf-8"?>

<resources>

<stylename="MyTheme"parent="@android:style/Theme.Material">

<!-- enable window content transitions -->

<itemname="android:windowContentTransitions">true</item>

<!-- specify enter and exit transitions -->

<itemname="android:windowEnterTransition">@android:transition/explode</item>

<itemname="android:windowExitTransition">@android:transition/explode</item>

<!-- specify shared element transitions -->

<itemname="android:windowSharedElementEnterTransition">@android:transition/move</item>

<itemname="android:windowSharedElementExitTransition">@android:transition/slide_top</item>

</style>

</resources>

注:每一个transition的xml中定义的就是一组change的元素

在代码中启用transitions:

  1. // inside your activity (if you did not enable transitions in your theme)
  2. getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
  3.  
  4. // set an exit transition
  5. getWindow().setExitTransition(new Explode());

在代码中设置transitions的方法还有

要想尽快进行transitions过渡,可在Activity中调用Window.setAllowEnterTransitionOverlap()

Start an activity using transitions 使用过渡启动Activity

假设你要启用transtions并设置为一个Activity的结束exit transtion,当你以例如以下方式启动还有一个Activity时。它将被激活:

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

当你在还有一个Activity中设置了enter transtion。在其启动时,它将被激活。想要disable transitions,那么在启动还有一个Activity时:

startActivity(intent,null);
 //传递null 的options bundle

Start an activity with a shared element  使用一个共享元素启动Acitvity

1.在主题中启用window content

2.在style中定义共享的过渡transitions

3.定义transitions的xml资源  res/transition

4.在layout中调用android:transitionName="" 设置第3步中定义的名字

5.调用 ActivityOptions.makeSceneTransitionAnimation()生成对应的ActivityOptions对象。

  1. // get the element that receives the click event
  2. final View imgContainerView = findViewById(R.id.img_container);
  3.  
  4. // get the common element for the transition in this activity
  5. final View androidRobotView = findViewById(R.id.image_small);
  6.  
  7. // define a click listener
  8. imgContainerView.setOnClickListener(new View.OnClickListener() {
  9.     @Override
  10.     public void onClick(View view) {
  11.         Intent intent = new Intent(this, Activity2.class);
  12.         // create the transition animation - the images in the layouts
  13.         // of both activities are defined with android:transitionName="robot"
  14.         ActivityOptions options = ActivityOptions
  15.             .makeSceneTransitionAnimation(this, androidRobotView, "robot");
  16.         // start the new activity
  17.         startActivity(intent, options.toBundle());
  18.     }
  19. });

在代码中能够用View.setTransitionName()来设置过渡动画

当你要关闭第二个Activity时,要反转过渡动画,那么能够调用Activity.finishAfterTransition()方法,而不是Activity.finish()。

Start an activity with multiple shared elements  用多共享元素启动Activity

若两个Activity拥有不仅仅一个的共享元素,要在它们之间開始场景transition动画,在它们的layout中都要使用 android:transitionName (或在Activity中代码中调用View.setTransitionName()
)来定义。并创建一个例如以下的 ActivityOptions 对象:

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

Use Curved Motion 使用曲线运动

在Material设计中的动画。依赖于曲线的时间插入值和空间运动模式。在android5.0(api21)及以上,能够自己定义动画时间曲线和曲线运动模式。

PathInterpolator类是一个新的基于贝塞尔曲线或路径对象的插入器。这个插入器指定了一个1 x1正方形运动曲线。它使用(0,0)为锚点,(1,1)为控制点,作为构造函数的參数。

你也能够定义一个path interpolator的xml资源:

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

系统提供了三种主要的曲线,XML资源:

  • @interpolator/fast_out_linear_in.xml
  • @interpolator/fast_out_slow_in.xml
  • @interpolator/linear_out_slow_in.xml

您能够用PathInterpolator对象作Animator.setInterpolator()方法的參数。

ObjectAnimator类有新构造函数使您可以激活坐标沿着一个path同一时候使用两种或两种以上的属性。

比方。例如以下的animator就使用了一个path 对象。来同一时候操作View的x和y属性:

  1. ObjectAnimator mAnimator;
  2. mAnimator = ObjectAnimator.ofFloat(view, View.X, View.Y, path);
  3. ...
  4. mAnimator.start();

Animate View State Changes  视图状态改变动画

StateListAnimator类同意您定义动画执行时视图的状态变化。

以下的样例演示怎样在xml中定义一个StateListAnimator:

  1. <!-- animate the translationZ property of a view when pressed -->
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3.   <item android:state_pressed="true">
  4.     <set>
  5.       <objectAnimator android:propertyName="translationZ"
  6.         android:duration="@android:integer/config_shortAnimTime"
  7.         android:valueTo="2dp"
  8.         android:valueType="floatType"/>
  9.         <!-- you could have other objectAnimator elements
  10.              here for "x" and "y", or other properties -->
  11.     </set>
  12.   </item>
  13.   <item android:state_enabled="true"
  14.     android:state_pressed="false"
  15.     android:state_focused="true">
  16.     <set>
  17.       <objectAnimator android:propertyName="translationZ"
  18.         android:duration="100"
  19.         android:valueTo="0"
  20.         android:valueType="floatType"/>
  21.     </set>
  22.   </item>
  23. </selector>

在上例中,为一个View加入视图状态动画。定义了一个使用selector元素的xml资源。并赋给view的android:stateListAnimator属性。如要在代码中为View指定视图状态动画,可使用AnimationInflater.loadStateListAnimator()载入xml资源。并使用View.setStateListAnimator()将其指定给View。

当你的主题继承了Material主题。button默认拥有了z动画。

为了避免这样的行为在你的button。设置android:stateListAnimator属性值为null。

AnimatedStateListDrawable类同意您创建图片以显示关联View的状态改变动画。一些系统的Widget。在5.0上默认使用这些动画。

以下的样例显示了怎样定义一个AnimatedStateListDrawable作为XML资源:

  1. <!-- res/drawable/myanimstatedrawable.xml -->
  2. <animated-selector
  3.     xmlns:android="http://schemas.android.com/apk/res/android">
  4.  
  5.     <!-- provide a different drawable for each state-->
  6.     <item android:id="@+id/pressed" android:drawable="@drawable/drawableP"
  7.         android:state_pressed="true"/>
  8.     <item android:id="@+id/focused" android:drawable="@drawable/drawableF"
  9.         android:state_focused="true"/>
  10.     <item android:id="@id/default"
  11.         android:drawable="@drawable/drawableD"/>
  12.  
  13.     <!-- specify a transition -->
  14.     <transition android:fromId="@+id/default" android:toId="@+id/pressed">
  15.         <animation-list>
  16.             <item android:duration="15" android:drawable="@drawable/dt1"/>
  17.             <item android:duration="15" android:drawable="@drawable/dt2"/>
  18.             ...
  19.         </animation-list>
  20.     </transition>
  21.     ...
  22. </animated-selector>

Animate Vector Drawables  矢量图片动画

矢量图片是可伸缩而不失真的。

AnimatedVectorDrawable类让你能使一个矢量图动起来。

通常在三种xml定义动态的矢量图:

·使用<vector>元素的矢量图,在res/drawable/

·一个动态矢量图,使用<animated-vector>元素,在res/drawable/

·一个或多个object animator。使用<objectAnimator>元素。在res/animator/

矢量图能够定义的属性元素有<group>和<path>,<group>定义了一个<path>的集合,或者子<group>。<path>定义绘制的路径。

定义矢量图时。能够给<group>和<path>指定一个名字,示比例如以下:

  1. <!-- res/drawable/vectordrawable.xml -->
  2. <vector xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:height="64dp"
  4.     android:width="64dp"
  5.     android:viewportHeight="600"
  6.     android:viewportWidth="600">
  7.     <group
  8.         android:name="rotationGroup"
  9.         android:pivotX="300.0"
  10.         android:pivotY="300.0"
  11.         android:rotation="45.0" >
  12.         <path
  13.             android:name="v"
  14.             android:fillColor="#000000"
  15.             android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
  16.     </group>
  17. </vector>

在矢量动画中,引用矢量图定义的名字:

  1. <!-- res/drawable/animvectordrawable.xml -->
  2. <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
  3.   android:drawable="@drawable/vectordrawable" >
  4.     <target
  5.         android:name="rotationGroup"
  6.         android:animation="@anim/rotation" />
  7.     <target
  8.         android:name="v"
  9.         android:animation="@anim/path_morph" />
  10. </animated-vector>

下面样例代表了一个 ObjectAnimator or AnimatorSet 对象:动作为旋转360度

  1. <!-- res/anim/rotation.xml -->
  2. <objectAnimator
  3.     android:duration="6000"
  4.     android:propertyName="rotation"
  5.     android:valueFrom="0"
  6.     android:valueTo="360" />

以下的样例表示矢量图path从一个图形到还有一个。两种渐变路径必须一致:他们必须具有同样数量的命令和同样数量的每一个命令的參数:

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

很多其它详见:AnimatedVectorDrawable.

Android(Lollipop/5.0) Material Design(六) 自定义动画的更多相关文章

  1. Android(Lollipop/5.0) Material Design(六) 使用图像

    Material Design列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design(二 ...

  2. Android(Lollipop/5.0) Material Design(二) 入门指南

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简介 Android(Lollipop/5.0)Material Design(二) ...

  3. Android(Lollipop/5.0) Material Design(一) 简单介绍

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...

  4. Android(Lollipop/5.0) Material Design(四) 创建列表和卡片

    Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...

  5. Android(Lollipop/5.0) Material Design(一) 简介

    官网地址:https://developer.android.com/intl/zh-tw/design/material/index.html 使用Material Design 需要api21,即 ...

  6. Android最佳实践之Material Design

    Material概述及主题 学习地址:http://developer.android.com/training/material/get-started.html 使用material design ...

  7. [转]Android 5.0——Material Design详解(动画篇)

    Material Design:Google推出的一个全新的设计语言,它的特点就是拟物扁平化. Material Design包含了很多内容,今天跟大家分享一下Material新增的动画: 在Andr ...

  8. Android开发之Android Material Design Toolbar自定义随笔

    一.自定义Toolbar的menu: 在menu下新建menu.xml文件,自定义menu的样式: <menu xmlns:android="http://schemas.androi ...

  9. Android Lollipop 5.0 经典新特性回顾

    *Tamic 专注移动开发! 更多文章请关注 http://blog.csdn.net/sk719887916 虽然Android已到了7.0 ,但是我们还是不能忘怀视觉革命性改变的5.0,今天回顾下 ...

随机推荐

  1. POJ 3422 Kaka&#39;s Matrix Travels(费用流)

    POJ 3422 Kaka's Matrix Travels 题目链接 题意:一个矩阵.从左上角往右下角走k趟,每次走过数字就变成0,而且获得这个数字,要求走完之后,所获得数字之和最大 思路:有点类似 ...

  2. TabHost+RadioGroup搭建基础布局

    xml的形势如下: <tabhost> <linearlayout vertival> <framlayout weight=1/> <tabwidget g ...

  3. node.js抓取数据(fake小爬虫)

    在node.js中,有了 cheerio 模块.request 模块,抓取特定URL页面的数据已经非常方便. 一个简单的就如下 var request = require('request'); va ...

  4. 【C语言探索之旅】 第一部分第四课第一章:变量的世界之内存那档事

    内容简介 1.课程大纲 2.第一部分第四课第一章:变量的世界之内存那档事 3.第一部分第四课第二章预告:变量的世界之声明变量 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答 ...

  5. vs2012 网站无法使用自定义服务器的解决方法

    我已经习惯新建一个Asp.net网站时把它挂载在IIS下调试运行,在使用Visual Studio 2012后,新建网站配置启动选项时,自定义服务器居然不可用 原来是Visual Studio 201 ...

  6. mapreduce任务失败、重试、猜測式运行机制小结

    mapreduce中我们自己定义的mapper和reducer程序在运行后有可能遇上出错退出的情况,mapreduce中jobtracker会全程追踪任务的运行情况,对于出错的任务mapreduce也 ...

  7. 从Java到C++——从union到VARIANT与CComVariant的深层剖析

    我第一次用VARIANT和CComVariant的时候全然不明确它是怎么回事,为它什么数据类型都能够存放.什么数据都能够被能够初始化?里面究竟是怎么实现的?感觉又奇妙又迷惑!我们在上一节中讲了unio ...

  8. JavaScipt面向对象编程----闭包

    在javascript中闭包是一个非常不好理解的概念,可是确实一个不可逃避的东西,那么今天我们就来一起学习一下闭包. 什么是闭包? 闭包:官方"的解释是:闭包是一个拥有很多变量和绑定了这些变 ...

  9. UBuntu经常使用的操作(网络资源)

    http://docs.google.com/Doc? id=dqsbw4c_46d89djccr 版权声明:本文博主原创文章.博客,未经同意不得转载.

  10. DNA和纳米(Nano)Fusion技术的发展趋势

    细观国内外有关DNA与Nano技术的报道.不得不承认存在不小的差距,并且差距有继续拉大的趋势. 在我们国内,DNA技术是作为遗产基因来对待的.引用的有关中文參考资料一般比較陈旧.缺少參考价值.在发达国 ...