网友通过本文主要向大家介绍了android 动画原理,android动画总结,android动画,android动画详解,android动画有几种等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android动画原理总结
计算机实现动画的原理 :1 跟放电影一样,定时改变图像。 2 必须有定时器动画的分类 : 属性动画;视图动画;drawable 动画drawable 动画 --静态图片动画----需要准备好一帧帧的图片,打包体积大。只有属性动画和 视图动画不能完成时,才考虑它。属性动画和视图动画都是动态产生每帧的图像的,不影响程序的体积。属性动画是来替代视图动画的,目标不限于 view 类对象,目标对象即使不可见也可以动。属性动画的实质就是 定时器发生---->计算新的属性的值 ---->将新值设置到目标对象的属性中---->目标对象刷新----> UI 发生变化。不断的重复这个过程,就出现动画了。推荐使用属性动画。相关的类 :1 Animator 基类,不能直接使用。 2 ValueAnimator ,实现了动画框架的大部分功能,但是只计算属性的值,并不把这个值设置到对象中。如果用的话,需要我们自己写一个侦听类,侦听 ValueAnimator 的事件,然后将属性的值设置到对象中,可以用它,但有更好的, 当更好用的不能用时,才考虑这个。 3 ObjectAnimator ValueAnimator 的子类,它就是更好的选择。它会计算对象 的属性值,然后将新值设置到对象的那个属性。大部分时间使用这个类即可。 4 AnimatorSet Set 表示动画集合。它包含多个动画。这多个动画可以同时执行, 可以一个个顺序执行,也可以执行完一个然后隔一段时间再执行另一个。使用属性动画类 :要让一个对象动起来,需要向 ObjectAnimator 指定这个对象, 指定要改变的那个属性,指定一个持续时间,还要指定动画本身的参数,比如平移的话,要指定开始位置和结束位置。匀速平移ObjectAnimator anim = ObjectAnimator.ofFloat( btnTarget,"translationX",0f,200f); anim.setDuration(2000); anim.start();旋转
ObjectAnimator anim = ObjectAnimator .ofFloat( btnTarget, "rotationX", 0f, 180f); anim.setDuration(2000); anim.start();使用动画集合类 :
AnimatorSet set = new AnimatorSet(); ObjectAnimator anim1 = ObjectAnimator.ofFloat(btnTarget,"rotationX",0f,180f); anim1.setDuration(2000); ObjectAnimator anim2 = ObjectAnimator.ofFloat(btnTarget,"rotationX",180f,0f); anim2.setDuration(2000); ObjectAnimator anim3 = ObjectAnimator.ofFloat(btnTarget,"rotationY",0f,180f); anim3.setDuration(2000); ObjectAnimator anim4 = ObjectAnimator.ofFloat(btnTarget,"rotationY",180f,0f); anim4.setDuration(2000); set.play(anim1).before(anim2); set.play(anim3).before(anim4); set.start();xml 实现 1 单个动画
<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--> <cke:objectanimator xmlns:android="http://schemas.android.com/apk/res/an droid" android:propertyname="translationX" android:duration="2000" android:valuefrom="0dp" android:valueto="200dp" android:valuetype="floatType"> </cke:objectanimator>
代码:
ObjectAnimator anim =(ObjectAnimator)AnimatorInflater.loadAnimator( MainActivity.this,R.animator.rotate_anim); anim.setTarget(btnTarget); anim.start();
多个动画
<set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially"> <cke:objectanimator android:propertyname="rotationX" android:duration="2000" android:valuefrom="0" android:valueto="180" android:valuetype="floatType"> <cke:objectanimator android:propertyname="rotationX" android:duration="2000" android:valuefrom="180" android:valueto="0" android:valuetype="floatType"> </cke:objectanimator></cke:objectanimator></set>
代码:
AnimatorSet set = (AnimatorSet)AnimatorInflater.loadAnimator( MainActivity.this,R.animator.rotate_anim); set.setTarget(btnTarget); set.start();
总结: 动画离不了定时器; android 系统有三种动画 ; 属性动画可以在纯程序中使用,也可以在 xml 中使用,xml 必须放在 res/animator 文件夹下;为了更好的重用,应尽量在 xml 中定义动画;属性动画的实质就是定时改变对象的属性的值;可以使用动画集合类,产生出复杂的动画