一、Tween动画
1、特点:
1.只是实现了简单的渐变,平移,拉伸,缩放;
2.Tween动画实现完成以后,该动画会恢复原状,自身属性并没有发生任何改变,动画的形成是通过父布局改变了其位置或渐变度,从而来改变其本身在短时间内的属性
3.此动画的形成是依赖于其父布局的,其属性没有发生任何改变
2、在Tween动画中所有动画的父类是Animation。
3、动画的渐变
/**
* 渐变
*/
private void alpin(){
/**
* 第一个参数:表示开始的透明度
* 第二个参数:表示结束的透明度
* 透明度的范围是0-1;1表示完全不透明;0表示完全透明
*/
Animation animation=new AlphaAnimation(0,1);
animation.setDuration(3000);
//设置动画重复的次数
animation.setRepeatCount(5);
/**
* Animation.REVERSE:重复的时候进行反转
* Animation.RESTART:每次执行都重新开始
*/
animation.setRepeatMode(Animation.RESTART);
animation.setAnimationListener(new Animation.AnimationListener() {
/**
* 动画开始执行的时候的一个回调
* @param animation
*/
@Override
public void onAnimationStart(Animation animation) {
}
/**
* 动画结束的时候的一个回调
* @param animation
*/
@Override
public void onAnimationEnd(Animation animation) {
}
/**
* 动画重复执行的时候的一个回调
* @param animation
*/
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mImageView.startAnimation(animation);
}
4、动画的缩放
/**
* 缩放
*/
private void scale(){
//按照宽和高的倍数来进行缩放
//默认的缩放点是左上角
// Animation animation=new ScaleAnimation(0.5f,1,0.5f,1);
//后面的两个参数是像素,确定缩放点的坐标
// Animation animation=new ScaleAnimation(0,1,0,1,mImageView.getWidth()/2,mImageView.getHeight()/2);
//后面的四个参数是用来确定缩放点的坐标
Animation animation=new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(3000);
animation.setRepeatCount(5);
animation.setRepeatMode(Animation.REVERSE);
mImageView.startAnimation(animation);
}
5、动画的平移
/**
*平移
*/
private void traslate(){
/**
* 坐标0,0是以自身左上角定点的坐标为坐标点;移动的单位是像素。
*/
// Animation animation=new TranslateAnimation(0,100,0,100);
/**
* 相对点的确定:
* Animation.RELATIVE_TO_SELF:相对于自己
* Animation.ABSOLUTE:相对于绝对点(很少用)
* Animation.RELATIVE_TO_PARENT:相对于父容器
*
* 值的确定:
* 是以当前宽和该乘以倍数后所确定的坐标
*/
Animation animation=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1f);
animation.setDuration(2000);
animation.setRepeatMode(Animation.REVERSE);
animation.setRepeatCount(5);
mImageView.startAnimation(animation);
}
6、动画的旋转
/**
* 旋转
*/
private void rotation(){
//默认的旋转点就是以左上角为旋转点
// Animation animation=new RotateAnimation(0,180);
//后面的两个参数表示的像素,确定的是旋转点的位置
// Animation animation=new RotateAnimation(0,180,mImageView.getWidth()/2,mImageView.getWidth()/2);
//后面的四个参数,都是为了确定的旋转点的位置,是按照宽和高的倍数来确定位置
Animation animation=new RotateAnimation(0,180,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(3000);
animation.setRepeatCount(5);
animation.setRepeatMode(Animation.REVERSE);
mImageView.startAnimation(animation);
}
7、多动画同时执行
步骤:
1.获取一个AnimationSet对象;
2.定义一系列动画(一个或多个);
3.将定义的动画添加到动画集中;
4.设置动画的参数(时间,循环次数,循环方式);
5.使用View对象的startAnimation方法启动动画集;
/**
* 多动画同时执行
*/
private void manyAnima(){
Animation animation=new AlphaAnimation(0,1);
Animation animation1=new ScaleAnimation(0,1,0,1,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
Animation animation2=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
AnimationSet animationSet=new AnimationSet(true);
animationSet.addAnimation(animation);
animationSet.addAnimation(animation1);
animationSet.addAnimation(animation2);
animationSet.setDuration(5000);
mImageView.startAnimation(animationSet);
}
8、通过配置文件实现动画的步骤:
1.在res的目录下建立一个anim的文件夹;
2.在xml文件中配置相应的动画属性;
3.通过AnimationUtils的LoadAnimation()方法加载动画;
4.设置动画的相应属性;
5.运行动画。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="5000"
android:fromAlpha="0"
android:toAlpha="1" />
</set>
private void alpin1(){
Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.animo_01);
mImageView.startAnimation(animation);
}
二、Frame动画
使用的步骤:
1.在res目录下右键-->new --> Android resource file -->填写名字,选择Drawable,将根节点给成animation-list -->OK;
2.在创建的xml文件中配置一系列item;
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/girl_1" android:duration="150"/>
<item android:drawable="@drawable/girl_2" android:duration="150"/>
<item android:drawable="@drawable/girl_3" android:duration="150"/>
<item android:drawable="@drawable/girl_4" android:duration="150"/>
<item android:drawable="@drawable/girl_5" android:duration="150"/>
<item android:drawable="@drawable/girl_6" android:duration="150"/>
<item android:drawable="@drawable/girl_7" android:duration="150"/>
<item android:drawable="@drawable/girl_8" android:duration="150"/>
<item android:drawable="@drawable/girl_9" android:duration="150"/>
<item android:drawable="@drawable/girl_10" android:duration="150"/>
<item android:drawable="@drawable/girl_11" android:duration="150"/>
</animation-list>
3.给需要帧动画的控件设置setBackgroundResource(R.drawable.frame_01);
4.通过当前对象.getBackground()获取其背景,强制类型转换成AnimationDrawable对象;
5.通过animationDrawable.start()直接运行帧动画;
private void frameAnima(){
//设置内容,内容是帧动画的布局文件
mImageView1.setBackgroundResource(R.drawable.frame_01);
//获取当前控件的背景
AnimationDrawable animationDrawable = (AnimationDrawable) mImageView1.getBackground();
//打开帧动画
animationDrawable.start();
}
三、属性动画
1、通过给定的时间间隔来完成属性的动态改变的动画;
2、属性动画改变的是属性,只不过默认的情况下是匀速的改变当前对象的属性;
3、属性动画的原理:
每次开始执行的时候都会首先通过调用当前对象的get属性名的方法获取当前的值,然后调用set方法来不断的进行设置对象的值,给定一个时间通过计算出每个单位时间所移动的距离,然后开始移动。
4、属性动画的应用:
1.缩放
/**
* 属性动画的缩放
*/
private void scale(){
ObjectAnimator scaleX = ObjectAnimator.ofFloat(mImageView, "scaleX", 1, 0.5f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(mImageView, "scaleY", 1, 0.5f);
scaleX.setDuration(3000);
scaleY.setDuration(3000);
scaleX.start();
scaleY.start();
}
2.渐变
/**
* 属性动画的渐变
*/
private void alphi(){
ObjectAnimator alpha = ObjectAnimator.ofFloat(mImageView, "alpha", 1, 0.8f, 0.2f);
alpha.setDuration(3000);
alpha.setRepeatCount(5);
alpha.setRepeatMode(Animation.REVERSE);
alpha.start();
}
3.旋转
/**
* 属性动画的旋转
*/
private void rotation(){
ObjectAnimator rotation = ObjectAnimator.ofFloat(mImageView, "rotation", 0, 360);
rotation.setDuration(3000);
rotation.setRepeatCount(3);
rotation.setRepeatMode(Animation.REVERSE);
rotation.start();
}
4.平移
/**
* 属性动画的平移
*/
private void translate(){
ObjectAnimator translationX = ObjectAnimator.ofFloat(mImageView, "translationX", 0, 500);
ObjectAnimator translationY = ObjectAnimator.ofFloat(mImageView, "translationY", 0, 500);
translationX.setDuration(3000);
translationY.setDuration(3000);
translationX.setRepeatCount(5);
translationY.setRepeatCount(5);
translationX.setRepeatMode(Animation.REVERSE);
translationY.setRepeatMode(Animation.REVERSE);
translationX.start();
translationY.start();
}
5.多个动画同时执行
/**
* 多动画同时执行
*/
private void many(){
AnimatorSet animatorSet=new AnimatorSet();
ObjectAnimator translationX = ObjectAnimator.ofFloat(mImageView, "translationX", 0, 100);
translationX.setDuration(3000);
ObjectAnimator rotation = ObjectAnimator.ofFloat(mImageView, "rotation", 0, 360);
rotation.setDuration(3000);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(mImageView, "scaleX", 1, 0.5f);
scaleX.setDuration(3000);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(mImageView, "scaleY", 1, 0.5f);
scaleY.setDuration(3000);
//按照顺序执行
/* List<Animator> list=new ArrayList<>();
list.add(translationX);
list.add(rotation);
list.add(scaleX);
list.add(scaleY);
animatorSet.playSequentially(list);*/
animatorSet.playSequentially(translationX);
animatorSet.playSequentially(rotation);
animatorSet.playSequentially(scaleX);
animatorSet.playSequentially(scaleY);
animatorSet.start();
}
6.设置动画对象的宽度
/**
* 因为这个属性是没有Set和Get方法的,所以我们需要通过封装一个类来提供Set和Get方法
*/
private void setWidth(){
Wrapper wrapper=new Wrapper(mImageView);
ObjectAnimator.ofInt(wrapper,"width",0,500).setDuration(3000).start();
}
class Wrapper{
View target=null;
public Wrapper(View target){
this.target=target;
}
public int getWidth(){
return this.target.getLayoutParams().width;
}
public void setWidth(int width){
this.target.setLayoutParams(new LinearLayout.LayoutParams(width,target.getLayoutParams().height));
}
}