Android动画(一)-视图动画与帧动画

项目中很久没用过动画了,因此关于动画的知识都忘光了。知识老是不用则忘。正好最近的版本要添加比较炫酷的动画效果,因此也借着这个机会,写博客来整理和总结关于动画的一些知识。也方便本身从此的查阅。html

Android中的动画分为三类。java

  • View animation:视图动画,也叫作 Tween(补间)动画。
  • Drawable animation:也叫作Frame 动画,帧动画。
  • Property animation: 属性动画。支持Android3.0以上版本。

咱们根据类别,来分别介绍。android

视图动画

视图动画是一种比较古老的,使用方式比较简单的动画。它能够在一个视图容器内执行一系列的简单变换(好比大小,旋转等)。它控制的是整个view。它支持四种效果。透明度旋转缩放位移。分别对应着 Animation的四个子类,AlphaAnimation,RotateAnimation,ScaleAnimationTranslateAnimationgit

可是视图动画有一点须要特别注意,那就是不具有交互性。什么意思呢?好比 一个Button进行了平移变换,已经从以前的A点,移动到了B点。可是你点击B点,该Button没反应,相反你点击A点,该Button才有反应。(虽然它的视图已经不显示在A点了)。因此说 视图动画改变的只是View的显示,却没有改变View的真实布局属性值。github

视图动画能够经过Xml或Android代码中定义。使用起来比较方便。api

若是在xml文件中使用动画,文件目录是res/anim/filename.xml,而且View动画既能够是单个动画,也能够由一系列动画组成:
它具体的使用方式以下:app

<?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:fromXDelta="float"
        android:toXDelta="float"
        android:fromYDelta="float"
        android:toYDelta="float" />
    <rotate
        android:fromDegrees="float"
        android:toDegrees="float"
        android:pivotX="float"
        android:pivotY="float" />
    <set>
        ...
    </set>
</set>

在代码中这样应用。ide

//@author  www.yaoxiaowen.com
//本文地址: http://www.cnblogs.com/yaoxiaowen/p/7499556.html
Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.filename);
myView.startAnimation(myAnim);

而若是不借助于xml文件,直接在java代码中定义,则相似这样使用:函数

//旋转动画, 旋转参考系为自身中心点  
//@author  www.yaoxiaowen.com
//本文地址: http://www.cnblogs.com/yaoxiaowen/p/7499556.html
RotateAnimation ra = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5F,
            RotateAnimation.RELATIVE_TO_SELF, 0.5F);

ra.setDuration(5000);
myView.startAnimation(ra);

知道了基本用法,那么咱们下面要作的,就是熟悉相关api了。。(具体api内容,参考了该篇博客,在此表示感谢)。oop

Animationabstract的,它也是AlphaAnimation等视图动画的基类。

Animation类相关属性方法以下:

xml属性 java方法 解释
android:detachWallpaper setDetachWallpaper(boolean) 是否在壁纸上运行
android:duration setDuration(long) 动画持续时间,毫秒为单位
android:fillAfter setFillAfter(boolean) 控件动画结束时是否保持动画最后的状态
android:fillBefore setFillBefore(boolean) 控件动画结束时是否还原到开始动画前的状态
android:fillEnabled setFillEnabled(boolean) 与android:fillBefore效果相同
android:interpolator setInterpolator(Interpolator) 设定插值器(指定的动画效果,譬如回弹等)
android:repeatCount setRepeatCount(int) 重复次数
android:repeatMode setRepeatMode(int) 重复类型有两个值,reverse表示倒序回放,restart表示从头播放
android:startOffset setStartOffset(long) 调用start函数以后等待开始运行的时间,单位为毫秒
android:zAdjustment setZAdjustment(int) 表示被设置动画的内容运行时在Z轴上的位置(top/bottom/normal),默认为normal

除此以外,Animation还有一些经常使用的方法:

Animation类的方法 解释
reset() 重置Animation的初始化
cancel() 取消Animation动画
start() 开始Animation动画
setAnimationListener(AnimationListener listener) 给当前Animation设置动画监听
hasStarted() 判断当前Animation是否开始
hasEnded() 判断当前Animation是否结束

Alpha相关属性:

xml属性 java方法 解释
android:fromAlpha AlphaAnimation(float fromAlpha, …) 动画开始的透明度(0.0到1.0,0.0是全透明,1.0是不透明)
android:toAlpha AlphaAnimation(…, float toAlpha) 动画结束的透明度,同上

Rotate相关属性:

xml属性 java方法 解释
android:fromDegrees RotateAnimation(float fromDegrees, …) 旋转开始角度,正表明顺时针度数,负表明逆时针度数
android:toDegrees RotateAnimation(…, float toDegrees, …) 旋转结束角度,正表明顺时针度数,负表明逆时针度数
android:pivotX RotateAnimation(…, float pivotX, …) 缩放起点X坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%作为初始点、50%p表示以当前View的左上角加上父控件宽高的50%作为初始点)
android:pivotY RotateAnimation(…, float pivotY) 缩放起点Y坐标,同上规律

Scale相关属性:

xml属性 java方法 解释
android:fromXScale ScaleAnimation(float fromX, …) 初始X轴缩放比例,1.0表示无变化
android:toXScale ScaleAnimation(…, float toX, …) 结束X轴缩放比例
android:fromYScale ScaleAnimation(…, float fromY, …) 初始Y轴缩放比例
android:toYScale ScaleAnimation(…, float toY, …) 结束Y轴缩放比例
android:pivotX ScaleAnimation(…, float pivotX, …) 缩放起点X轴坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%作为初始点、50%p表示以当前View的左上角加上父控件宽高的50%作为初始点)
android:pivotY ScaleAnimation(…, float pivotY) 缩放起点Y轴坐标,同上规律

Translate相关属性:

xml属性 java方法 解释
android:fromXDelta TranslateAnimation(float fromXDelta, …) 起始点X轴坐标(数值、百分数、百分数p,譬如50表示以当前View左上角坐标加50px为初始点、50%表示以当前View的左上角加上当前View宽高的50%作为初始点、50%p表示以当前View的左上角加上父控件宽高的50%作为初始点)
android:fromYDelta TranslateAnimation(…, float fromYDelta, …) 起始点Y轴从标,同上规律
android:toXDelta TranslateAnimation(…, float toXDelta, …) 结束点X轴坐标,同上规律
android:toYDelta TranslateAnimation(…, float toYDelta) 结束点Y轴坐标,同上规律

另外还有一个AnimationSet,它表明的是一系列动画的组合。
在代码当中咱们能够这样使用:

//@author  www.yaoxiaowen.com
//本文地址: http://www.cnblogs.com/yaoxiaowen/p/7499556.html
AnimationSet as = new AnimationSet(true);
as.setDuration(1000);

AlphaAnimation aa = new AlphaAnimation(0, 1);
aa.setDuration(1000);
as.addAnimation(aa);

TranslateAnimation ta = new TranslateAnimation(0, 100, 0, 200);
ta.setDuration(1000);
as.addAnimation(ta);

btn7.startAnimation(as);

可是值得注意的是,若是咱们对AnimationSet设置了一些属性,那么有些属性会影响到它所包含的子控件,而有些则不会。API文档上是这样解释的。

The way that AnimationSet inherits behavior from Animation is important to understand. Some of the Animation attributes applied to AnimationSet affect the AnimationSet itself, some are pushed down to the children, and some are ignored, as follows:

  • duration, repeatMode, fillBefore, fillAfter: These properties, when set on an AnimationSet object, will be pushed down to all child animations.
  • repeatCount, fillEnabled: These properties are ignored for AnimationSet.
  • startOffset, shareInterpolator: These properties apply to the AnimationSet itself.

视图动画是供View使用的,而View基类中和动画相关的经常使用方法以下:

View类的经常使用动画操做方法 解释
startAnimation(Animation animation) 对当前View开始设置的Animation动画
clearAnimation() 取消当View在执行的Animation动画

帧动画

帧动画是一种比较简单的动画,利用多张图片就像放电影那样轮流播放,而后就造成了动画效果。或者说像播放幻灯片也是一个道理。由于它实质上就是多张图,因此它也叫做 Drawable动画。系统提供了 AnimationDrawable类来使用 帧动画。该类和 Animation没有继承关系。

Goole官方demo给出的使用方式以下:

<!-- Animation frames are wheel0.png through wheel5.png
     files inside the res/drawable/ folder -->
 <animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/wheel0" android:duration="50" />
    <item android:drawable="@drawable/wheel1" android:duration="50" />
    <item android:drawable="@drawable/wheel2" android:duration="50" />
    <item android:drawable="@drawable/wheel3" android:duration="50" />
    <item android:drawable="@drawable/wheel4" android:duration="50" />
    <item android:drawable="@drawable/wheel5" android:duration="50" />
 </animation-list>

在java代码中加载使用该动画。

// Load the ImageView that will host the animation and
 // set its background to our AnimationDrawable XML resource.
 ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
 img.setBackgroundResource(R.drawable.spin_animation);

 // Get the background, which has been compiled to an AnimationDrawable object.
 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

 // Start the animation (looped playback by default).
 frameAnimation.start();

对于帧动画,注意如下几点就ok了。

  • android:oneshot属性:true表示动画只播放一次,而后中止在最后一帧上,false表示动画循环播放。
    item表明每一帧的图片, android:drawable表示具体图片引用,android:duration表示每一帧停留的时间。
  • AnimationDrawable#start()方法不能够在Activity#onCreate()方法中调用,这和AnimationDrawable还未彻底附着到window上有关,所以最好的调用时机是在Activity#onWindowFocusChanged()方法中。

Activity或fragment的切换动画

跳转Activity时,系统有默认的切换效果,不过咱们也能够自定义,只要直接调用Activity中的一个方法便可,

/**
 * @author  www.yaoxiaowen.com
 * @param enterAnim   进入Activity时,所须要的动画资源id, 传递 0 表明 不使用动画效果
 * @param exitAnim   离开Activity时,所须要的动画资源id, 传递 0 表明 不使用动画效果
 */
public void overridePendingTransition (int enterAnim, int exitAnim)

可是该方法必需要紧跟着startActivity(Intent)finish()方法后面当即被调用,不然没效果。
相似下面这样

//www.yaoxiaowen.com
@Override
public void finish() {
    super.finish();
    overridePendingTransition(0, R.anim.exit_anim);
}

而Fragment也能够添加切换动画。则要使用FragmentTransaction中的这个方法。

FragmentTransaction setCustomAnimations (int enter, int exit)

不过要注意,Activityfragment添加的动画都应该是 视图动画,而不是属性动画。


做者: www.yaoxiaowen.com

博客地址: www.cnblogs.com/yaoxiaowen/

github: https://github.com/yaowen369

欢迎对于本人的博客内容批评指点,若是问题,可评论或邮件(yaowen369@gmail.com)联系

欢迎转载,转载请注明出处.谢谢

相关文章
相关标签/搜索