程序员Android开发探索首页投稿(暂停使用,暂停投稿)

Android动画基础

2016-10-08  本文已影响0人  JocherCH

先上个动画小图



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()
Transitions
Interpolators
VectorDrawable
上一篇下一篇

猜你喜欢

热点阅读