一、概述
动画是动态变化的效果,可以分为两类
- 帧动画(又称图像动画)
帧动画的原理是drawable中放几张不同的图像,图像不断切换(放电影一样),产生动态动画的效果。 - 补间动画(又称View动画)
对一张动画进行操作,让动画形成运动的轨迹。但是,实际上,图片的位置是初始位置,不会变的 - 属性动画
其实是补间动画发展来的,用了面向对象的思想,把动画用到的属性进行封装(类似于javaBean,暴露get/set接口),使用过程就是面向对象调用方法的过程。注意到,图片在屏幕上是真的在变化的。
二、帧动画
直接说操作步骤:
- 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/logo1" android:duration="200" />
<item android:drawable="@drawable/logo2" android:duration="200" />
<item android:drawable="@drawable/logo3" android:duration="200" />
</animation-list>
- java 代码
//播放动画
AnimationDrawable anim = (AnimationDrawable) iv_title.getBackground();
anim.start();
三、补间动画
被间动画是让程序操纵其轨迹,形成动态效果。被单动画分为四类,动画效果之间可以相互组合,从而形成更加丰富的效果。使用补间动画的要注意一点,动画的位置是不变的(如只能点击原始位置才有事件,运动中的位置是没有效果的)。
- 旋转动画
- 缩放
- 透明度渐变
- 平移
1.用java代码来实现
就是全部的实现都用java代码来实现,通过一不同动画效果的Animation,控制图片或其它资源形成动画效果。
- 旋转动画 RotateAnimation
- 缩放 ScaleAnimation
- 透明度渐变 AlphaAnimation
- 平移 TranslateAnimation
举例一:
/**
* Animation.ABSOLUTE :代表的是后面给定的值是绝对的长度, 像素个数。
* Animation.RELATIVE_TO_SELF : 参照的是自己的宽高倍数 , 后面的值说的是倍数
* Animation.RELATIVE_TO_PARENT : 代表的是父元素的倍数: 在这代表整个屏幕
*
*/
TranslateAnimation anim = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0.5f,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0.5f);
//定义时间长度
anim.setDuration(2000);
//设置动画无限循环播放
anim.setRepeatCount(Animation.INFINITE);
//设置动画循环的模式 , reverse ,播放完毕之后,倒着播放
anim.setRepeatMode(Animation.REVERSE);
//指定让这个控件播放当前的动画
iv.startAnimation(anim);
举例二:AnimationSet的组合动画
public void set(View v){
RotateAnimation anim = new RotateAnimation(
0, 360 , //从什么角度旋转到什么角度
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
// 定义时间长度
anim.setDuration(2000);
// 设置动画无限循环播放
anim.setRepeatCount(Animation.INFINITE);
// 设置动画循环的模式 , reverse ,播放完毕之后,倒着播放
anim.setRepeatMode(Animation.REVERSE);
ScaleAnimation sanim = new ScaleAnimation(
3, -3, //初始的倍数到什么倍数
3, -3/**/,
Animation.RELATIVE_TO_SELF, 0.5f, //从自己的中心缩放
Animation.RELATIVE_TO_SELF, 0.5f);
// 定义时间长度
sanim.setDuration(2000);
// 设置动画无限循环播放
sanim.setRepeatCount(Animation.INFINITE);
// 设置动画循环的模式 , reverse ,播放完毕之后,倒着播放
sanim.setRepeatMode(Animation.REVERSE);
//shareInterpolator --是否共用同一个插入器
//true ---里面的子动画共用集合的插入器
//false - -子动画使用自己的插入器
//声明一个动画集合
AnimationSet set = new AnimationSet(false);
//往集合里面添加动画
set.addAnimation(anim);
set.addAnimation(sanim);
//让控件播放集合中的动画
iv.startAnimation(set);
}
2.XML中写补间
在实际开发中,用XML比较多,XML比较简洁。这种方法,分面两部分,这里对translate举例,其它(包括set)都一样。
- XML中(放在res/anim文件夹中)
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="50%p"
android:fromYDelta="0"
android:toYDelta="50%p"
android:duration="2000"
android:interpolator="@android:anim/bounce_interpolator"
android:repeatCount="infinite"
android:repeatMode="reverse">
<!-- 如果里面给的是数字,代表的是绝对值的长度 , Animation.ABSOLUTE
如果是百分比,代表的是自己的倍数, Animation.RELATIVE_TO_SELF
如果百分比后面加个p 代表的是父元素的倍数 Animation.RELATIVE_TO_PARENT
-->
</translate>
- JAVA代码
public void translate(View v){
Animation anim = AnimationUtils.loadAnimation(this, R.anim.translate_demo);
iv.startAnimation(anim);
}
四、属性动画
属性动画在3.0版本后出现,相比于补间动画,属性动画最重要的一点是,过程中位置真的在移动。属性动画建议用java代码直接控制,不要用XML。(因为代码可以更方便控制运动参数)
1.单个的效果(平移举例)
// 构建一个对象属性动画
// 参数一: 谁将播放这个动画,在这里是 iv
// 参数二: 到底当前这个动画是什么动画呢? 这里指定的是x方向的平移动画
// 参数三 : 可变参数 ,可以看做是一个数组 , 里面给定的将是平移的数值。
ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "translationX",
new float[] { 0, 10, 100 });
// 定义时间长度
animator.setDuration(2000);
// 设置动画无限循环播放
animator.setRepeatCount(Animation.INFINITE);
// 设置动画循环的模式 , reverse ,播放完毕之后,倒着播放
animator.setRepeatMode(Animation.REVERSE);
// 让这个动画播放起来。
animator.start();
2.集合
public void set(View v) {
ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "translationX",
new float[] { 0, 100 });
// 定义时间长度
animator.setDuration(2000);
/*// 设置动画无限循环播放
animator.setRepeatCount(Animation.INFINITE);
// 设置动画循环的模式 , reverse ,播放完毕之后,倒着播放
animator.setRepeatMode(Animation.REVERSE);*/
ObjectAnimator animatory = ObjectAnimator.ofFloat(iv, "translationY",
new float[] { 0, 100 });
// 定义时间长度
animatory.setDuration(2000);
/* // 设置动画无限循环播放
animatory.setRepeatCount(Animation.INFINITE);
// 设置动画循环的模式 , reverse ,播放完毕之后,倒着播放
animatory.setRepeatMode(Animation.REVERSE);*/
AnimatorSet set = new AnimatorSet();
// set.playTogether(animator , animatory); 两个动画一起播放
//按顺序播放 ,先播放前面的动画,后播放后面添加进来的动画 动画只能播放一次。
set.playSequentially(animatory , animator);
set.start();
}
3.xml形式的动画
- xml(res/animator中)
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="translationX"
android:valueFrom="0"
android:valueTo="200"
android:duration="2000"
android:repeatCount="infinite"
android:repeatMode="reverse">
</objectAnimator>
- java代码
public void translate(View v) {
//1,构建动画, 导入动画
Animator anim = AnimatorInflater.loadAnimator(this, R.animator.translatex);
//2.设置谁播放这个动画
anim.setTarget(iv);
//3.播放这个动画
anim.start();
}