Android动画基础

先上个动画小图



Q:Android 中动画有几类?

A:目前有三种:分别是补间动画、帧动画和属性动画。

tween补间动画
通过指定View的初末状态和变化时间、方式,对View的内容完成一系列的图形变换来实现动画效果。

补间动画在细分可以分为渐变动画与转换动画

渐变动画 转换动画
alpha(AlphaAnimation) translate(TranslateAnimation)
scale(ScaleAnimation) rotate(RotetaAnimation)

对于xml中各个效果的属性设定就不在详细描述,放在下面属性动画会提到

最后通过View.startAnimation()来为子类添加动画效果

frame帧动画
AnimationDrawable 控制 animation-list xml布局
xml中内容展示
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
    <item android:drawable="@drawable/a_01" android:duration="50"/>
    <item android:drawable="@drawable/a_02" android:duration="50"/>
    <item android:drawable="@drawable/a_03" android:duration="50"/>
    <item android:drawable="@drawable/a_04" android:duration="50"/>
    <item android:drawable="@drawable/a_05" android:duration="50"/>
</animation-list>
Java中的代码
imageview.setBackground(R.anim.anim);
AnimationDrawable anims = (AnimationDrawable)imageview.getBackground()
anims.start();

属性动画(Propety Animation)

Duration动画持续时间,默认300ms
Time interpolation 插补器,定义动画的变化率
Repeat count and behavior 重复次数、以及重复模式
Animator set 动画集合,你可以定义一组动画,一起执行或者顺序执行
Frame refresh delay 帧刷新延迟,对于你的动画,多久刷新一次帧,默认为10ms
相关类
ObejctAnimator 动画的执行类
ValueAnimator 动画的执行类
AnimatorSet 用于控制一组动画的执行
TypeEvaluator 类型估值,主要用于设置动画操作属性的值
TimeInterpolator 插补器

ObjectAnimator

ObjectAnimator.ofFloat(this,"rotationX",0.0f,360.0f).setDuration(500).start();

  1. 提供了ofInt、ofFloat、ofObject,这几个方法都是设置动画作用的元素、作用的属性、动画开始、结束中间的任意个属性值。
ValueAnimator

ValueAnimator.ofFloat(0,100).setTarget(this).setDuration(1000).start();

监听动画的事件
  1. AnimatorUpdateListener
  2. AnimatorListener
  3. AnimatorListenerAdapter
AnimatorSet
ObjectAnimator anim1 = ObjectAnimator.ofFloat(this,"scaleX",1.0f,2f);
Objectanimator anim2 = ObjectAnimator.ofFloat(this,"scaleY",1.0f,2f);
AnimatorSet animSet = new AnimatorSet();
animSet.setDuration(1000);
animSet.setInterpolator(new LinearInterpolator());
animSet.playTogether(anim1,anim2);
animSet.start();
xml创建属性动画
<ObjectAnimator xmlns:android="http://schema.android.com/apk/res/android"
    android:duration="1000"
    android:propertyName="scaleX"
    android:valueFrom="1.0"
    android:valueTo="2.0"
    android:valueType="floatType"
/>
    
AnimatorInflater.loadAnimator(this,R.animator.scalex)
.setTarget(mMv)
.start();

overridePendingTransition activity转场动画效果

Material Design中的Transaction动画
Ripple Effect

MD中在用户触摸屏幕时提供反馈,有助于视觉交流,形成互动。可以通过如下代码设置波纹的背景:

android:background="?android:attr/selectableItemBackground"   //默认选项,在视图范围内展示波纹效果
android:backgournd="?android:attr/selecableItemBackgroundBorderless"  //可选项,将波纹延伸到视图之外
android:background="@drawable/customRipple" //自定义背景的ripple effect

customRipple.xml

<ripple xmlns:android="http:schemas.android.com/apk/res/androdi"
android:color="#c9352a"
>

<item>
    <shape android:shape="rectangle">
        <solid android:color="#02cce7"/>
        <corners android:radius="4dp"/>
    </shape>
</item>

</ripple>

这里通过几个button的点击效果可以直观的感受下ripples

Circular Reveal

简单翻译来说称之为圆形展现

ViewAnimationUtil.createCircularReveal(View view,int centerX,int centerY,float startRadius,float endRadius).start()
  • centerX –点击视图的 X轴中心;
  • centerY -点击视图的 Y轴中心;
  • view –要显示的视图;
  • startRadius 动画开始半径
  • startRadius 动画结束半径
Transitions
Interpolators
  • enter 决定活动视图如何进入场景
  • exit 决定活动视图如何退出场景
  • reenter 决定活动试图退出后如何再度进入场景
  • shared elements 决定活动间如何共享视图转换
VectorDrawable
  • Height & Width –矢量图像的实际大小;
  • Viewport Height & Width –声明描述矢量路径的虚拟画布的大小;
  • Group name –声明路径所属的组名;
  • Pivot X & Y –声明群组规模和旋转所使用的中心点;
  • Path Fill Color –描述矢量路径的填充色;
  • Path Data –声明用于绘制矢量的矢量路径数据。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...
    Rtia阅读 6,231评论 1 38
  • 为什么要引入属性动画 逐帧动画主要是用来实现动画的,而补间动画才能实现控件的渐入渐出、移动、旋转和缩放效果;属性动...
    凌川江雪阅读 591评论 0 3
  • 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让全面说说Android的动画,所以今...
    未聞椛洺阅读 2,746评论 0 10
  • 学习资料:Android开发艺术探索和Animation的api 1.属性动画 属性动画可以对任意对象的属性进行动...
    英勇青铜5阅读 2,015评论 0 22
  • 作者卡洛.罗韦利是意大利著名物理学家,圈量子引力理论开创者之一,本书的内容是量子物理研究的新进展,它颠覆了对时间的...
    瞰川阅读 3,976评论 0 3