前言

之前已经讲过动画相关的内容,没有接触过的读者可以看下笔者之前对android动画使用的整理。

Android动画总结 (valueAnimator、objectAnimator、AnimatorSet、PropertyValuesHolder、Interpolator)

插值器概念

动画插值器可以用来控制动画的变化规律,比如变化速率是先快后慢,还是先慢后快,或者是更细节的其他。

android系统已经提供了一些常用的插值器,基本已经能满足大部分需求,对此有兴趣的可以看下笔者前言中的文章,此处就不徒增篇幅了。

对于具体的业务,有时候系统的插值器也没法满足需求。
对于某些场景,一方面需要对动画速率有较细致的控制,另一方面可能使用较为广泛,需要将此逻辑集成,这时就可以使用自定义的插值器。

例子(Demo)

本文的例子实现了“弹窗弹性效果”的插值器。
主要有三个动画:

  1. 使用自定义插值器,从上至下的动画。
  2. 使用自定义插值器,从左至右的动画。
  3. 不使用自定义插值器,实现与插值器类似的效果。(控制上不如插值器细致)

个人理解:
自定义插值器一般还是要在“对动画运动规律需要细致控制”的前提下,大多数情况其实直接像Demo中的第三个动画一样就可以满足需求了。

代码

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {//uiprivate Button btnAnimOne;private Button btnAnimTwo;private Button btnAnimThree;private TextView tvAnim;//dataObjectAnimator anim;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initViews();}private void initViews() {tvAnim = findViewById(R.id.tv_anim_main);btnAnimOne = findViewById(R.id.btn_anim_one_main);btnAnimTwo = findViewById(R.id.btn_anim_two_main);btnAnimThree = findViewById(R.id.btn_anim_three_main);btnAnimOne.setOnClickListener(this);btnAnimTwo.setOnClickListener(this);btnAnimThree.setOnClickListener(this);}private void startAnimOne() {if (tvAnim == null || anim != null) {return;}tvAnim.setVisibility(View.INVISIBLE);anim = ObjectAnimator.ofFloat(tvAnim, "y", 0, tvAnim.getY());anim.setDuration(1000);anim.setInterpolator(new ElasticityInterpolator());anim.addListener(new AnimatorListenerAdapter() {@Override public void onAnimationStart(Animator animation) {tvAnim.setVisibility(View.VISIBLE);}@Override public void onAnimationEnd(Animator animation) {anim = null;}});anim.start();}private void startAnimTwo() {if (tvAnim == null || anim != null) {return;}tvAnim.setVisibility(View.INVISIBLE);anim = ObjectAnimator.ofFloat(tvAnim, "x", 0, tvAnim.getX());anim.setDuration(1000);anim.setInterpolator(new ElasticityInterpolator());anim.addListener(new AnimatorListenerAdapter() {@Override public void onAnimationStart(Animator animation) {tvAnim.setVisibility(View.VISIBLE);}@Override public void onAnimationEnd(Animator animation) {anim = null;}});anim.start();}private void startAnimThree() {if (tvAnim == null || anim != null) {return;}tvAnim.setVisibility(View.INVISIBLE);float x = tvAnim.getX();anim = ObjectAnimator.ofFloat(tvAnim, "x", 0, x * 1.2f, x * 0.9f, x * 1.1f, x * 0.9f, x);anim.setDuration(1000);anim.addListener(new AnimatorListenerAdapter() {@Override public void onAnimationStart(Animator animation) {tvAnim.setVisibility(View.VISIBLE);}@Override public void onAnimationEnd(Animator animation) {anim = null;}});anim.start();}@Override public void onClick(View v) {int id = v.getId();switch (id) {case R.id.btn_anim_one_main:startAnimOne();break;case R.id.btn_anim_two_main:startAnimTwo();break;case R.id.btn_anim_three_main:startAnimThree();break;}}
}

ElasticityInterpolator.java

public class ElasticityInterpolator extends BaseInterpolator {/*** @param input 表示动画进度,大小从0到1*/@Override public float getInterpolation(float input) {if (input <= 0.5) {return input * 2;} else if (input <= 0.6f) {return 1 + (input - 0.5f) * 2;} else if (input <= 0.7) {return 1.2f - ((input - 0.6f) * 3);} else if (input <= 0.8) {return 0.9f + ((input - 0.7f) * 2);} else if (input <= 0.9) {return 1.1f - ((input - 0.8f) * 2);} else if (input <= 1) {return input;}return 0;}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:id="@+id/tv_anim_main"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="提示"android:textSize="30sp"android:textStyle="bold"android:elevation="100dp"/><Buttonandroid:id="@+id/btn_anim_one_main"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="竖向动画"android:textSize="30sp"/><Buttonandroid:id="@+id/btn_anim_two_main"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="横向动画"android:textSize="30sp"/><Buttonandroid:id="@+id/btn_anim_three_main"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="非插值器实现"android:textSize="30sp"/></LinearLayout>

android自定义动画插值器(Interpolator)相关推荐

  1. Android自定义动画三-SVG动画

    Android自定义动画三-SVG动画 本篇文章主要是对SVG的一个介绍和使用,以及Android中对SVG的一个支持,从而可以帮助我们在android下很轻松的通过SVG实现一些非常酷炫的动画效果. ...

  2. Android自定义动画专题二

    android自定义动画专题二 在上篇文章中给大家介绍了android自定义动画的第一种表现形式:view的绘制:不过这只是一种单纯利用自定义控件绘制的方式去实现:这篇文章会给大家演示如何通过自定义控 ...

  3. android 自定义loading,Android自定义动画-StarLoadingView

    今天来分享第二个自定义loading的动画,起了个名字叫 蹦跶的星星 ,还是老规矩先介绍,后上图. 实现效果在最后,GIF有点大,手机流量慎重. 介绍 首先声明做这个动画的初衷是为了学习和分享,所以从 ...

  4. Android自定义动画专题一

    Android自定义动画 在目前的移动端产品中,不管是app还是网页一个好看酷炫的页面总是会第一时间吸引人的眼球,那么对于android开发人员来说,要想实现一个好看的页面必然需要掌握自定义控件以及自 ...

  5. Android自定义动画学习,实现左右摇摆动画

    (转载)http://johnnyg.iteye.com/blog/2074464 我们都知道Android SDK给我们提供了4种常用的动画效果分别是: AlphaAnimation:透明度变化动画 ...

  6. android 属性翻牌动画,Android自定义动画--卡牌翻牌动画

    Android系统中自带了四种动画,但是都只是平面上的并不能实现我们很常见的翻牌动画,所以今天我们就要通过自定义动画来实现翻牌动画. 要实现翻牌动画,我们需要了解三个类,一个是matrix类,一个是c ...

  7. Android 自定义动画(实现类似分享动画)

    最近在开发app中,要实现点击进入分享动画页面,然后照着每个Item的功能,来实现各自的功能 效果图如下: 首选自定义动画Activity import android.animation.Anima ...

  8. Android动画学习笔记(二)——动画插值器Interpolator

    前言:上篇博客我们对补间动画的xml及代码生成做了简单的介绍,今天我们来讲讲动画的一个核心类Interpolator插值器,对于数学好的童鞋来说,学习本节内容就soeasy了!(ps:本人数学很差,都 ...

  9. Android 自定义动画 LoadingView

    项目地址:https://github.com/CuteWiseCode/MyLoadingView 先上效果图 实现思路: 代码实现主要结合自定义view 以及动画属性的方式,根据需求调整动画的展示 ...

最新文章

  1. Eclipse不自动编译 设置后重新编译仍然没有编译
  2. AngularJS自定义表单验证
  3. android5.0(Lollipop) BLE Peripheral深入理解系统篇之提高篇
  4. 【JavaSE_08】Java中static、继承、重写
  5. cad文字递增快捷键_CAD的这些快捷键,好用到暴风哭泣,一秒钟完成3小时操作...
  6. hdu 1874 Dijkstra算法模板
  7. Python第三方模块tesserocr安装
  8. ios查看线程数量_关于iOS多线程,你看我就够了(已更新)
  9. Java工作笔记-接入互联网的免费WebService
  10. StringBuffer的基本用法 2101 0311
  11. 揭开KPI异常检测顶级AI模型面纱
  12. 5G让万物互联成为可能 大连接时代谋划物联网
  13. 【Solr原理】Leader Shard选举
  14. 新一代口腔清洁神器 素诺智能可视超声波洁牙仪T11Pro评测
  15. 项目进度管理方法——里程碑
  16. Android中的传感器之---光线传感器
  17. 超级实时图形计算机,HyperCalc Graphing Calculator(超级图形计算器)
  18. 二维码扫描+长按识别二维码demo
  19. 关于访问后端接口报404的问题——全网最详细的404错误详解
  20. python快速找到列表中出现最多的元素

热门文章

  1. 关于CAN总线的理解
  2. 计算机网络 实验二 交换机配置与 MAC 地址转发表管理
  3. 【学习笔记】数理统计习题十一
  4. 使用C语言实现链栈(带头结点和不带头结点)
  5. 跨过文档管理这道“槛”
  6. LADP打通Gitlab(全指南)
  7. Python办公自动化,全网最全整理!
  8. 读取raw,arw等格式图像,并转格式
  9. 使用缓存实现前端性能优化——浏览器缓存机制、缓存分类
  10. python聚宽量化_今天开始使用聚宽的系统学习python量化交易