Android 动画总结

一.Android 动画分类
1,View Animation (补间动画)
2,Frame Animation (帧动画)
3,Property Animation(属性动画)

二.动画的详细介绍
1,View Animation
View Animation又称为Tween Animation,即补间动画,它通过对场景里的不断做图像变换(平移、缩放、旋转、透明度)从而产生动画,它是一种渐进式动画,并且View Animation支持自定义。View animation只能应用于View对象,不能适用于属性,对于View animation,它只是改变了View对象绘制的位置,而没有改变View对象本身,其中点击事件不会随位置改变而改变并且在动画结束后默认的会回到最初的状态。
动画的父类是Animation,经常使用的是四个子类和一个动画集合。

公共属性如下:

XML属 性:android:detachWallpaper
关联方法:setDetachWallpaper(boolean)
注释说明:特殊选项窗口动画:如果这个窗口是墙纸的顶部,墙纸不与窗口同时动画

XML属 性:android:duration
关联方法:setDuration(long)
注释说明:动画执行时间

XML属 性:android:fillAfter
关联方法:setFillAfter(boolean)
注释说明:如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态

XML属 性:android:fillBefore
关联方法:setFillBefore(boolean)
注释说明:如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态

XML属 性:android:fillEnabled
关联方法:setFillEnabled(boolean)
注释说明:如果设置为true,将fillBefore设置考虑在内

XML属 性:android:interpolator
关联方法:setInterpolator(Interpolator)
注释说明:设置动画的变化速率

XML属 性:android:repeatCount
关联方法:setRepeatCount(int)
注释说明:设置动画重复执行的次数

XML属 性:android:repeatMode
关联方法:setRepeatMode(int)
注释说明:当到达结束和重复计数大于0或无穷大,定义动画行为。

XML属 性:android:startOffset
关联方法:setStartOffset(long)
注释说明:设置动画执行之前的等待时间

XML属 性:android:zAdjustment
关联方法:setZAdjustment(int)
注释说明:允许内容的Z-次序的调整被动画的动画的持续时间

公共方法:

void cancel():取消动画
boolean hasEnded():动画是否结束
boolean hasStarted():动画是否开始
void reset():重置动画
boolean getTransformation(long currentTime, Transformation outTransformation, float scale) 获取转换的指定时间点
void start():启动动画首次getTransformation(长,转换)被调用
void startNow():在当前时间以毫秒为单位启动动画
boolean getTransformation(long currentTime, Transformation outTransformation, float scale)
boolean getTransformation(long currentTime, Transformation outTransformation)
获取动画指定时间currentTime的Transformation(此方法用于重写)

补间动画的四个子类:

(1)TranslateAnimation(平移)
平移动画既使view沿着x,y轴移动
实现方式2种:
1,xml中
在res中创建anim文件夹,创建Animation resource file

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="100"//x轴的起始位置
    android:fromYDelta="100"//x轴的起始位置
    android:toXDelta="300"//x轴的终止位置
    android:toYDelta="300"//x轴的终止位置
    android:duration="2000"/>

使用:

TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(this,R.anim.tan);
 textView.startAnimation(translateAnimation);(textView是一个TextView控件)

2,直接在java代码中使用

	TranslateAnimation translateAnimation1 = new TranslateAnimation(100,300,100,300);
    translateAnimation1.setDuration(2000);//动画执行时间
    translateAnimation1.setFillAfter(true);//动画结束后是否停在结束动画的位置
    translateAnimation1.setInterpolator(new LinearInterpolator());//动画的插值器,怎么执行动画
    translateAnimation1.setAnimationListener(new Animation.AnimationListener() {//动画的监听
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    translateAnimation1.setRepeatCount(10);//动画的重复次数
    textView.startAnimation(translateAnimation1);

注意:尽管我们把view停留在动画结束的位置,但他的点击事件还是在原点。

(2)AlphaAnimation(透明度)
把view变成0-1之间的透明度
实现方式2种:
1,xml中
在res中创建anim文件夹,创建Animation resource file

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:toAlpha="1"//最后的透明度
    android:fromAlpha="0"//开始的透明度
    android:duration="2000"/>

使用:

AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils.loadAnimation(this,R.anim.tan);
textView.startAnimation(alphaAnimation);(textView是一个TextView控件)

2,直接在java代码中使用

	AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
    alphaAnimation.setDuration(2000);//动画执行时间
    alphaAnimation.setFillAfter(true);//动画结束后是否停在结束动画的位置
    alphaAnimation.setInterpolator(new LinearInterpolator());//动画的插值器,怎么执行动画
    alphaAnimation.setAnimationListener(new Animation.AnimationListener() {//动画的监听
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    alphaAnimation.setRepeatCount(-1);//动画的重复次数(-1永远执行)
    textView.startAnimation(alphaAnimation);

(3)ScaleAnimation(缩放)
把view按照某一个坐标点进行放大缩小
实现方式2种:
1,xml中
在res中创建anim文件夹,创建Animation resource file

<?xml version="1.0" encoding="utf-8"?>
<scale
        android:fromXScale="1.0"//x轴的初始值
        android:toXScale="0.2"//x轴的终止值
        android:fromYScale="1.0"//y轴的初始值
        android:toYScale="0.2"//y轴的终止值
        android:pivotX="50%"//相对于自身的那个点50%就是中点
        android:pivotY="50%"/>//相对于自身的那个点50%就是中点

使用:

ScaleAnimation scaleAnimation = (ScaleAnimation) AnimationUtils.loadAnimation(this,R.anim.tan);
textView.startAnimation(scaleAnimation);(textView是一个TextView控件)

2,直接在java代码中使用

	ScaleAnimation scaleAnimation = new ScaleAnimation(1,1.5f,1,1.5f,0.5f,0.5f);
    scaleAnimation.setDuration(2000);
    scaleAnimation.setFillAfter(true);
    scaleAnimation.setInterpolator(new LinearInterpolator());
    scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    scaleAnimation.setRepeatCount(1);
    textView.startAnimation(scaleAnimation);

(4)RotateAnimation(旋转)
把view按照某一个坐标点进行旋转
实现方式2种:
1,xml中
在res中创建anim文件夹,创建Animation resource file

<?xml version="1.0" encoding="utf-8"?>
<rotate
        android:fromDegrees="0"//起始角度
        android:toDegrees="+360"//终止角度
        android:pivotX="50%"//
        android:pivotY="50%"//
        android:duration="3000"/>

使用:

RotateAnimation rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(this,R.anim.tan);
textView.startAnimation(rotateAnimation);(textView是一个TextView控件)

2,直接在java代码中使用

	RotateAnimation rotateAnimation = new RotateAnimation(0,90,0.5f,0.5f);
    rotateAnimation.setDuration(2000);
    rotateAnimation.setFillAfter(true);
    rotateAnimation.setInterpolator(new LinearInterpolator());
    rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    rotateAnimation.setRepeatCount(1);
    textView.startAnimation(rotateAnimation);

(5)补间动画的集合动画AnimationSet
把上述的四种动画组合起来实现一种比较复杂的效果
实现方式2种:
1,xml中
在res中创建anim文件夹,创建Animation resource file

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true">
    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"/>
    <translate
        android:fromXDelta="100"
        android:fromYDelta="100"
        android:toYDelta="300"
        android:toXDelta="300"/>
    <scale
        android:fromXScale="1"
        android:toXScale="1.8"
        android:fromYScale="1"
        android:toYScale="1.5"/>
    <rotate
        android:fromDegrees="0"
        android:toDegrees="120"/>
</set>

使用:

AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(this,R.anim.b);
textView.startAnimation(animationSet);

2,直接在java代码中使用

	AnimationSet animationSet = new AnimationSet(true);
    animationSet.addAnimation(alphaAnimation)//通过add方法把上面的动画加进去即可
    textView.startAnimation(rotateAnimation);

注意;动画集合是在一段时间内完成所有动画而不是一个一个的进行

参考文档
https://blog.csdn.net/io_field/article/details/52915772

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白成长不了之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值