以前对Android动画这块一直是只知其一;不知其二,知道个大概,并不会使用。恰好这几天没有太多的任务要作,能够梳理一下Android动画的一些知识。Android Animation的基础用法就不说了,这里主要记录下简单实用中遇到的问题。html
主要就是android:repeatCount,android:repeatMode无效。这个问题听说是Google的工程师刻意为之。【参考:http://stackoverflow.com/questions/4480652/android-animation-does-not-repeat】。java
不过也有一些补救措施,好比能够给Animation设置AnimationListener。而后在onAnimationEnd()方法中,从新开始一遍动画便可。android
因为AnimationSet的addAnimation()方法添加的动画会按照添加动画的顺序进行矩阵变化等等处理,因此假设有一系列的动画(不仅有一个变换位置的动画)做用于A上使得A转换成了B,那么若是想经过另一系列动画使得B还转换成以前的A,最好保证先后两次转换的动画的顺序相同。好比图片image前后通过动画:a,b,c变换成image2,若是想再从image2变换成image,那么动画的顺序也须要a,b,c(固然这个前提是要有多个可能产生位置变化的动画)动画
举个例子:在XML中定义动画:先ronate、而后alpha、接着scale,最后translate。xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:re > <rotate android:duration="3000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360" > </rotate> <alpha android:duration="3000" android:fromAlpha="1.0" android:startOffset="0" android:toAlpha="0.5" /> <scale android:duration="3000" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:toXScale="4" android:toYScale="4" > </scale> <translate android:duration="3000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="-100" android:toYDelta="-800" > </translate> </set>
若是须要在把动画还原,须要:htm
AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 1.0f); RotateAnimation rotateAnimation = new RotateAnimation(360f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ScaleAnimation scaleAnimation = new ScaleAnimation(4.0f, 1.0f, 4.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); TranslateAnimation translateAnimation = new TranslateAnimation( Animation.ABSOLUTE, -100, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -800, Animation.ABSOLUTE, 0); AnimationSet set = new AnimationSet( true); set.setInterpolator(new AccelerateDecelerateInterpolator()); set.addAnimation(alphaAnimation); set.addAnimation(scaleAnimation); set.addAnimation(rotateAnimation); set.addAnimation(translateAnimation); set.setDuration(3000);
否则就有可能在转换的过程当中,动画不流畅,出现闪动的现象。 blog