补间动画使用很简单,几行代码便能实现效果,上图上代码:

xml很简单,不用说,先给每个按钮加上点击监听

 findViewById(R.id.rotateBtn).setOnClickListener(new RotateButtonListener());findViewById(R.id.rotate3DBtn).setOnClickListener(new Rotate3DButtonListener());findViewById(R.id.scaleBtn).setOnClickListener(new ScaleButtonListener());findViewById(R.id.alphaBtn).setOnClickListener(new AlphaButtonListener());findViewById(R.id.translateBtn).setOnClickListener(new TranslateButtonListener());findViewById(R.id.groupBtn).setOnClickListener(new GroupButtonListener());findViewById(R.id.xmlBtn).setOnClickListener(new XMLButtonListener());

旋转动画:

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
RotateAnimation(float fromDegrees, float toDegrees)
public RotateAnimation(Context context, AttributeSet attrs)

RotateAnimation一共4种构造方法,前三种比较常用,最后一种是调用AnimationUtils.createAnimationFromXml或者AnimationUtils.loadAnimation才会用到,用于从xml创建动画,后面会演示

来讲一下前三种,参数解释代码注释里面我有写,这里说一下各自的默认值

private int mPivotXType = ABSOLUTE;
private int mPivotYType = ABSOLUTE;
private float mPivotXValue = 0.0f;
private float mPivotYValue = 0.0f;
private float mPivotX=0.0f;
private float mPivotY=0.0f;

其它的动画大体相似,在此不再赘述

class RotateButtonListener implements View.OnClickListener {public void onClick(View v) {//参数1:从哪个旋转角度开始//参数2:转到什么角度//后4个参数用于设置围绕着旋转的圆的圆心在哪里//参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标//参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴//参数5:确定y轴坐标的类型//参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴RotateAnimation rotateAnimation = new RotateAnimation(0, 720,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);rotateAnimation.setDuration(2000);v.startAnimation(rotateAnimation);}}

缩放动画:

class ScaleButtonListener implements View.OnClickListener {public void onClick(View v) {//参数1:x轴的初始值//参数2:x轴收缩后的值//参数3:y轴的初始值//参数4:y轴收缩后的值//参数5:确定x轴坐标的类型//参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴//参数7:确定y轴坐标的类型//参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1.5f, 0, 1.5f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);scaleAnimation.setDuration(2000);v.startAnimation(scaleAnimation);}}

透明度动画:

class AlphaButtonListener implements View.OnClickListener {public void onClick(View v) {//创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//设置动画执行的时间alphaAnimation.setDuration(2000);//将alphaAnimation对象添加到AnimationSet当中//使用ImageView的startAnimation方法执行动画v.startAnimation(alphaAnimation);}}

平移动画:

class TranslateButtonListener implements View.OnClickListener {public void onClick(View v) {//参数1~2:x轴的开始位置//参数3~4:X轴的结束位置//参数5~6:Y轴的开始位置//参数7~8:Y轴的结束位置TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,Animation.RELATIVE_TO_SELF, 2f,Animation.RELATIVE_TO_SELF, 0f,Animation.RELATIVE_TO_SELF, 0f);translateAnimation.setDuration(2000);v.startAnimation(translateAnimation);}}

组合动画:

private class GroupButtonListener implements View.OnClickListener {@Overridepublic void onClick(View v) {//传true代表使用系统默认的interpolator,传false代表使用自己自定义的interpolatorAnimationSet animationSet=new AnimationSet(true);TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -2f,Animation.RELATIVE_TO_SELF, 0f,Animation.RELATIVE_TO_SELF, 0f,Animation.RELATIVE_TO_SELF, 0f);translateAnimation.setDuration(2000);AlphaAnimation alphaAnimation=new AlphaAnimation(0,1);alphaAnimation.setDuration(2000);animationSet.addAnimation(translateAnimation);animationSet.addAnimation(alphaAnimation);v.startAnimation(animationSet);}}

XML动画:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"//下方属性为四种动画共有属性android:duration="3000"  //动画持续时间android:startOffset="1000"  //延持执行时间android:fillBefore="true"  //动画执行完后是否停留在初始状态 默认trueandroid:fillAfter="false"  //动画执行完后是否停留在最后状态 默认false 优先于fillBeforeandroid:fillEnabled="true"  //fillBefore属性是否可用 默认为trueandroid:repeatMode="restart"  //动画播放模式 restart是顺序播放 reverse为倒序播放 默认正序android:repeatCount = “0” // 重放次数(动画的播放次数=重放次数+1),为infinite时无限重复 //translate动画特有属性android:fromXDelta="0"  //X起始坐标android:toXDelta="500"  //X终点坐标android:fromYDelta="0"  //Y起始坐标android:toYDelta="500"  //Y终点坐标/>
private class XMLButtonListener implements View.OnClickListener {@Overridepublic void onClick(View v) {Animation animation = AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.translate);v.startAnimation(animation);}}

其它动画的xml动画大体相似,这里说几个需要注意的地方

缩放动画:

fromXScale fromYScale 动画在X和Y轴上的起始缩放倍数 ,1.0表示正常大小,比1大是放大,比1小是缩小, 0.0表示收缩到没有

privotX prrvotY 缩放轴点的X,Y坐标

设置是数字时(如50),轴点是在左上角原点X或Y方向加上该50px的点

设置是百分比时(如50%),轴点是在左上角该视图50%宽度或高度距离的点

设置为百分之P时(如50%p),轴点是在左上角该视图的父视图50%宽度或高度的点

可以使用xml写组合动画,使用set标签,默认是一起播放,如果想设置播放顺序可以使用startoffset

我们除了可以给视图加动画,还可以使用补间动画来切换跳转界面或者fragment

left_slide_out.xml

<translate xmlns:android="http://schemas.android.com/apk/res/android"android:fromXDelta="0"android:toXDelta="-100%"android:duration="1000"/>

right_slide_in.xml

<translate xmlns:android="http://schemas.android.com/apk/res/android"android:fromXDelta="100%"android:toXDelta="0"android:duration="1000"/>
overridePendingTransition(R.anim.right_slide_in,R.anim.left_slide_out);
startActivity(new Intent(AnimationActivity.this,Activity2.class));

我们使用v.startAnimation()来开始动画,那它是如何执行的呢?让我们来看看源码

startAnimation()方法

/*** Start the specified animation now.** @param animation the animation to start now*/public void startAnimation(Animation animation) {animation.setStartTime(Animation.START_ON_FIRST_FRAME);setAnimation(animation);invalidateParentCaches();invalidate(true);}

第一行是设置动画开始时间,START_ON_FIRST_FRAME代表立刻执行,也可以调用start()方法,两个方法一样

/*** Convenience method to start the animation the first time* {@link #getTransformation(long, Transformation)} is invoked.*/public void start() {setStartTime(-1);}

第二行是设置动画,其中包含了判断屏幕状态,如果屏幕是灭的,那么亮屏才会执行

public void setAnimation(Animation animation) {mCurrentAnimation = animation;if (animation != null) {// If the screen is off assume the animation start time is now instead of// the next frame we draw. Keeping the START_ON_FIRST_FRAME start time// would cause the animation to start when the screen turns back onif (mAttachInfo != null && mAttachInfo.mDisplayState == Display.STATE_OFF&& animation.getStartTime() == Animation.START_ON_FIRST_FRAME) {animation.setStartTime(AnimationUtils.currentAnimationTimeMillis());}animation.reset();}}

第三行是用于移除其父控件的PFLAG_INVALIDATED标签,这样其父控件就会重建用于渲染的显示列表,该方法不会引起invalidate方法的调用,所以第四行需要调用一遍invalidate方法

 /*** Used to indicate that the parent of this view should clear its caches. This functionality* is used to force the parent to rebuild its display list (when hardware-accelerated),* which is necessary when various parent-managed properties of the view change, such as* alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only* clears the parent caches and does not causes an invalidate event.** @hide*/protected void invalidateParentCaches() {if (mParent instanceof View) {((View) mParent).mPrivateFlags |= PFLAG_INVALIDATED;}}

既然知道了动画是如何开始执行的了,我们可以自己尝试调用一次

            rotateAnimation.start();v.setAnimation(rotateAnimation);Class<?> class1 = v.getClass();try{Method method = class1.getSuperclass().getSuperclass().getDeclaredMethod("invalidateParentCaches");method.setAccessible(true);method.invoke(v);}catch (Exception e){e.printStackTrace();}v.invalidate();

首先调用start()方法设置动画开始时间,设置动画,随后使用反射调用invalidateParentCaches方法重建渲染列表,这块我遇到了两个小问题。

第一个问题是protected的权限问题,被protected修饰的方法可以被同包下的类或子类调用。Button是View的子类,却不是一个包下,这种情况下该方法只能在子类内部调用却不能被子类对象调用,这点需要注意一下。

第二个问题是反射调用的问题,首先需要明确getDeclardMethod和getMethod两个方法的区别。getDeclaredMethod可以获取对象表示的类或接口声明的所有方法,包括公共、保护、默认(包)访问、私有方法和它所实现接口的方法,但不包括继承的方法。getMethod方法返回某个类的所有公用(public)方法包括其继承类的公用方法和它所实现接口的方法。所以说前者比后者获取的范围大,但是不包括继承的方法,所以要想从一个Button对象获取到View对象的方法需要调用两次getSuperclass。Button类的父类是TextView,TextView的父类才是View

解决了这个问题后,调用invalidate更新视图,动画便执行了

Android 补间动画相关推荐

  1. Android平移补间动画,Android 补间动画之平移动画TranslateAnimation

    Android动画系列 博客导航: 1.介绍: Android补间动画之平移动画,在实际的开发过程中,其实有好多地方需要用到平移动画,这是对于平移动画的简单介绍. 2.属性 duration 时间 f ...

  2. android image 位移动画_「translateanimation」Android 补间动画之平移动画TranslateAnimation - seo实验室...

    translateanimation 博客导航: 1.介绍: Android补间动画之平移动画,在实际的开发过程中,其实有好多地方需要用到平移动画,这是对于平移动画的简单介绍. 2.属性 durati ...

  3. Android 补间动画(Tween Animation)

    Tween Animation(补间动画): Tween动画,通过对View的内容进行一系列的图形变换 (包括平移.缩放.旋转.改变透明度)来实现动画效果.动画效果的定义可以采用XML来做也可以采用编 ...

  4. 每日一道面试题(第7期)---Android补间动画与属性动画的区别

    零零碎碎的东西总是记不长久,仅仅学习别人的文章也只是他人咀嚼后留下的残渣.无意中发现了这个每日一道面试题,想了想如果只是简单地去思考,那么不仅会收效甚微,甚至难一点的题目自己可能都懒得去想,坚持不下来 ...

  5. Android补间动画笔记

    布局文件: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...

  6. android 补间动画有停顿,Android动画原理分析(一)----补间动画

    1.基本特点 补间动画(Tween动画),是android最早的动画框架,从Android1.0开始就有. 功能:可以实现移动.旋转.缩放.渐变四种效果以及这四种效果的组合形式. 实现形式:xml和代 ...

  7. android 属性动画 补间动画,每日一道面试题(第7期)---Android补间动画与属性动画的区别...

    零零碎碎的东西总是记不长久,仅仅学习别人的文章也只是他人咀嚼后留下的残渣.无意中发现了这个每日一道面试题,想了想如果只是简单地去思考,那么不仅会收效甚微,甚至难一点的题目自己可能都懒得去想,坚持不下来 ...

  8. Android补间动画

    概述 本篇博客基于<Android开发艺术探索>,将会介绍以下两种动画: 补间动画 帧动画 事实上帧动画也是补间动画的一种,但是它们的使用方式略有不同,所以将它们分开介绍. 一.补间动画 ...

  9. 面试:Android补间动画、属性动画的原理

     Q1:属性动画相关基础概念: Android属性动画(一)-- 属性动画的基本使用_Marck的博客-CSDN博客_android 属性动画 Android属性动画(二)-- 插值器和估值器_Mar ...

最新文章

  1. 机器学习数据预处理之缺失值:前向填充
  2. 中兴物联笔试和面试随记
  3. iOS学习资源(一)
  4. PMCAFF | 刷微信朋友圈行为分析:刷朋友圈是一种感觉?有一种批皇帝批奏章的感觉...
  5. Linux之shell中的(),(()),{},=,==:{var}(cmd)((exp))=赋值==判断
  6. 下载nodejs的mysql安装包下载安装_Node.js安装 下载
  7. 1.13 编程基础之综合应用 47 大整数除法方法 python
  8. Windows Server 2016补丁更新机制
  9. 请问asp.net网页里能显示tiff格式的图片吗?
  10. spring mvc 解决csrf跨站请求攻击
  11. access 江苏计算机二级_计算机二级考试都要考些什么?
  12. 中国地图流动图(一)
  13. 如何创建一个vue项目(详细步骤)
  14. win7(win10)更改“文件类型显示图标“的终极修改方法
  15. 人一生要做的一百件事
  16. matlab开普勒方程求地球偏心距,第二章-开普勒方程PPT课件
  17. html中获取浏览器窗口宽度,JavaScript 获取浏览器窗口的大小
  18. linux 心跳灯_Linux下信号灯的使用
  19. Exception类
  20. Unity的数据本地储存的集中方法

热门文章

  1. 暑期实习|转行|宝洁供应链训练营面经
  2. 生产mysql遇到kill不掉的sql的解决方法
  3. 美国电影协会评出50部一生不得不看的经典电影
  4. 什么是OpenGL/DirectX 什么是HLSL﹑GLSL﹑Cg 什么是Draw Call
  5. CSDN下载页面无下载按钮 谷歌插件(净网大师导致)
  6. onie支持pice硬盘
  7. python 使用myo臂环
  8. 漫谈软件架构:APP架构总结
  9. 数字图像处理——中值滤波降噪
  10. 树莓派24/100 - Pico与1602显示屏相连输出2行文本