Android应用开发之Android开发中属性动画(ObjectAnimator)中 插值器(Time Interpolator )详解
凌雪 2018-11-29 来源 :网络 阅读 709 评论 0

摘要:本文将带你了解Android应用开发之Android开发中属性动画(ObjectAnimator)中 插值器(Time Interpolator )详解,希望本文对大家学Android有所帮助。

本文将带你了解Android应用开发之Android开发中属性动画(ObjectAnimator)中 插值器(Time Interpolator )详解,希望本文对大家学Android有所帮助。


在 Android开发中,为了保持良好的交互体验,我们经常会用到动画来提升用户体验,而动画中属性动画用的最多。
虽然动画可以让界面看起来不是那么粗糙,但是很多时候仅仅只是匀速运动的动画可能不足以描述一些我们想呈现给用户的界面,比如模拟一个下落的小球,我们知道下落的小球并不是以匀速运动的,而是加速下落的,如果要实现这样一个效果,我们可能就需要用到 Time Interpolator 。
其实 Android 已经给我们提供了若干类型的 interpolator 供我们使用,一般情况下这些已经足够我们使用了,所以也就不需要我们自己创造了,下面我们就具体了解一下各个类型的 interpolator 的使用方法和适用场景。
    AccelerateDecelerateInterpolator 缓慢启动,中间过程移动非常快,缓慢结束,适用于在屏幕上启动和结束的动画。 AccelerateInterpolator 缓慢启动,在移动过程中慢慢加快,适用于在屏幕上启动,屏幕外结束的动画。 AnticipateInterpolator 类似于 AccelerateInterpolator ,但是有一个导致它以负值启动的弹力,一次一个物体向下移动的动画会先向上移动,然后再向下移动,可以理解为把一个物体放在一个弹弓上向后拉然后弹射出去。 AnticipateOvershootInterpolator 前半部分与AnticipateInterpolator 相同,到达终点后会继续以这个速度继续向原来的方向运动一段距离之后再回到终点。 BounceInterpolator 类似于一个有弹性的小球掉到地上,然后又弹回来,再落下去,但是每次弹起来的高度会越来越低,直到最终静止在地面上。 CycleInterpolator 运动曲线类似于 sin 函数,起始位置相当于 sin 零点,终点相当于 sin 的顶点。比如有一个处于中心的一个小球,你的目标是把它移到下方的一个位置,这个位置暂称之为终点,那这个小球的运动轨迹会是 : 先向下移动到终点,然后再向上移动到初始位置,然后在向上移动 初始位置到终点 同样的距离,然后再返回到初始位置。这是一个周期,至于动画会执行几个周期,这取决于你在构造函数中传入的浮点参数,这个参数就是周期。 DecelerateInterpolator 以最大速度启动,结束时放慢速度,适用于在屏幕之外启动,在屏幕内结束的动画。 FastOutLinearInInterpolator 在 support 库中,这个插值器使用一个查找表来描述位移。简单的说,它的启动过程像 AccelerateInterpolator ,结束过程像 LinearInterpolator 。 FastOutSlowInInterpolator 在 support 库中,这个插值器也是使用一个查找表来描述位移。它的启动过程像 AccelerateInterpolator ,结束过程像 DecelerateInterpolator 。 LinearInterpolator 以一个恒定的速度变化的动画。 LinearOutSlowInInterpolator 在 support 库中,这个插值器也是使用一个查找表来描述位移。它的启动过程像 LinearInterpolator ,结束过程像 DecelerateInterpolator 。 OvershootInterpolator 类似于 AccelerateInterpolator,但是有一个是它不能立即停到终点位置的力,使它超过终点位置,然后在弹回终点,就想撞到了弹弓上。 PathInterpolator 这个是在 Android 5.0(API level 21) 中加入的,这个插值动画基于你穿给它的一个路径。X 坐标表示时间,Y   坐标代表返回的浮点值。如果你任意给定的 X 值只对应一个 Y 值,并且没有间断(即一段给定的 0~1 之间的   X 值对应一段连续的 Y 值),那么所有种类的路径都可以被支持。
我们来看一下使用各个   interpolator 的运行效果。
   
    下面附上源码:
    testActivity.class
      <code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code>import   android.animation.ObjectAnimator;import   android.animation.TimeInterpolator;import   android.support.v4.view.animation.FastOutLinearInInterpolator;import   android.support.v4.view.animation.FastOutSlowInInterpolator;import   android.support.v4.view.animation.LinearOutSlowInInterpolator;import   android.support.v7.app.AppCompatActivity;import android.os.Bundle;import   android.view.View;import android.view.Window;import   android.view.WindowManager;import android.view.animation.AccelerateDecelerateInterpolator;import   android.view.animation.AccelerateInterpolator;import   android.view.animation.AnticipateInterpolator;import   android.view.animation.AnticipateOvershootInterpolator;import   android.view.animation.BounceInterpolator;import android.view.animation.CycleInterpolator;import   android.view.animation.DecelerateInterpolator;import   android.view.animation.LinearInterpolator;import   android.view.animation.OvershootInterpolator;import   android.widget.Button;import android.widget.ImageView;import android.widget.TextView; public   class testActivity extends AppCompatActivity   {    ImageView   testImage;    ObjectAnimator   mObjectAnimator;    TimeInterpolator   mTimeInterpolator;    String currentInterpolator =   "AccelerateDecelerateInterpolator";    int   count =   0;     @Override    protected   void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏        setContentView(R.layout.activity_test);         testImage   =   findViewById(R.id.image_test);         mObjectAnimator   =   ObjectAnimator.ofFloat(testImage,"translationY",0,600);        mObjectAnimator.setDuration(2000);        mTimeInterpolator   = new AccelerateDecelerateInterpolator();        mObjectAnimator.setInterpolator(mTimeInterpolator);          Button   startButton =   findViewById(R.id.testButton);        startButton.setOnClickListener(new   View.OnClickListener()   {            @Override            public   void onClick(View view) {                mObjectAnimator.setInterpolator(mTimeInterpolator);                mObjectAnimator.start();            }        });        final   TextView stringAcc = findViewById(R.id.stringAcc);        stringAcc.setText("AccelerateDecelerateInterpolator");        Button   next   =findViewById(R.id.nextButton);        next.setOnClickListener(new   View.OnClickListener()   {            @Override            public   void onClick(View view) {                count   ++;                if(count>11){                    count   =   0;                }                switch(count){                    case   0:mTimeInterpolator = new   AccelerateDecelerateInterpolator();                        currentInterpolator   =   "AccelerateDecelerateInterpolator";break;                    case   1:mTimeInterpolator = new   AccelerateInterpolator();                        currentInterpolator   =   "AccelerateInterpolator";break;                    case   2:mTimeInterpolator = new   AnticipateInterpolator();                        currentInterpolator   =   "AnticipateInterpolator";break;                    case   3:mTimeInterpolator = new   AnticipateOvershootInterpolator();                        currentInterpolator   =   "AnticipateOvershootInterpolator";break;                    case   4:mTimeInterpolator = new BounceInterpolator();                        currentInterpolator   =   "BounceInterpolator";break;                    case   5:mTimeInterpolator = new   CycleInterpolator(1.0f);                        mObjectAnimator   =   ObjectAnimator.ofFloat(testImage,"translationX",0,250);                        mObjectAnimator.setDuration(2000);                        currentInterpolator   = "CycleInterpolator";break;                    case   6:mTimeInterpolator = new   DecelerateInterpolator();                        mObjectAnimator   =   ObjectAnimator.ofFloat(testImage,"translationY",0,600);                        mObjectAnimator.setDuration(2000);                        currentInterpolator   = "DecelerateInterpolator";break;                    case   7:mTimeInterpolator = new   FastOutLinearInInterpolator();                        currentInterpolator   =   "FastOutLinearInInterpolator";break;                    case   8:mTimeInterpolator = new   FastOutSlowInInterpolator();                        currentInterpolator   = "FastOutSlowInInterpolator";break;                    case   9:mTimeInterpolator = new   LinearInterpolator();                        currentInterpolator   =   "LinearInterpolator";break;                    case   10:mTimeInterpolator = new   LinearOutSlowInInterpolator();                        currentInterpolator   = "LinearOutSlowInInterpolator";break;                    case   11:mTimeInterpolator = new   OvershootInterpolator();                        currentInterpolator   =   "OvershootInterpolator";break;                  }                stringAcc.setText(currentInterpolator);            }        });    } }</code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
    activity_test.xml
      <code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><!--?xml   version="1.0" encoding="utf-8"?--><relativelayout   android:background="#ede7e7" android:layout_height="match_parent"   android:layout_width="match_parent"   tools:context="com.tobetheonlyone.testActivity"   xmlns:android="https://schemas.android.com/apk/res/android"   xmlns:app="https://schemas.android.com/apk/res-auto"   xmlns:tools="https://schemas.android.com/tools">    <imageview   android:background="@drawable/text_round"   android:id="@+id/image_test" android:layout_alignparenttop="true"   android:layout_centerhorizontal="true"   android:layout_height="50dp"   android:layout_margintop="30dp"   android:layout_width="50dp"><button   android:background="@drawable/btn_green" android:id="@+id/testButton"   android:layout_alignparentbottom="true"   android:layout_centerhorizontal="true"   android:layout_height="50dp"   android:layout_marginbottom="20dp"   android:layout_marginend="10dp" android:layout_marginstart="10dp"   android:layout_width="match_parent" android:text="开始"></button><button   android:background="@drawable/btn_green"   android:id="@+id/nextButton"   android:layout_above="@+id/testButton"   android:layout_centerhorizontal="true"   android:layout_height="50dp"   android:layout_marginbottom="10dp"   android:layout_marginend="10dp"   android:layout_marginstart="10dp"   android:layout_width="match_parent" android:text="下一个">    <textview   android:id="@+id/stringAcc"   android:layout_above="@+id/nextButton"   android:layout_centerhorizontal="true"   android:layout_height="wrap_content" android:layout_marginbottom="10dp"   android:layout_width="wrap_content"   android:textsize="25sp"> </textview></button></imageview></relativelayout></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
    btn_green.xml
    <code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><!--?xml   version="1.0" encoding="utf-8"?--><selector   xmlns:android="https://schemas.android.com/apk/res/android">     <item   android:state_pressed="true">        <shape>            <solid   android:color="#07e18c">            <stroke   android:color="#07e18c"   android:width="1.0dp">            <corners   android:radius="3dp">        </corners></stroke></solid></shape>    </item>    <item>        <shape>            <stroke   android:color="#06f799"   android:width="2.0dp">            <corners   android:radius="3dp">        </corners></stroke></shape>    </item></selector></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>
    text_round.xml
      <code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><code><!--?xml   version="1.0" encoding="utf-8"?--><selector   xmlns:android="https://schemas.android.com/apk/res/android">     <item>        <shape   android:shape="oval">             <size   android:height="100dp"   android:width="100dp">            <solid   android:color="#29694d">        </solid></size></shape>    </item></selector></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code></code>    

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!

本文由 @凌雪 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程