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

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

Android中的动画分为三类。

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

我们根据类别,来分别介绍。

视图动画

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

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

视图动画可以通过Xml或Android代码中定义。使用起来比较方便。

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

<?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>

在代码中这样应用。

//@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内容,参考了该篇博客,在此表示感谢)。

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)联系

<p >
		 欢迎转载,转载请注明出处.谢谢
</p>


<script type="text/javascript">
 function    Curgo()   
 {   
     window.open(window.location.href);
 }   
</script>
posted @ 2017-09-09 23:21  eleven_yw  阅读(954)  评论(0编辑  收藏  举报