Android动画之---补间动画

补间动画也叫Tween动画,对某个View操作,使其展现出平移,缩放,旋转以及渐变一种转换过程,它值需要提供开始和结束相关信息,中间过程并没有显示,而是在变化过程中由系统计算并显示,所以叫做补间动画。

补间动画可以使用XML标签表示或者java代码来实现

名称XML标签JAVA类
渐变动画< alpha >AlaphAnimation
平移动画< translate >TranslateAnimation
缩放动画< scale >ScaleAnimation
旋转动画< rotate >RotateAnimation
集合动画< set >AnimationSet

上面表格中的集合动画是上述四种动画的任意组合,这里只是为了学习方便,才这样写! 
上面的java类都是继承了抽象类Animtion,先看看Animation中比较重要的方法:

方法名作用备注
setDuration(long durationMillis)设置动画持续的时间长度durationMillis不能为负数
setRepeatCount(int repeatCount)设置动画重复的次数repeatCount<0的话,方法内部会设为无限次
setRepeatMode(int repeatMode)设置动画重复的模式Animation.REVERSE:从结束位置反过来进行动画;Animation.INFINITE:从开始位置重新进行动画
setFillBefore(boolean fillBefore)设置是否保存动画开始之前的状态 
setFillAfter(boolean fillAfter)设置是否保持动画结束之后的状态 
setStartTime(long startTimeMillis)设置动画开始的时间 
setStartOffset(long startOffset设置动画之间开始的时间间隔 
setInterpolator(Interpolator i)设置动画的差值器 

下面就逐一进行学习 
1.渐变动画 
1.1,java代码实现,使用AlaphAnimation类

                Animation aiAnimation = new AlphaAnimation(0.1f, 1.0f);//从透明到不透明
                aiAnimation.setDuration(1000);//设置持续时间
                aiAnimation.setFillAfter(false);//设置不保持动画结束的状态
                aiAnimation.setRepeatMode(Animation.REVERSE);//设置从结束位置反过来进行动画模式
                aiAnimation.setRepeatCount(Animation.INFINITE);//设置动画无限循环的次数
                iv.startAnimation(aiAnimation);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

1.2,XML实现,使用< alaph >标签 
在res/anim文件夹下创建一个文件anim_alaph.xml,内容如下:

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="false"
    android:fromAlpha="0.1"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toAlpha="1.0">

</alpha>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在java代码中只需要

                Animation aiAnimation = AnimationUtils.loadAnimation(SubActivity.this, R.anim.anim_alaph);//动画工具加载动画
                iv.setAnimation(aiAnimation);
  • 1
  • 2

上面两种方式可以实现同样的动画效果; 
这里写图片描述

2.平移动画 
2.1TranslateAnimation类用于实现平移动画,它主要有两个构造方法 
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 
public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, 
int fromYType, float fromYValue, int toYType, float toYValue) 
这连个构造方法里面涉及如下变量值; 
/** 
* The specified dimension is an absolute number of pixels. 
*/ 
public static final int ABSOLUTE = 0; 
/** 
* The specified dimension holds a float and should be multiplied by the 
* height or width of the object being animated. 
*/ 
public static final int RELATIVE_TO_SELF = 1; 
/** 
* The specified dimension holds a float and should be multiplied by the 
* height or width of the parent of the object being animated. 
*/ 
public static final int RELATIVE_TO_PARENT = 2; 
关于这些,可以参考浅谈Android视图动画中的坐标系问题 
java代码实现方式:

        TranslateAnimation animation = new TranslateAnimation(50, 300, 50, 300);
        animation.setDuration(2000);
        animation.setFillAfter(true);
        animation.setInterpolator(new AccelerateDecelerateInterpolator());
        iv.startAnimation(animation);
  • 1
  • 2
  • 3
  • 4
  • 5

XML实现方式:

在res/anim文件夹下创建一个文件anim_translate.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fillAfter="true"
    android:fromXDelta="50"
    android:fromYDelta="50"
    android:interpolator="@android:interpolator/accelerate_decelerate"
    android:toXDelta="300"
    android:toYDelta="300">

</translate>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在代码中调用;

 Animation aiAnimation = AnimationUtils.loadAnimation(SubActivity.this, R.anim.anim_translate);//动画工具加载动画
 iv.setAnimation(aiAnimation);
  • 1
  • 2

上面运行的效果是一样的: 
这里写图片描述

3.缩放动画 
1.public ScaleAnimation(float fromX, float toX, float fromY, float toY) 
fromX:表示x缩放的起始点缩放比例 toX:表示x缩放的结束点缩放比例 
fromY:表示Y缩放的起始点缩放比例 toY:表示y缩放的结束点缩放比例 
这个构造函数默认缩放中心在(0,0), X和Y的缩放坐标类型都是Animation.ABSOLUTE 
2.public ScaleAnimation(float fromX, float toX, float fromY, float toY,float pivotX, float pivotY) 
//这个构造可以自己设置缩放的中心点坐标 X和Y的缩放坐标类型都是Animation.ABSOLUTE 
3.public ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
//这个构造可以自己设置缩放的起始点和终点坐标,缩放中心点 ,XY缩放的类型 
java代码实现:

ScaleAnimation aiAnimation = new ScaleAnimation(1f, 0.5f, 1f, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
aiAnimation.setDuration(1500);
aiAnimation.setRepeatMode(Animation.REVERSE);
aiAnimation.setRepeatCount(Animation.INFINITE);
iv.startAnimation(aiAnimation);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

XML的实现:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1500"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toXScale="0.5"
    android:toYScale="0.5"
    >

</scale>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

java加载

 Animation aiAnimation = AnimationUtils.loadAnimation(SubActivity.this, R.anim.anim_scale);
 iv.setAnimation(aiAnimation);
  • 1
  • 2

运行效果: 
这里写图片描述 
4.旋转动画 
旋转动画构造就不贴了 
java代码:

        RotateAnimation aiAnimation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        aiAnimation.setDuration(1500);
        aiAnimation.setRepeatMode(Animation.REVERSE);
        aiAnimation.setRepeatCount(Animation.INFINITE);
        iv.startAnimation(aiAnimation);
  • 1
  • 2
  • 3
  • 4
  • 5

XML实现:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1500"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toDegrees="180">

</rotate>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

java代码加载:

Animation aiAnimation = AnimationUtils.loadAnimation(SubActivity.this, R.anim.anim_rotate);
 iv.setAnimation(aiAnimation);
  • 1
  • 2

运行效果图: 
这里写图片描述

集合动画 
多个动画的合集 
java实现如下:

     AnimationSet animationSet = new AnimationSet(true);
     //渐变动画
     AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0.05f);
     alphaAnimation.setDuration(3000);
     //缩放动画
     ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0.05f, 1f, 0.05f,    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
     scaleAnimation.setDuration(3000);
     //旋转动画
     RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
     rotateAnimation.setDuration(3000);
     //动画集合添加三个动画
     animationSet.addAnimation(alphaAnimation);
     animationSet.addAnimation(scaleAnimation);
     animationSet.addAnimation(rotateAnimation);
     iv.startAnimation(animationSet);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

使用XML也可以实现:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="3000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.01"
        android:toYScale="0.01" />
    <alpha
        android:duration="3000"
        android:fromAlpha="1"
        android:toAlpha="0.05" />
    <rotate
        android:duration="3000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />

</set>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

java代码加载

Animation aiAnimation = AnimationUtils.loadAnimation(SubActivity.this, R.anim.anim_set);
iv.setAnimation(aiAnimation);
  • 1
  • 2

上面运行的效果: 
这里写图片描述

补间动画除了上面四种形式,还有了些特殊的使用方式,比如LayoutAnimation可以用于控制ViewGroup中子元素的出场顺序,通常用于ListView中,比如下面的效果: 
这里写图片描述 
纯粹使用XML也可以达到效果 
1,res/anim文件夹下面先定义一个anim_item.xml文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:shareInterpolator="true">
    <translate
        android:duration="300"
        android:fromXDelta="100%"
        android:toXDelta="0%" />
    <alpha
        android:duration="300"
        android:fromAlpha="0.1"
        android:toAlpha="1.0" />

</set>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2,文件res/anim文件夹下载新建一个anim_layout.xml文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/anim_item"
    android:animationOrder="normal"
    android:delay="0.5"
    android:interpolator="@android:anim/accelerate_interpolator">

</layoutAnimation>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

解释一下 
android:animation表示为子元素指定具体的入场动画,这里是我们定义的anim_item.xml 
android:animationOrder表示子元素动画的顺序,有三个值noraml(正序)reverse(逆序)random(随机) 
android:delay表示子元素动画的 时间延迟,这里0.5是子View每个动画延迟150s,每个动画的周期300s(上面定义的)乘以0.5

3,在ListView的布局中引用 android:layoutAnimation=”@anim/anim_layout”

    <ListView
        android:id="@+id/lv_anim"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@id/btnAnimation"
        android:divider="@android:color/background_dark"
        android:dividerHeight="1px"
        android:layoutAnimation="@anim/anim_layout" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

当然是用java代码也可以实现效果

         Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_item);
        /**布局动画加载器,构建每个子view的动画效果*/
        LayoutAnimationController lac = new LayoutAnimationController(animation);
        lac.setDelay(0.5f);//设置子项动画时间间隔
        lac.setInterpolator(new AccelerateInterpolator());//设置加速
        lac.setOrder(LayoutAnimationController.ORDER_NORMAL);//设置是正序
        listview.setLayoutAnimation(lac);//设置给ListView
        listview.startLayoutAnimation();//启动
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值