补间动画 Interpolator 简介 示例
简介
补间动画的原理:每次绘制视图时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;@Overrideprotected 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;}@Overridepublic 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" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal" ><Buttonandroid:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="alpha"android:text="透明度" /><Buttonandroid:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="trans"android:text="移位" /><Buttonandroid:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="scale"android:text="缩放" /><Buttonandroid:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="rotate"android:text="旋转" /><Buttonandroid:layout_width="0dip"android:layout_height="wrap_content"android:layout_weight="1"android:onClick="set"android:text="组合" /></LinearLayout><ImageViewandroid: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 简介 示例的更多相关文章
- Android中四种补间动画的使用示例(附代码下载)
场景 Android中四种补间动画. 透明度渐变动画 旋转动画 缩放动画 平移动画 注: 博客: https://blog.csdn.net/badao_liumang_qizhi关注公众号 霸道的程 ...
- 【补间动画示例】Tweened Animation
代码中定义动画示例 public class MainActivity extends ListActivity </integer> 常用的Activity转场动画中的补间动画 publ ...
- Android动画效果之Tween Animation(补间动画)
前言: 最近公司项目下个版本迭代里面设计了很多动画效果,在以往的项目中开发中也会经常用到动画,所以在公司下个版本迭代开始之前,抽空总结一下Android动画.今天主要总结Tween Animation ...
- Android动画总结#补间动画(Tween Animation/View Animation) #帧动画(Frame Animation/Drawable Animation)#属性动画(PropertyAnimation)
1.共有三种动画,英文名字多种叫法如下 第一种动画:补间动画(Tween Animation/View Animation) 四个:RotateAnimation旋转. AlphaAnimation透 ...
- 【Android - 进阶】之Animation补间动画
补间动画也叫View动画,它只能针对View进行动画操作,且补间动画操作的只是View中可见的部分,即只操作界面,对于可点击区域等都不会进行操作. 在Android中,补间动画的顶级类是Animati ...
- Android Animation动画详解(一): 补间动画
前言 你有没有被一些APP中惊艳的动画效果震撼过,有没有去思考,甚至研究过这些动画是如何实现的呢? 啥?你没有思考,更没有研究过? 好吧,那跟着我一起来学习下如何去实现APP中那些让我们惊羡的动画特效 ...
- android 帧动画,补间动画,属性动画的简单总结
帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...
- android 补间动画和Animation
介绍: 补间动画是一种设定动画开始状态.结束状态,其中间的变化由系统计算补充.这也是他叫做补间动画的原因. 补间动画由Animation类来实现具体效果,包括平移(TranslateAnimation ...
- Android 学习笔记多媒体技术之 Drawable类+Tween(补间动画)+Frame(帧动画)
学习内容: 1.了解Drawable类的作用 2.如何使用Drawable... 3.了解Tween动画... 4.如何创建和使用Tween动画... 1.Drawable类... Drawabl ...
随机推荐
- Qt中widget重新setParent需要注意的问题
有时候需要在widget中重新setParent,但会发现setParent有时候会出现问题,比如子窗口不在刷出来等等. 其实,有一点是需要注意的,就是Qt文档里说的,如果你当前widget重新设置了 ...
- HTML5 QQ登录背景动态图片
预览效果如图所示: 代码如下: <!DOCTYPE html> <head> <meta http-equiv="Content-Type" cont ...
- TP开发小技巧
TP开发小技巧原文地址http://wp.chenyuanzhao.com/wp/2016/07/23/tp%E5%BC%80%E5%8F%91%E5%B0%8F%E6%8A%80%E5%B7%A7/ ...
- 使用微妙计算PHP脚本执行时间
在PHP中,大多数的时间格式都是以UNIX时间戳表示的,而UNIX时间戳是以s(秒)为最小的计量时间的单位.这对某些应用程序来说不够精确,所以可以调用microtime()返回当前UNIX时间戳和微妙 ...
- Android模拟器genymotion安装与eclipse 插件安装
推荐一款Android模拟器"Genymotion",有点速度快,占用资源少,可整合eclipse.闲话少谈,看安装步骤. 1.下载地址:https://www.genymotio ...
- C#编写的序列化通用类代码
using System; using System.IO; using System.IO.Compression; using System.Runtime.Serialization.Forma ...
- noi 3531 判断整除
3531:判断整除 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 一个给定的正整数序列,在每个数之前都插入+号或-号后计算它们的和.比如序列:1.2.4共有 ...
- JS基于时间戳写的浏览访问人数
Title:JS基于时间戳写的浏览访问人数 --2013-12-23 14:07 <script language="JavaScript"> var timesta ...
- listview底部增加按钮
View bottomView=getActivity().getLayoutInflater().inflate(R.layout.btn_my_course, null); myCourses = ...
- cf C. Hamburgers
http://codeforces.com/contest/371/problem/C 二分枚举最大汉堡包数量就可以. #include <cstdio> #include <cst ...