Android : 视图动画的属性意思

本文详细介绍了Android视图动画的基本概念及其四大类型:透明度、缩放、位移和旋转动画,并提供了XML和代码实现方式及逐帧动画的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘要 : 本文记录了视图动画<alpha>, <scale>, <translate>, <rotate> 动画属性的大致意思和逐帧动画的大致使用。

我们有时候通过动画,去把一个视图包装的精美一些。让其增加好感。 android动画包含视图动画和属性动画。在这里对视图动画做一下常用到属性的记录。

从上图中可以看出 Animation 包含 alpha、scale、translate、rotate。我们可以通过 xml的方式 和 代码的的方式去设置一个动画或者一个动画的集合。

Animation


可以看到 AlphaAnimation、TranslateAnimation、RotateAnimation、ScaleAnimation、AnimationSet 都是继承与Animation 。对于它的属性子类就可以使用。
其定义的属性有 :

<declare-styleable name="Animation">
        <!-- Defines the interpolator used to smooth the animation movement in time. -->
        <attr name="interpolator" />
        <!-- When set to true, the value of fillBefore is taken into account. -->
        <attr name="fillEnabled" format="boolean" />
        <!-- When set to true or when fillEnabled is not set to true, the animation transformation
             is applied before the animation has started. The default value is true. -->
        <attr name="fillBefore" format="boolean" />
        <!-- When set to true, the animation transformation is applied after the animation is
             over. The default value is false. If fillEnabled is not set to true and the
             animation is not set on a View, fillAfter is assumed to be true.-->
        <attr name="fillAfter" format="boolean" />
        <!-- Amount of time (in milliseconds) for the animation to run. -->
        <attr name="duration" />
        <!-- Delay in milliseconds before the animation runs, once start time is reached. -->
        <attr name="startOffset" format="integer" />
        <!-- Defines how many times the animation should repeat. The default value is 0. -->
        <attr name="repeatCount" format="integer">
            <enum name="infinite" value="-1" />
        </attr>
        <!-- Defines the animation behavior when it reaches the end and the repeat count is
             greater than 0 or infinite. The default value is restart. -->
        <attr name="repeatMode">
            <!-- The animation starts again from the beginning. -->
            <enum name="restart" value="1" />
            <!-- The animation plays backward. -->
            <enum name="reverse" value="2" />
        </attr>
        <!-- Allows for an adjustment of the Z ordering of the content being
             animated for the duration of the animation.  The default value is normal. -->
        <attr name="zAdjustment">
            <!-- The content being animated be kept in its current Z order. -->
            <enum name="normal" value="0" />
            <!-- The content being animated is forced on top of all other
                 content for the duration of the animation. -->
            <enum name="top" value="1" />
            <!-- The content being animated is forced under all other
                 content for the duration of the animation. -->
            <enum name="bottom" value="-1" />
        </attr>
        <!-- Special background behind animation.  Only for use with window
             animations.  Can only be a color, and only black.  If 0, the
             default, there is no background. -->
        <attr name="background" />
        <!-- Special option for window animations: if this window is on top
             of a wallpaper, don't animate the wallpaper with it. -->
        <attr name="detachWallpaper" format="boolean" />
    </declare-styleable>

可能用到属性的大致意思 :

属性含义
android:interpolator插值器,可以设置动画速率的变化,比如加速、减速、与加速等等。需要指定 Interpolator资源
android:fillEnabled设置为true时,android:fillBefore的值才有效,否则android:fillBefore会被忽略
android:fillBefore设置为true时,待动画执行完成后,会回到动画执行前的状态。默认为 true
android:fillAfter设置为 true时,待动画执行完成后,会停留在最后的状态视图。默认为false;如果是动画集合<set> 需要在<set> 标签中设置该属性才有效
android:duration动画从开始到执行完成所花费的时间(单位:毫秒值)
android:startOffset设置动画在执行之前需要等待的时间(单位:毫秒值),若是重复执行,则每次执行都需要等待
android:repeatCount设置动画重复执行的次数。默认值为0.既不重复;可设置为 -1 或 INFINITE (int INFINITE = -1),表示无线重复
android:repeatMode设置动画的重复执行的模式,可以设置为以下两个值
  • restart 动画从开始重复执行,默认值
  • reverse 动画反方向重复
android:zAdjustment表示被设置动画的内容在动画运行时在Z轴上的位置,取值为以下三个值之一:
  • normal 默认值,保持内容在Z轴上的位置不变
  • top 保持在Z周最上层
  • bottom 保持在Z轴最下层
android:detachWallpaper设置是否在壁纸上运行,只对设置了壁纸背景的窗口动画(window animation)有效。设为true,则动画只在窗口运行,壁纸背景保持不变



android:interpolator

  通过interpolator可以定义动画速率变化的方式,比如加速、减速、匀速等,每种interpolator都是 Interpolator 类的子类,Android系统已经实现了多种interpolator,对应也提供了公共的资源ID,如下表:

Interpolator classResource IDDescription
AccelerateDecelerateInterpolator@android:anim/accelerate_decelerate_interpolator在动画开始与结束的地方速率变换比较慢,在中间的时候加速
AccelerateInterpolator@android:anim/accelerate_interpolator在动画开始的时候速率比较慢,然后开始加速
AnticipateInterpolator@android:anim/anticipate_interpolator 开始的时候向后然后向前甩
AnticipateOvershootInterpolator@android:anim/anticipate_overshoot_interpolator开始的时候向后然后向前甩一定值后返回最后的值
BounceInterpolator@android:anim/bounce_interpolator动画结束的时候弹起
CycleInterpolator@android:anim/bounce_interpolator动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator@android:anim/decelerate_interpolator在动画开始的地方快然后慢
LinearInterpolator@android:anim/decelerate_interpolator 以常量速率改变
OvershootInterpolator@android:anim/overshoot_interpolator向前甩一定值后再回到原来位置

如果系统提供的以上 Interpolator不符合效果的话,也可以通过自定义。有如下二种方式 :

  • 直接继承 Interpolator 或者其子类
  • 通过自定义Xml文件,可以更改上面 Interpolator属性。
      -注意,自定的 Xml文件需要存放于res/anim/目录下。跟标签与上面相应。
Interpolator classDescription
< accelerateDecelerateInterpolator >在动画开始与结束时速率改变比较慢,在中间的时候加速。没有可以更改设置属性。
< accelerateInterpolator >在动画开始时速率比较慢,然后开始加速。有一个属性可以设置加速的速率
  • android:factor 浮点值,加速的速率,默认为1
< anticipateInterpolator >动画开始的时候向后然后往前抛。有一个属性设置向后拉的值
  • android:tension 浮点值,向后的拉力,默认为2,当设为0时,则不会有向后的动画了
< anticipateOvershootInterpolator >动画开始的时候向后然后向前抛,会抛超过目标值后再返回到最后的值。可设置两个属性
  • android:tension 浮点值,向后的拉力,默认为2,当设为0时,则不会有向后的动画了
  • android:extraTension 浮点值,拉力的倍数,默认为1.5(2*1.5),当设为0时,则不会有拉力了
< bounceInterpolator >动画结束的时候会弹跳。没有可更改设置的属性
< cycleInterpolator >动画循环做周期运动,速率改变沿着正弦曲线。有一个属性设置循环次数
  • android:cycles 整数值,循环的次数,默认为1
< decelerateInterpolator >在动画开始时速率改变比较快,然后开始减速。有一个属性设置减速的速率
  • android:factor 浮点值,减速的速率,默认为1
< linearInterpolator >动画匀速播放。没有可更改设置的属性
< overshootInterpolator >动画向前抛,会抛超过最后值,然后再返回。有一个属性
  • android:tension 浮点值,超出终点后的拉力,默认为2



通过setAnimationListener()可以对动画进行监听。从而实现多个动画的接连播放。

public void setAnimationListener(AnimationListener listener) ;

//三种时间状态下的监听回调
public static interface AnimationListener {
          //开始
        void onAnimationStart(Animation animation);

         //结束
        void onAnimationEnd(Animation animation);

         //重复
        void onAnimationRepeat(Animation animation);
    }

AlphaAnimation


AlphaAnimation对应Xml的 < alpha> 标签,可以实现透明度渐变的动画效果,也就是淡入淡出的效果。其定义的属性有:

    <declare-styleable name="AlphaAnimation">
        <attr name="fromAlpha" format="float" />
        <attr name="toAlpha" format="float" />
    </declare-styleable>
属性含义
android:fromAlpha 动画开始时的透明度,0.0为全透明,1.0为不透明,默认为1.0
android:toAlpha 动画结束时的透明度,0.0为全透明,1.0为不透明,默认为1.0
当设置开始时透明度为0.0,结束时为1.0,就能实现淡入效果;相反,当设置开始时透明度为1.0,结束时为0.0,那就能实现淡出效果。示例代码如下:
setAlphaType(1, 0);

private void setAlphaType(float fromAlpha, float toAlpha){
        AlphaAnimation animation = new AlphaAnimation(fromAlpha, toAlpha);
        animation.setDuration(1800) ;
        animation.setRepeatMode(Animation.REVERSE) ;
        animation.setRepeatCount(2) ;
        tv.startAnimation(animation) ;
    }
如果是加载写好的 Xml
****.startAnimation(AnimationUtils.loadAnimation(this, R.anim.****));
运行如下 :

TranslateAnimation


TranslateAnimation对应Xml的 < translate> 标签,可以实现位置移动的动画效果,可以是垂直方向的移动,也可以是水平方向的移动。其定义属性有:

 <declare-styleable name="TranslateAnimation">
        <attr name="fromXDelta" format="float|fraction" />
        <attr name="toXDelta" format="float|fraction" />
        <attr name="fromYDelta" format="float|fraction" />
        <attr name="toYDelta" format="float|fraction" />
    </declare-styleable>
属性含义
android:fromXDelta起始位置的X坐标的偏移量
android:toXDelta结束位置的X坐标的偏移量
android:fromYDelta起始位置的Y坐标的偏移量
android:toYDelta结束位置的Y坐标的偏移量
坐标的值可以有三种格式 :
类中字段xml中字段描述
Animation.ABSOLUTE直接写数字 如 :100表示相对于 View本身的具体像素值
Animation.RELATIVE_TO_SELF以 % 结束 如:50%表示相对于 View本身的百分比位置
Animation.RELATIVE_TO_PARENT以 p%结束 如:50P%表示相对于View的父View的百分比位置
以下代码为绝度像素[100,100],相对于自身为 3倍自身,相对于父控件 1倍。
setTranslateType(Animation.ABSOLUTE,0,100,0,100);
setTranslateType(Animation.RELATIVE_TO_SELF,0,3,0,3);
setTranslateType(Animation.RELATIVE_TO_PARENT,0,1,0,1);
运行结果如下 :

RotateAnimation


AlphaAnimation对应Xml的 < rotate> 标签,可以实现旋转的动画效果。其定义属性有 :

 <declare-styleable name="RotateAnimation">
        <attr name="fromDegrees" />
        <attr name="toDegrees" />
        <attr name="pivotX" />
        <attr name="pivotY" />
    </declare-styleable>
属性含义
android:fromDegrees旋转开始的角度
android:toDegrees旋转结束的角度
android:pivotX旋转中心点的X坐标
android:pivotY旋转中心点的Y坐标
旋转中心坐标的值可以有三种格式 :
类中字段xml中字段描述
Animation.ABSOLUTE直接写数字 如 :100表示相对于 View本身的具体像素值
Animation.RELATIVE_TO_SELF以 % 结束 如:50%表示相对于 View本身的百分比位置
Animation.RELATIVE_TO_PARENT以 p%结束 如:50P%表示相对于View的父View的百分比位置
以下代码为绝对像素,相对于自身,相对于父控件在 2秒中旋转 1080°
setRotateType(0, 1080, Animation.ABSOLUTE, 0.5f, Animation.ABSOLUTE, 0.5f) ;
setRotateType(0, 1080, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f) ;
setRotateType(0, 1080, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f) ;
运行结果如下 :

ScaleAnimation


AlphaAnimation对应Xml的 < scale> 标签,可以实现缩放的动画效果,其定义属性有:

<declare-styleable name="ScaleAnimation">
        <attr name="fromXScale" format="float|fraction|dimension" />
        <attr name="toXScale" format="float|fraction|dimension" />
        <attr name="fromYScale" format="float|fraction|dimension" />
        <attr name="toYScale" format="float|fraction|dimension" />
        <attr name="pivotX" />
        <attr name="pivotY" />
    </declare-styleable>
属性含义
android:fromXScale动画开始时X坐标上的缩放尺寸
android:toXScale动画结束时X坐标上的缩放尺寸
android:fromYScale动画开始时Y坐标上的缩放尺寸
android:toYScale动画结束时Y坐标上的缩放尺寸
android:pivotX缩放时的固定不变的X坐标
android:pivotY 缩放时的固定不变的Y坐标
缩放中心坐标的值可以有三种格式(一般用百分比表示,0%表示左边缘,100%表示右边缘) :
类中字段xml中字段描述
Animation.ABSOLUTE直接写数字 如 :100表示相对于 View本身的具体像素值
Animation.RELATIVE_TO_SELF以 % 结束 如:50%表示相对于 View本身的百分比位置
Animation.RELATIVE_TO_PARENT以 p%结束 如:50P%表示相对于View的父View的百分比位置
以下代码为绝对像素,相对于自身,相对于父控件 的0.5,进行放大 2倍
setScaleType(0, 2, 0, 2, Animation.ABSOLUTE, 0.5f, Animation.ABSOLUTE, 0.5f) ;
setScaleType(0, 2, 0, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f) ;
setScaleType(0, 2, 0, 2, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f) ;
运行结果如下 :

AnimationSet


AnimationSet对应Xml的 < set> 标签,可以将多个动画组合起来,变成一个动画集。比如想将一张图片缩放的同时也做移动,这时候就要用标签组合缩放动画和移动动画了。其定义属性如下 :

<declare-styleable name="AnimationSet">
        <attr name="shareInterpolator" format="boolean" />
        <attr name="fillBefore" />
        <attr name="fillAfter" />
        <attr name="duration" />
        <attr name="startOffset" />
        <attr name="repeatMode" />
    </declare-styleable>
以上的属性在 Animation中有,描述一下 shareInterpolator的属性 :
android:shareInterpolator
  shareInterpolator取值true或false,取true时,指在AnimationSet中定义一个插值器(interpolater),它下面的所有动画共同。如果设为false,则表示它下面的动画自己定义各自的插值器。
<set>标签在视图动画中除了可以组合<alpha>, <scale>, <translate>, <rotate>这四种标签,也可以嵌套其他<set>标签。 也可以addAnimation() 方法加入动画
public void addAnimation (Animation a)

AnimationDrawable控制逐帧动画


通过< animation-list > 标签,可以实现逐帧动画 。一般使用步骤有以下 3步:
  1. res/drawable 文件夹下新建一个xml文件,该文件详细定义了动画播放时所用的图片、切换每张图片
  2. 在代码中,将该动画布局文件,赋值给特定的图片展示控件,如本例子中的ImageView。
  3. 通过 iv.getBackGround()获取相应的AnimationDrawable对象,然后通过该对象的方法进行控制动画
存放图片的 xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false"
    >

    <item
        android:drawable="@drawable/azs"
        android:duration="200" />
    <item
        android:drawable="@drawable/azt"
        android:duration="200" />

省略若干图片 ... 
</animation-list>

<!--   
    根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画  
    根标签下,通过item标签对动画中的每一个图片进行声明  
    android:duration 表示展示所用的该图片的时间长度  
 -->  
在Java中的代码 :
private void startAnimationDrawable() {
        iv.setBackgroundResource(R.drawable.animation_list);
         a=(AnimationDrawable) iv.getBackground() ;
        a.start() ;
    }

private void stopAnimationDrawable() {
        if(a.isRunning()){
            a.stop() ;
        }
    }
运行结果:

可能会用到的方法 :
setOneShot(boolean flag) 和在配置文件中进行配置一样,可以设置动画是否播放一次,false为连续播放;
addFrame (Drawable frame, int duration) 动态的添加一个图片进入该动画中

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值