android之动画

作者: 237a476a5ad7 | 来源:发表于2019-04-04 22:23 被阅读14次

    属性动画

    view和属性动画的方法

    属性动画是从API11才有的,为了兼容旧版本可以可以采用nineoldandroid开源库:http://nineoldandroids.com

    常用的动画类:ValueAnimation.ObjectAnimation.AnimationSet。其中ObjectAnimation继承自ValueAnimation,AnimationSet是动画集合。

    属性动画有两种定义方式:1.在代码中定义,2.在xml中定义

    代码中定义使用方式:

    View.animate() 后跟 translationX() 等方法,动画会自动执行。

    ObjectAnimator使用的是.ofFloat()的方式,如果是自定义的控件,还要给加上get/set函数,然后start()方法。

    两种使用方式

    AnimatorSet的代码定义的使用方法

    AnimatorSet的代码定义

    Interpolator:插值器

    TypeEvaluator:估值器(Int,Float,Argb)

    button的宽度并不能通过属性动画来改变,因为button的setWidth()并不是view的方法,而是继承textView的方法,而属性动画只有在view中有get/set的方法才能做属性的变化。

    解决的方法有:

    1.给你的对象加上get和set的方法,如果你有权限的话(自定义的控件)

    2.用一个类来包装原始对象,间接为其提供get和set方法

    包装类提供方法

    3.采用ValueAnimator,监听动画过程,自己实现属性的改变

    ValueAnimator监听动画过程

    设置监听器:

    1.ViewPropertyAnimator.setListener() / ObjectAnimator.addListener()

    AnimatorListener接口四大回调方法:

    onAnimationStart(Animator animation)

    onAnimationEnd(Animator animation) 

    onAnimationCancel(Animator animation)

    onAnimationRepeat(Animator animation)

    2.ViewPropertyAnimator.setUpdateListener() / ObjectAnimator.addUpdateListener()

    AnimatorUpdateListener接口回调方法:每播放一帧,就调用一次

    onAnimationUpdate(ValueAnimator animation)。

    3.ObjectAnimator.addPauseListener()

    4.ViewPropertyAnimator.withStartAction/EndAction()

    5.要移除监听器时通过 set[Update]Listener(null) ,移除监听器则是通过 remove[Update]Listener() 

    属性动画案例1:一个简单的图片平移,旋转,放大 

    效果图

    view动画

    接下来讲讲view动画,它只可以实现四种效果:TranslateAnimation,ScaleAnimation,RotateAnimation.AlphaAnimation。它有两种方法实现:1.在xml文件中定义 2.通过代码来应用动画。

    1.在XML中创建动画

    fromX->toX  alpha.translate.rotate.scale

    其中缩放和旋转有pivotX.Y的轴点

    interpolator:插值器:实现动画速度,比如先快后慢,默认:@android:anin/accelerate_decelerate_interpolator

    shareInterpolator:集合中是否共享一个插值器

    duration:动画持续时间

    filllAfter:动画结束后view是否停留在最后的位置

    XML中定义view动画

    如何应用上面的动画?

    Animation am=AnimationUtils.loadAnimation(this,R.anim.filename)

    mBtn.startAnimation(am)

    2.在代码中应用动画

    AlphaAnimation aa=new AlphaAnimation(0,1)

    aa.setDuration(300)

    mBtn.startAnimation(aa)

    view动画除了实现控件的动画效果,还可以定义viewGroup的子元素的出场效果和activity的切换效果

    其中实现子元素的出场效果同样有两种方式:

    1.XML中定义

    在anim文件中创建LayoutAnimation

    创建LayoutAnimation

    delay:动画延迟出场的百分比

    animationOrder:效果开始顺序,前面,后面,随机 normal.reverse.random

    animation:指定入场动画

    在viewGrop中调用

    Android:layoutAnimation=“”

    2.代码中定义

    实例化入场动画的XML

    通过LayoutAnimationController对象绑定入场动画

    给LayoutAnimationController设置属性

    listview.setLayoutAnimation(Controller)

    代码中创建LayoutAnimation

    通过view动画来实现activity的切换效果

    overridePendingTransition(R.anim.enteranim,R.anim.exitanim)

    这个方法要放在startActivity(intent)或finish()之后

    插值器:

    AccelerateDecelerateInterpolator:先加速再减速,这是默认的 。

    FastOutSlowInInterpolator:先加速再减速。

    CycleInterpolator:这个也是一个正弦 / 余弦曲线。

    LinearInterpolator:匀速。

    AccelerateInterpolator:持续加速。

    FastOutLinearInInterpolator:加速运动。

    DecelerateInterpolator:持续减速直到 0。

    LinearOutSlowInInterpolator:持续减速。

    AnticipateInterpolator:先回拉一下再进行正常动画轨迹。

    OvershootInterpolator:动画会超过目标值一些,然后再弹回来。

    AnticipateOvershootInterpolator:开始前回拉,最后超过一些然后回弹。

    BounceInterpolator:在目标值处弹跳。

    view动画和属性动画的练手项目:https://github.com/lihaimings/ViewAndPropertyAnimaor

    相关文章

      网友评论

        本文标题:android之动画

        本文链接:https://www.haomeiwen.com/subject/oekciqtx.html