android动画研究

Android的动画效果在3.0得到巨大的升级,重点推出了属性动画,Android也是在这个版本后,开发的App越来越流畅,动画效果越来越好,接下来的内容,主要分一下几点介绍研究的结果:

1、Android3.0前的tween animation(View Animation),frame animation(Frame Animation)两种动画介绍

tween animation以及frame animation动画都是基于View实现,实现原理关键在View的OnDraw函数里面,所以这两种动画没有改变View的相关自有属性,而是在显示的时候draw的过程结合canvas实现动画,这样子的动画设计优点在于操作简单,实现方便,对View的属性不会有任何影响,比如View的大小为40 x 40,通过scale放大10倍成400 x 400,但View的大小属性还是40 x 40,windowmanagerservice记录的还是40 x 40的大小,用户点击的时候,点击事件只会在40 x 40的区域,不会影响其他View的事件响应区域。××动画如果以xml形式实现需要把xml文件放在anim文件夹里面或者drawable文件夹里面。

(1) tween animation(补间动画):

tween animation动画可以xml形式实现或者代码实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@[package:]anim/interpolator_resource"
android:shareInterpolator=["true" | "false"] >

<alpha
android:fromAlpha="float"
android:toAlpha="float" />

<scale
android:fromXScale="float"
android:toXScale="float"
android:fromYScale="float"
android:toYScale="float"
android:pivotX="float"
android:pivotY="float" />

<translate
android:fromX="float"
android:toX="float"
android:fromY="float"
android:toY="float" />

<rotate
android:fromDegrees="float"
android:toDegrees="float"
android:pivotX="float"
android:pivotY="float" />

<set>
...
</set>
</set>

set是一个动画容器,与之相对应的Java对象是AnimationSet。它有两个属性:Android:interpolator代表一个插值器资源,可以引用系统自带插值器资源,也可以用自定义插值器资源,默认值是匀速插值器,android:shareInterpolator代表set里面的多个动画是否要共享插值器,默认值为true,即共享插值器,如果设置为false,那么set的插值器就不再起作用,我们要在每个动画中加入插值器。
scale是缩放动画,可以实现动态调控件尺寸的效果,与之对应的Java对象是ScaleAnimation。android:fromXScale属性代表起始的X方向上相对自身的缩放比例,浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍;android:toXScale属性代表结尾的X方向上相对自身的缩放比例,浮点值;android:fromYScale属性代表起始的Y方向上相对自身的缩放比例,浮点值;android:toYScale属性代表结尾的Y方向上相对自身的缩放比例,浮点值;android:pivotX属性代表缩放的中轴点X坐标,浮点值,android:pivotY属性代表缩放的中轴点Y坐标,浮点值,对于这两个属性,如果我们想表示中轴点为图像的中心,我们可以把两个属性值定义成0.5或者50%。
translate是位移动画,代表一个水平、垂直的位移。与之对应的Java对象是TranslateAnimation。android:fromXDelta属性代表起始X方向的位置,android:toXDelta代表结尾X方向上的位置,android:fromYScale属性代表起始Y方向上的位置,android:toYDelta属性代表结尾Y方向上的位置,以上四个属性都支持三种表示方式:浮点数、num%、num%p;如果以浮点数字表示,代表相对自身原始位置的像素值;如果以num%表示,代表相对于自己的百分比,比如toXDelta定义为100%就表示在X方向上移动自己的1倍距离;如果以num%p表示,代表相对于父类组件的百分比。
rotate是旋转动画,与之对应的Java对象是RotateAnimation。android:fromDegrees属性代表起始角度,浮点值,单位:度;android:toDegrees属性代表结尾角度,浮点值,单位:度;android:pivotX属性代表旋转中心的X坐标值,android:pivotY属性代表旋转中心的Y坐标值,这两个属性也有三种表示方式,数字方式代表相对于自身左边缘的像素值,num%方式代表相对于自身左边缘或顶边缘的百分比,num%p方式代表相对于父容器的左边缘或顶边缘的百分比。
另外,在动画中,如果我们添加了android:fillAfter=”true”后,这个动画执行完之后保持最后的状态;android:duration=”integer”代表动画持续的时间,单位为毫秒。

(2) frame animation(帧动画):

Frame动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似,我们称为逐帧动画。Frame动画可以被定义在XML文件中,也可以完全编码实现。

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>  
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot=["true" | "false"] >

<item
android:drawable="@[package:]drawable/drawable_resource_name"
android:duration="integer" />

</animation-list>

animation-list 必须作为frame animation的xml根元素,对应的java类为AnimationDrawable,AnimationDrawable动画实现需要注意的一点是不能放在Activity的OnCreate生命周期里面,因为AnimationDrawable动画的start函数需要Windowmanagerservice的支持,OnCreate里面windowmanagerservice还没有初始化完。建议放在OnResume里面

2、Android3.0后推出的属性动画Property

Animator介绍(ValueAnimator、ObjectAnimator、AnimationSet、TypeEvalutors、TimeInterplator),属性动画的实现是通过改变View的属性再RuqestLayout函数更新界面,所以属性动画的前提是该View具有该属性,所以属性动画面对的是属性,不一定是View,属性动画的关键类是:
ValueAnimation 属性动画的核心,主要与TypeEvalutors以及TimeInterplator结合使用,该类由TypeEvalutors以及TimeInterplator提供的属性值以及时间值实现对对象的动态状态改变,通过ValueAnimator.AnimatorUpdateListener对对象进行处理
TypeEvalutors 估值器,对特定的时间返回特定的值,用于描述对象的属性变化值(属性点)

1
2
3
4
5
6
7
8
public interface TypeEvaluator<T> {
/**
** fraction 取值0~1,已过去的时间比例
** startValue 开始值
** endValue 结束值
**/

public T evaluate(float fraction, T startValue, T endValue);
}

TimeInterplator 插值器,描述对象运动的时间点

1
2
3
4
5
6
public interface  TimeInterpolator {
/**
** input A value between 0 and 1.0 indicating our current point in the animation where 0 ** represents the start and 1.0 represents the end
**/

float getInterpolation(float input);
}

3 ViewDragHelp介绍

ViewDragHelp可以很轻松地帮我们处理触摸事件,按google官方的说法:

ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number
of useful operations and state tracking for allowing a user to drag and reposition
views within their parent ViewGroup.
主要的回调函数为:
tryCaptureView如何返回ture则表示可以捕获该view,你可以根据传入的第一个view参数决定哪些可以捕获
clampViewPositionHorizontal,clampViewPositionVertical可以在该方法中对child移动的边界进行控制,left , top 分别为即将移动到的位置.
onViewReleased 触摸松开时回调
settleCapturedViewAt
onEdgeDragStarted 边界触摸时回调

参考文章

Android动画学习笔记-Android Animation
贝塞尔曲线
Android ViewDragHelper完全解析 自定义ViewGroup神器
一步一步教你实现Periscope点赞效果

文章目录
  1. 1. 1、Android3.0前的tween animation(View Animation),frame animation(Frame Animation)两种动画介绍
    1. 1.1. (1) tween animation(补间动画):
    2. 1.2. (2) frame animation(帧动画):
  2. 2. 2、Android3.0后推出的属性动画Property
  3. 3. 3 ViewDragHelp介绍
    1. 3.1. 参考文章
,