android动画总结

一、帧动画(FrameAnimation)

通过播放一帧一帧的图片组成动画。可以在xml添加每一帧的图片,也可能通过代码来添加每一帧的图片。帧动画只针对ImageView对象

  • 使用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/compositedst1" android:duration="500"></item>
<item android:drawable="@drawable/compositedst2" android:duration="500"></item>
<item android:drawable="@drawable/compositedst3" android:duration="500"></item>
<item android:drawable="@drawable/compositedst4" android:duration="500"></item>
</animation-list>
  • 使用代码添加帧图片
AnimationDrawable animationDrawable = new AnimationDrawable();
animationDrawable.setOneShot(false);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.compositedst1),1000);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.compositedst2),1000);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.compositedst3),1000);
animationDrawable.addFrame(getResources().getDrawable(R.drawable.compositedst4),1000);

运行帧动画

imageView = findViewById(R.id.imageview);
//使用xml方式添加帧图片
imageView.setImageResource(R.drawable.animationlist);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
//使用代码方式添加帧图片
AnimationDrawable animationDrawable = new AnimationDrawable();
animationDrawable.addFrame(xxx);
animationDrawable.addFrame(xxx);
//启动动画
animationDrawable.start();

二、补间动画(TweenAnimation)

设定动画的几个关键点,其他点使用系统计算来完成的动画就补间动画。补间动画只能变化View的展示状态,不能改变View的位置

  • 平移动画(TranslateAnimation)
  • 旋转动画(RotationAnimation)
  • 缩放动画(ScaleAnimation)

使用

  1. 在xml里定义动画
    也可以使用java代码生成动画对象,但一般建议直接使用xml,更方便、代码也会更简洁。
  2. 在使用时加载xml动画,生成动画对象
Animation animation = AnimationUtils.loadAnimation(getContext(),R.anim.flower);
animation.setDuration(1000);
  1. 展示动画
mPieChart.startAnimation(animation);

差值器

控制动画开始值过渡到结束值变化规律的接口。如加速变化、减速变化等。
使用差值器,可在动画的xml里配置android:interpolator,或在代码里使用setInterpolator()方法进行配置。

创建差值器需要使用代码实现,创建一个插件器类实现Interpolator接口

/**
 * An interpolator defines the rate of change of an animation. This allows
 * the basic animation effects (alpha, scale, translate, rotate) to be 
 * accelerated, decelerated, repeated, etc.
 */
public interface Interpolator extends TimeInterpolator {
    // A new interface, TimeInterpolator, was introduced for the new android.animation
    // package. This older Interpolator interface extends TimeInterpolator so that users of
    // the new Animator-based animations can use either the old Interpolator implementations or
    // new classes that implement TimeInterpolator directly.
}

/**
 * A time interpolator defines the rate of change of an animation. This allows animations
 * to have non-linear motion, such as acceleration and deceleration.
 */
public interface TimeInterpolator {

    /**
     * Maps a value representing the elapsed fraction of an animation to a value that represents
     * the interpolated fraction. This interpolated value is then multiplied by the change in
     * value of an animation to derive the animated value at the current elapsed animation time.
     *
     * @param input A value between 0 and 1.0 indicating our current point
     *        in the animation where 0 represents the start and 1.0 represents
     *        the end
     * @return The interpolation value. This value can be more than 1.0 for
     *         interpolators which overshoot their targets, or less than 0 for
     *         interpolators that undershoot their targets.
     */
    float getInterpolation(float input);
}

系统插值器有以下几种

类名对应资源作用
LinearInterpolator@android:anim/linear_interpolator匀速运行
AccelerateInterpolator@android:anim/accelerate_interpolator动画加速运行
DecelerateInterpolator@android:anim/decelerate_interpolator减速运行
AccelerateDecelerateInterpolator@android:anim/accelerate_decelerate_interpolator先加速再减速运行
OvershootInterpolator
AnticipateInterpolator@android:
AnticipateOvershootInterpolator
BounceInerpolator
CycleInterpolator

估值器

控制动画从开始值过渡到结束值时具体的数据值接口。需要注意的是估值器只针对属性动画。使用估值器时,需要需要调用setEvaluator将估值器设置进去即可;实现估值器,也需要使用代码来实现TypeEvaluator接口

package android.animation;

/**
 * Interface for use with the {@link ValueAnimator#setEvaluator(TypeEvaluator)} function. Evaluators
 * allow developers to create animations on arbitrary property types, by allowing them to supply
 * custom evaluators for types that are not automatically understood and used by the animation
 * system.
 *
 * @see ValueAnimator#setEvaluator(TypeEvaluator)
 */
public interface TypeEvaluator<T> {

    /**
     * This function returns the result of linearly interpolating the start and end values, with
     * <code>fraction</code> representing the proportion between the start and end values. The
     * calculation is a simple parametric calculation: <code>result = x0 + t * (x1 - x0)</code>,
     * where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>,
     * and <code>t</code> is <code>fraction</code>.
     *
     * @param fraction   The fraction from the starting to the ending values
     * @param startValue The start value.
     * @param endValue   The end value.
     * @return A linear interpolation between the start and end values, given the
     *         <code>fraction</code> parameter.
     */
    public T evaluate(float fraction, T startValue, T endValue);

}

差值器与估值器关系

差值器与估值器合作关系。差值器决定了变化规则,即如何变化;估值器决定了变化的具体值。

三、属性动画(PropertyAnimation)

通过修改对象的属性来产生的动画,叫属性动画。内部通过插值器来计算运动规律,再结合估值器给出一个确定的值,再通过对象的set方法来修改其属性。
从本质上来讲,属性动画与补间动画没有什么区别,不同点在于属性动画真实地修改了对象的属性,而补间动画只修改了对象的展示效率,并未修改其属性

如何使用

ObjectAnimator.ofInt(tv1, "translationX", 0, 500)
              .setDuration(1000)
              .start();

四、转场动画

在Activity、Fragment页面切换时添加的动画,叫转场动画。

  • api21以下
    api21以下在执行页面切换(startActivity或finish)后,需要手动调用overridePendingTransition进行转场动画,也可以设置Activity主题。
    手动调用方式
//进入新页面
startActivty(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

//返回原页面
finish();
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);

使用style样式

    <style name="AppThisTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <item name="android:windowAnimationStyle">@style/activityAnimation</item>
    </style>

    <style name="activityAnimation" parent="@android:style/Animation.Activity">
        <item name="android:activityOpenEnterAnimation">@anim/slide_right_in</item>
        <item name="android:activityOpenExitAnimation">@anim/slide_left_out</item>
        <item name="android:activityCloseEnterAnimation">@anim/slide_left_in</item>
        <item name="android:activityCloseExitAnimation">@anim/slide_right_out</item>
    </style>
  • api21及以上
    • slide 移动效果
    • fade 渐入渐出
    • explode 爆炸效率
      api21及以上与api21以下一样,也可以通过activity主题或代码形式设置。但建议使用主题方式,这样代码量更少,更简洁。

style方式相比于以前的设置方式,把动画属性都放在了AppTheme里了。

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <!--必须制定该属性不然动画不起作用-->
        <item name="android:windowActivityTransitions">true</item>
        <!--activity进入动画-->
         <item name="android:windowEnterTransition">@transition/slide_right</item>
        <!--activity退出动画-->
         <item name="android:windowExitTransition">@transition/slide_left</item>
         <!--是否同时执行,如果同时执行A页面动画还没退出,B页面已经开始动画,感觉不是很和谐-->
         <item name="android:windowAllowReturnTransitionOverlap">false</item>
        <item name="android:windowAllowEnterTransitionOverlap">false</item>
  </style>
 

手动调用方式

TransitionSet transitionSet = new TransitionSet();
transitionSet.setDuration(300);
       
transitionSet.setOrdering(TransitionSet.ORDERING_TOGETHER);
Slide slideTransition = new Slide();
slideTransition.setSlideEdge(Gravity.LEFT);
transitionSet.addTransition(slideTransition);

Fade fadeTransition = new Fade();
transitionSet.addTransition(fadeTransition);
//排除状态栏
transitionSet.excludeTarget(android.R.id.statusBarBackground, true);
//是否同时执行
getWindow().setAllowEnterTransitionOverlap(false);
getWindow().setAllowReturnTransitionOverlap(false);
//退出这个界面
getWindow().setExitTransition(slideTransition);

//使用Compat相关api进行调用
ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this);
ActivityCompat.startActivity(MainActivity.this,
                        new Intent(MainActivity.this,TransitionSlideActivity.class), 
                        optionsCompat.toBundle());

参考

  1. https://www.jianshu.com/p/2f19fe1e3ca1
  2. https://www.jianshu.com/p/ea53d2f03cfd
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值