版权声明:本文为博主原创文章,转载请注明出处!谢谢! https://blog.csdn.net/aaa1050070637/article/details/88714264
先贴工具类代码,然后再讲解下插值器
/**
* 补间动画
* 平移translation、缩放scale、旋转rotate、透明度translucent
* 1.XML形式
* 2.代码形式
* Created by 大漠dreamer on 2019/3/21.
*/
public class PatchAnimationUtils {
/**
* 平移动画
*
* @param view 需要控制的view
* @param turnStatus true代表平移进入视图,false代表平移离开视图
* @param isXml true代表使用xml形式,false代表使用代码形式
* @param context 上下文
*/
public static void translateAnimation(View view, boolean turnStatus, boolean isXml, Context context) {
int x = view.getWidth();
int y = view.getHeight();
//通过代码形式来实现
if (!isXml) {
if (turnStatus) {//当视图不可见,平移进入视图
if (View.INVISIBLE == view.getVisibility() && View.GONE == view.getVisibility()) {
TranslateAnimation tsAni = new TranslateAnimation(x, 0, y, 0);
tsAni.setInterpolator(new AccelerateInterpolator());//插值器--从慢到快
tsAni.setDuration(1000 * 3);
view.setAlpha(1f);
tsAni.setFillAfter(true);
view.setVisibility(View.VISIBLE);
view.startAnimation(tsAni);
}
} else {
if (View.VISIBLE == view.getVisibility()) {//当视图可见,平移移出视图
TranslateAnimation tsAni = new TranslateAnimation(-x, -x * 2, -y, -y * 2);
tsAni.setInterpolator(new LinearInterpolator());//线性加速
tsAni.setDuration(1000 * 2);
view.setAlpha(0.5f);
tsAni.setFillAfter(true);
view.setVisibility(View.GONE);
view.startAnimation(tsAni);
}
}
} else {
if (turnStatus) {//移入
Animation animation = AnimationUtils.loadAnimation(context,
R.anim.translation_in);
view.startAnimation(animation);
} else {//移出
Animation animation = AnimationUtils.loadAnimation(context, R.anim.translation_out);
view.startAnimation(animation);
}
}
}
/**
* 缩放动画
*
* @param context 上下文
* @param view 需要缩放的view
* @param isXml true代表Xml方式实现,false代表代码形式实现
*/
public static void scaleAnimation(Context context, View view, boolean isXml) {
if (!isXml) {
ScaleAnimation animation = new ScaleAnimation(0.2f, 1.5f, 0.2f, 1.5f);
animation.setInterpolator(new AnticipateInterpolator());//反向,先向相反方向改变一段再加速播放。
animation.setDuration(1000 * 3);
view.startAnimation(animation);
} else {
Animation animation = AnimationUtils.loadAnimation(context, R.anim.scale);
view.startAnimation(animation);
}
}
/**
* 旋转动画
*
* @param context 上下文
* @param view 需要缩放的view
* @param isXml true代表Xml方式实现,false代表代码形式实现
*/
public static void rotateAnimation(Context context, View view, boolean isXml) {
if (!isXml) {
//从0-360°,X方向从左开始,Y方向从上边开始
RotateAnimation animation = new RotateAnimation(0, 360, 0f, 0f);
animation.setInterpolator(new CycleInterpolator(3));//转3圈
animation.setDuration(1000 * 3);
view.startAnimation(animation);
} else {
Animation animation = AnimationUtils.loadAnimation(context, R.anim.rotate);
view.startAnimation(animation);
}
}
/**
* 透明度动画
*
* @param context 上下文
* @param view 需要缩放的view
* @param isXml true代表Xml方式实现,false代表代码形式实现
*/
public static void alphaAnimation(Context context, View view, boolean isXml) {
if (!isXml) {
//从0-360°,X方向从左开始,Y方向从上边开始
AlphaAnimation animation = new AlphaAnimation(0f, 1f);
animation.setInterpolator(new AccelerateDecelerateInterpolator());//前后慢,中间快
animation.setDuration(1000 * 3);
view.startAnimation(animation);
} else {
Animation animation = AnimationUtils.loadAnimation(context, R.anim.translucent);
view.startAnimation(animation);
}
}
}
然后贴下xml中补间动画的定义
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="3000"
android:fromDegrees="0"
android:repeatCount="3"
android:repeatMode="reverse"
android:toDegrees="360"
>
</rotate>
<scale
android:duration="2000"
android:fromXScale="0.5"
android:fromYScale="0.5"
android:pivotX="20%"
android:pivotY="20%"
android:toXScale="2"
android:toYScale="2">
</scale>
<translate
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="200"
android:toYDelta="300">
</translate>
<alpha
android:duration="3000"
android:fromAlpha="0"
android:toAlpha="1">
</alpha>
</set>
插值器---Interpolator讲解
用来控制动画的变化速度,可以理解成动画渲染器,
当然我们也可以自己实现Interpolator 接口,自行来控制动画的变化速度,而Android中已经为我们提供了可供选择的实现类。
1.LinearInterpolator:动画以均匀的速度改变。
2.AccelerateInterpolator:在动画开始的地方改变速度较慢,然后开始加速。
3.AccelerateDecelerateInterpolator:在动画开始、结束的地方改变速度较慢,中间时加速。
4.CycleInterpolator:动画循环播放特定次数,变化速度按正弦曲线改变
Math.sin(2 * mCycles * Math.PI * input)。
5.DecelerateInterpolator:在动画开始的地方改变速度较快,然后开始减速。
6.AnticipateInterpolator:反向,先向相反方向改变一段再加速播放。
7.AnticipateOvershootInterpolator:开始的时候向后然后向前甩一定值后返回最后的值。
8.BounceInterpolator: 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100。