您的位置:首页 > 移动开发 > Android开发

Android动画之补间动画(Tween Animation)

2016-08-26 14:06 615 查看
动画的介绍,网上一搜索一大把,为什么我还是会写呢?我写这个第一个是为了复习一下这个知识点,加深一点印象,第二是为了以后用的时候如果忘记了的话,查看自己写的很容易就懂了,记起来了,毕竟自己写的自己容易看懂一点;

android动画分类

Android动画分为3种:帧动画(Frame Animation),补间动画(Tween Animation),属性动画(Property Animation);

今天我们来说说补间动画,欢迎指正,虚心接受

补间动画的分类

补间动画又可以分为4种:

透明补间动画(AlphaAnimation),移动补间动画(TranslateAnimation),缩放补间动画(ScaleAnimation),旋转补间动(RotateAnimation)。

而这4种动画可以在XML文件中静态设置,也可以在代码中动态设置,下面我们先来说说静态设置动画的属性;

首先在工程目录下的res文件夹下面新建一个名字为anim的文件夹,在anim下建一个XML文件,下面介绍里面具体的代码

透明补间动画(AlphaAnimation)

XML静态设置

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="false">
<alpha
android:duration="1000"
android:fromAlpha="1"
android:repeatCount="2"
android:repeatMode="reverse"
android:toAlpha="0" />
</set>


android:fillAfter = "false"这个属性的意思是动画结束后是否保持结束的状态,false为不保持,恢复动画之前的状态,true则是保持动画之后的状态,注意一定要写在set标签里面,否则无效

android:duration = "1000"             动画持续的时间,单位是毫秒

android:fromAlpha = "1"                动画起始的透明度,1是完全不透明,0是完全透明

android:toAlpha = "0"                    动画结束时的透明度

android:repeatCount = "2"             动画的重复次数,传infinite为无限次

android:repeatMode = "reverse"   动画重复的模式;reverse:动画结束后再从结束状态颠倒回去,restart:动画结束后重新开始动画

然后就可以在类中调用了,具体用法:

//要使用动画的控件
ImageView imageView = (ImageView) findViewById(R.id.main_imageview);
//得到动画对象,传两个参数,参数1:context上下文对象;参数2:animation
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
//开始动画
imageView.startAnimation(animation);


代码动态设置

//动画的控件
ImageView imageView = (ImageView) findViewById(R.id.main_imageview);
//拿到动画实例对象,传入fromAlpha和toAlpha
AlphaAnimation animation = new AlphaAnimation(1, 0);
// 设置执行的时间
animation.setDuration(1000);
// 设置动画重复的次数
animation.setRepeatCount(2);
// 设置动画重复的模式
animation.setRepeatMode(Animation.REVERSE);
// 设置动画结束以后得状态
animation.setFillAfter(false);
//开始动画
imageView.startAnimation(animation);


以上两种代码实现的效果是一样的,效果为:



移动补间动画(TranslateAnimation)

XML静态设置

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="2"
android:repeatMode="reverse"
android:toXDelta="400"
android:toYDelta="500" />
</set>


android:duration="1000" 动画持续时间
android:fromXDelta="0"   起始位置是相对于控件偏移量为X坐标的位置,0代表控件当前的位置,传负数为当前控件位置的左边为动画起始位置,反之则反
android:fromYDelta="0"  起始位置是相对于控件偏移量为Y坐标的位置,0代表控件当前的位置,传负数为控件向上的位置为起始位置,反之则反
android:toXDelta="400"  动画结束时相对于控件X轴的偏移量,400就是控件项右边偏移了400
android:toYDelta="500"  同上,向下偏移了500
android:repeatCount = "2"   动画的重复次数
android:repeatMode = "reverse"   动画重复的模式;reverse:动画结束后再从结束状态颠倒回去,restart:动画结束后重新开始动画

代码动态设置

//动画的控件
ImageView imageView = (ImageView) findViewById(R.id.main_imageview);
//参数分别是:fromXDelta,toXDelta,fromYDelta,toYDelta
TranslateAnimation animation = new TranslateAnimation(0, 400, 0, 500);
// 设置执行的时间
animation.setDuration(1000);
// 设置动画重复的次数
animation.setRepeatCount(2);
// 设置动画重复的模式
animation.setRepeatMode(Animation.REVERSE);
//开始动画
imageView.startAnimation(animation);


以上两种代码实现的效果是一样的,效果为:



缩放补间动画(ScaleAnimation)

XML静态设置

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


android:fromXScale="0"   动画起始时控件X轴的大小,0代表起始动画时控件X轴的大小为控件的0倍,传入几就是几倍
android:fromYScale="0"   动画起始时控件X轴的大小,0代表起始动画时控件Y轴的大小为控件的0倍,传入几就是几倍
android:toXScale="2"       动画结束时控件X轴的大小,2代表结束时,控件X轴的大小是控件原来大小的2倍大
android:toYScale="2"动画结束时控件Y轴的大小,2代表结束时,控件Y轴的大小是控件原来大小的2倍大

androi:pivotX="50%" 动画相对于控件的X坐标的开始位置,控制偏移量的     50%就是控件的X轴方向的中点位置
androi:pivotY="50%" 动画相对于控件的Y坐标的开始位置,控制偏移量的 50%就是控件的Y轴方向的中点位置 

代码动态设置

//动画的控件
ImageView imageView = (ImageView) findViewById(R.id.main_imageview);
//参数分别是:fromXScale,toXScale,fromYScale,toYScale,
// 相对于谁Animation.RELATIVE_TO_SELF相对于自己,
// 0.5f就是动画相对于控件的X坐标的开始位置,控制偏移量的
ScaleAnimation animation = new ScaleAnimation(0, 2, 0, 2, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// 设置执行的时间
animation.setDuration(1000);
// 设置动画重复的次数
animation.setRepeatCount(2);
// 设置动画重复的模式
animation.setRepeatMode(Animation.REVERSE);
//开始动画
imageView.startAnimation(animation);

以上两种代码实现的效果是一样的,效果为:



旋转补间动(RotateAnimation)

XML静态设置

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


android:fromDegrees="0"  动画开始时的角度
android:toDegrees="0"  动画要旋转多少度

androi:pivotX="50%" 动画相对于控件的X坐标的开始位置,控制偏移量的     50%就是控件的X轴方向的中点位置
androi:pivotY="50%" 动画相对于控件的Y坐标的开始位置,控制偏移量的 50%就是控件的Y轴方向的中点位置 

代码中调用:略

代码动态设置

ImageView imageView = (ImageView) findViewById(R.id.main_imageview);
RotateAnimation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
// 设置执行的时间
animation.setDuration(1000);
//                // 设置动画重复的次数
animation.setRepeatCount(2);
//                // 设置动画重复的模式
animation.setRepeatMode(Animation.REVERSE);
//                //开始动画
imageView.startAnimation(animation);

效果如下



如果将属性修改为:

android:pivotY="150%"


旋转的中心点沿Y轴偏移了150%,则效果为:



混合使用

可以2个或者多个一起混合使用
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<rotate
android:duration="1000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:toDegrees="360" />

<scale
android:duration="1000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:toXScale="1"
android:toYScale="1" />

<translate
android:duration="1000"
android:fromXDelta="-300"
android:repeatMode="reverse"
android:toXDelta="0" />

</set>

效果为:



简单的使用

在Activity跳转的简单使用,我们可以设置进入的Activity和出去的Activity的动画,先看一下效果:



首先创建两个动画的XM文件,里面的代码分别为:
进去的动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<rotate
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:toDegrees="720" />

<scale
android:duration="2000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:toXScale="1"
android:toYScale="1" />

<translate
android:duration="2000"
android:fromXDelta="-300"
android:repeatMode="reverse"
android:toXDelta="0" />

</set>

出去的动画为:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toYDelta="900"
android:toXDelta="900" />

</set>


然后在跳转的时候使用,具体代码为:
Intent intent = new Intent(MainActivity.this, AnimationActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in,R.anim.out);


有更多的知识点以后再来补充,欢迎指正
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动画 android