简介

补间动画的原理:
每次绘制视图时View所在的【ViewGroup】中的drawChild函数获取该View的Animation的值,然后调用canvas.concat (transformToApply.getMatrix()),通过【矩阵运算】完成动画帧,如果动画没有完成,继续调用【invalidate()】函数,启动下次绘制来驱动动画。动画过程中的帧之间间隙时间是绘制函数所消耗的时间,可能会导致动画消耗比较多的CPU资源,最重要的是,动画改变的只是显示,并不能响应事件。
主要特点:
  • 补间动画只能应用于View对象,而且只支持一部分属性,如支持缩放、旋转而不支持背景颜色的改变。
  • 补间动画只是改变了View对象绘制的位置,而没有改变View对象本身,比如对于一个按钮,在动画过程中,触发按钮点击的区域仍是动画前的区域。
  • 补间动画就是一系列View形状的变换,如大小的缩放,透明度的改变,位置的改变,动画的定义既可以用代码定义也可以用XML定义,建议用XML定义。
  • 默认情况下,所有动画是同时进行的,可以通过startOffset属性设置各个动画的开始偏移(开始时间)来达到动画顺序播放的效果。
  • 可以通过设置interpolator属性改变动画渐变的方式,默认为AccelerateDecelerateInterpolator。
四种动画类型:
  • alpha        AlphaAnimation
  • scale        ScaleAnimation
  • translate  TranslateAnimation
  • rotate       RotateAnimation
  • set            AnimationSet
三种参照物模式:
  • Animation.ABSOLUTE    绝对位置
  • Animation.RELATIVE_TO_SELF    相对于自身的位置
  • Animation.RELATIVE_TO_PARENT    相对于父控件的位置
可监听的动画状态:
  • onAnimationEnd(Animation animation) - 当动画结束时调用
  • onAnimationRepeat(Animation animation) - 当动画重复时调用
  • onAniamtionStart(Animation animation) - 当动画启动时调用
使用xml中定义的动画:
         Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha);
         iv.startAnimation(aa);

演示代码

public class MainActivity extends Activity {
    private ImageView iv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
    }
    //透明度动画
    public void alpha(View view) {
        AlphaAnimation aa = new AlphaAnimation(0.2f, 1.0f);//开始、结束时的透明度。1位全不透明,0为全透明
        aa.setDuration(2000);//播放时间
        aa.setRepeatCount(1);//重复次数,播放次数=重复次数+1;Animation.INFINITE 或 -1 表示不停止的播放
        aa.setRepeatMode(Animation.REVERSE);//重复模式:【REVERSE】倒序重复播放(往返形式),【RESTART】重新开始执行(默认)
        aa.setFillAfter(true);//默认为false,设为true则表示播放完毕后会保持播放完毕那一刻的图像;同样,seFillBefore表示是否……播放开始时的图像。
        aa.setInterpolator(new AccelerateInterpolator());//设定变化速度,AccelerateInterpolator表示先慢后快
        iv.startAnimation(aa);
    }
    //位移动画
    public void trans(View view) {
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0);
        //【int fromXType, float fromXValue, int toXType, float toXValue,】 【后面是4个Y】
        //【开始时x相对谁的距离,结束时x相对谁的距离】其中:RELATIVE_TO_PARENT代表相对于父控件
        ta.setDuration(2000);
        ta.setRepeatCount(1);
        ta.setRepeatMode(Animation.RESTART);//RESTART重新开始执行(默认)
        ta.setInterpolator(new BounceInterpolator());//动画结束的时候弹起
        iv.startAnimation(ta);
    }
    //缩放动画
    public void scale(View view) {
        ScaleAnimation sa = new ScaleAnimation(0.1f, 2.0f, 0.1f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        //【float fromX, float toX, float fromY, float toY,】【 int pivotXType, float pivotXValue, int pivotYType, float pivotYValue】
        //【开始和结束时x,y的缩放比例】【x和y缩放时所使用的模式和中心点】其中:RELATIVE_TO_SELF 代表相对自身
        sa.setDuration(2000);
        sa.setInterpolator(new CycleInterpolator(1));//动画循环播放特定的次数,速率改变沿着正弦曲线
        iv.startAnimation(sa);
    }
    //旋转动画
    public void rotate(View view) {
        RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        //【float fromDegrees, float toDegrees, 】【int pivotXType, float pivotXValue, int pivotYType, float pivotYValue】
        //【开始和结束时旋转的角度】【x和y旋转时所使用的模式和中心点】
        ra.setDuration(2000);
        ra.setRepeatCount(1);
        ra.setRepeatMode(Animation.REVERSE);
        ra.setInterpolator(new AnticipateInterpolator());//开始的时候向后然后向前甩
        iv.startAnimation(ra);
    }

    //组合动画
    public void set(View view) {
        AnimationSet set = new AnimationSet(false);//是否使用共同的播放速度
        set.setInterpolator(new MyInterpolator(2));//使用自定义的Interpolator
        //位移
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
        ta.setDuration(1200);
        ta.setRepeatCount(1);
        ta.setRepeatMode(Animation.REVERSE);
        //缩放
        ScaleAnimation sa = new ScaleAnimation(0.1f, 1.2f, 0.1f, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(1000);
        sa.setRepeatCount(1);
        sa.setRepeatMode(Animation.REVERSE);
        //旋转
        RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        ra.setDuration(2000);
        ra.setRepeatCount(0);
        //将上面这些动画放到集合中
        set.addAnimation(ra);
        set.addAnimation(ta);
        set.addAnimation(sa);
        iv.startAnimation(set);
    }

    class MyInterpolator implements Interpolator {
        private float mFactor;
        private int i;
        public MyInterpolator(int i) {//初始化时设定速率变化规则
            this.i = i;
        }
        @Override
        public float getInterpolation(float input) {
            //参数input是一个0.0f~1.0f的浮点数,Interpolator可认为是一个基于input构造出的函数
            switch (i) {
            case 2://“变化率”呈二次方
                mFactor = input * input;
                break;
            case 3://“变化率”呈三次方
                mFactor = input * input * input;
                break;
            default://“变化率”是匀速的
                mFactor = input;
                break;
            }
            return mFactor;
        }
    }
}

演示布局

    
<RelativeLayout 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"
    tools:context=".MainActivity" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="alpha"
            android:text="透明度" />
        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="trans"
            android:text="移位" />
        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="scale"
            android:text="缩放" />
        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="rotate"
            android:text="旋转" />
        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="set"
            android:text="组合" />
    </LinearLayout>
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="10dp"
        android:background="#6699ff00"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

补间动画 Interpolator 简介 示例的更多相关文章

  1. Android中四种补间动画的使用示例(附代码下载)

    场景 Android中四种补间动画. 透明度渐变动画 旋转动画 缩放动画 平移动画 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程 ...

  2. 【补间动画示例】Tweened Animation

    代码中定义动画示例 public class MainActivity extends ListActivity </integer> 常用的Activity转场动画中的补间动画 publ ...

  3. Android动画效果之Tween Animation(补间动画)

    前言: 最近公司项目下个版本迭代里面设计了很多动画效果,在以往的项目中开发中也会经常用到动画,所以在公司下个版本迭代开始之前,抽空总结一下Android动画.今天主要总结Tween Animation ...

  4. Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)

    1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...

  5. 【Android - 进阶】之Animation补间动画

    补间动画也叫View动画,它只能针对View进行动画操作,且补间动画操作的只是View中可见的部分,即只操作界面,对于可点击区域等都不会进行操作. 在Android中,补间动画的顶级类是Animati ...

  6. Android Animation动画详解(一): 补间动画

    前言 你有没有被一些APP中惊艳的动画效果震撼过,有没有去思考,甚至研究过这些动画是如何实现的呢? 啥?你没有思考,更没有研究过? 好吧,那跟着我一起来学习下如何去实现APP中那些让我们惊羡的动画特效 ...

  7. android 帧动画,补间动画,属性动画的简单总结

      帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...

  8. android 补间动画和Animation

    介绍: 补间动画是一种设定动画开始状态.结束状态,其中间的变化由系统计算补充.这也是他叫做补间动画的原因. 补间动画由Animation类来实现具体效果,包括平移(TranslateAnimation ...

  9. Android 学习笔记多媒体技术之 Drawable类+Tween(补间动画)+Frame(帧动画)

    学习内容: 1.了解Drawable类的作用 2.如何使用Drawable... 3.了解Tween动画... 4.如何创建和使用Tween动画... 1.Drawable类...   Drawabl ...

随机推荐

  1. Linux 4.1内核编译报告

    编译环境 Arch Linux on VirtualBox 下载内核 https://www.kernel.org/ 下载的内核压缩包,此时的最新内核版本为4.1: 解压包 # tar -xvJf l ...

  2. php图片上传

    //处理图片 private function imageDeal($param){ $arrType=array('image/jpg','image/bmp','image/png','image ...

  3. JS中 confirm()方法的使用?

    confirm() 方法用于显示一个带有指定消息和 OK 及取消按钮的对话框. 如果用户点击确定按钮,则 confirm() 返回 true.如果点击取消按钮,则 confirm() 返回 false ...

  4. 关于PHP参数的引用传递和值传递

    如果希望编写一个名为increment()的函数来增加一个变量的值,我们可能会按如下方式编写这个函数: 这段代码是没有用的.下面测试代码的输出结果是“10”. $value 的内容没有被修改.这要归因 ...

  5. 探究ListView 的缓存机制

    概述 ListView 是继承AbListView,AbListView是所有列表类控件的基类. ListView的数据加载 在ListView数据加载中最关键的一个函数就是makeAndAddVie ...

  6. 黑马程序员—C语言的函数、数组、字符串

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 一.函数 定义:函数是代码复用的一种机制. 函数的基本语法: 返回类型 函数名 ( 参数类型 参 ...

  7. laravel框架——上传、下载文件

    文件上传 在config文件夹下新建一个 项目名.php return [ 'title' => 'My Test', 'posts_per_page' => 5, 'uploads' = ...

  8. DIV+CSS两种盒子模型

    盒子模型有两种,分别是 IE 盒子模型和标准 W3C 盒子模型.他们对盒子模型的解释各不相同, 先来看看我们熟悉的标准盒子模型: 从上图可以看到标准 W3C 盒子模型的范围包括 margin.bord ...

  9. Github上最受关注的前端大牛,快来膜拜吧!

    1. Paul Irish Github主页: https://github.com/paulirish 个人主页: http://paulirish.com 维基百科: http://en.wiki ...

  10. 哎,就硬盘还不是最掉价的,1999的自配主机,VIRTUALBOX里虚拟机,聊以自慰吧。

    安装时注意的问题,要是不测试MYSQL,则CONFIGURE参数和DISABLE-MYSQL,在编译时有提示的. 然后就是LIBTOOL包过老的问题,以及未安装LIBTOOL包的问题. 最后,是运行命 ...