Android 之动画实现animation

</pre>关于动画的实现,Android提供了Animation,在Android SDK介绍了2种Animation模式:<p></p><p><strong><span class="token lf" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token li" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"><span class="token md md-li" style="outline:none!important">1.</span>Tween Animation:通过对场景里的对象不断做图像变换(平移、缩放、旋转)产生动画效果,即是一种渐变动画;</span></strong></p><p><strong><span class="token li" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"><span class="token lf" style="outline:none!important"></span></span><span class="token li" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"><span class="token md md-li" style="outline:none!important">2.</span>Frame Animation:顺序播放事先做好的图像,是一种画面转换动画。</span></strong></p><p><span class="token li" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"><span class="token lf" style="outline:none!important"></span></span><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">动画类型</span><span class="token lf" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">下面先来看看Android提供的动画类型。Android的animation由四种类型组成</span><span class="token lf" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">在XML文件中:</span></p><p><span style="color:#ff0000"><span class="token lf" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">alpha 渐变透明度动画效果</span></span></p><p><span style="color:#ff0000"><span class="token lf" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">scale 渐变尺寸伸缩动画效果</span></span></p><p><span style="color:#ff0000"><span class="token lf" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">translate 画面转换位置移动动画效果</span></span></p><p><span style="color:#ff0000"><span class="token lf" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">rotate 画面转移旋转动画效果</span><span class="token lf" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">在</span></span></p><p><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">Java 源码中定义了相应的类,可以使用这些类的方法来获取和操作相应的属性:</span></p><p><span style="color:#ff0000"><span class="token lf" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">AlphaAnimation渐变透明度动画效果</span></span></p><p><span style="color:#ff0000"><span class="token lf" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">ScaleAnimation渐变尺寸伸缩动画效果</span></span></p><p><span style="color:#ff0000"><span class="token lf" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">TranslateAnimation画面转换位置移动动画效果</span></span></p><p><span style="color:#ff0000"><span class="token lf" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">RotateAnimation画面转移旋转动画效果</span></span></p><p><span class="token lf" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">Tween Animation</span><span class="token lf" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">一个tween动画将对视图对象中的内容进行一系列简单的转换(位置,大小,旋转,透明性)。</span></p><p><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">如果你有一个文本视图对象,你可以移动它,旋转它,让它变大或让它变小,如果文字下面还有背景图像,背景图像也会随着文件进行转换。</span><span class="token lf" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">使用XML来定义Tween Animation</span><span class="token lf" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">动画的XML文件在工程中res/anim目录,这个文件必须包含一个根元素,可以使<span class="token tag" style="color:rgb(52,52,52); font-weight:bold; font-size:0.9em; line-height:normal; font-family:'Source Code Pro',monospace; outline:none!important"><span class="token tag" style="font-size:0.9em; outline:none!important"><span class="token punctuation" style="outline:none!important; font-weight:normal"><</span>alpha</span><span class="token punctuation" style="outline:none!important; font-weight:normal">></span></span><span class="token tag" style="color:rgb(52,52,52); font-weight:bold; font-size:0.9em; line-height:normal; font-family:'Source Code Pro',monospace; outline:none!important"><span class="token tag" style="font-size:0.9em; outline:none!important"><span class="token punctuation" style="outline:none!important; font-weight:normal"><</span>scale</span><span class="token punctuation" style="outline:none!important; font-weight:normal">></span></span><span class="token tag" style="color:rgb(52,52,52); font-weight:bold; font-size:0.9em; line-height:normal; font-family:'Source Code Pro',monospace; outline:none!important"><span class="token tag" style="font-size:0.9em; outline:none!important"><span class="token punctuation" style="outline:none!important; font-weight:normal"><</span>translate</span><span class="token punctuation" style="outline:none!important; font-weight:normal">></span></span><span class="token tag" style="color:rgb(52,52,52); font-weight:bold; font-size:0.9em; line-height:normal; font-family:'Source Code Pro',monospace; outline:none!important"><span class="token tag" style="font-size:0.9em; outline:none!important"><span class="token punctuation" style="outline:none!important; font-weight:normal"><</span>rotate</span><span class="token punctuation" style="outline:none!important; font-weight:normal">></span></span>插值元素或者是把上面的元素都放入<span class="token tag" style="color:rgb(52,52,52); font-weight:bold; font-size:0.9em; line-height:normal; font-family:'Source Code Pro',monospace; outline:none!important"><span class="token tag" style="font-size:0.9em; outline:none!important"><span class="token punctuation" style="outline:none!important; font-weight:normal"><</span>set</span><span class="token punctuation" style="outline:none!important; font-weight:normal">></span></span>元素组中,默认情况下,所以的动画指令都是同时发生的,为了让他们按序列发生,需要设置一个特殊的属性<strong>startOffset</strong>。动画的指令定义了你想要发生什么样的转换,当他们发生了,应该执行多长时间,转换可以是连续的也可以使同时的。例如,你让文本内容从左边移动到右边,然后旋转180度,或者在移动的过程中同时旋转,没个转换需要设置一些特殊的参数(开始和结束的大小尺寸的大小变化,开始和结束的旋转角度等等,也可以设置些基本的参数(例如,开始时间与周期),如果让几个转换同时发生,可以给它们设置相同的开始时间,如果按序列的话,计算开始时间加上其周期。</span><span class="token lf" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">Tween Animation共同的节点属性</span></p><p><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span><table border="1" cellspacing="0" cellpadding="0" style="margin:0px; padding:0px; color:rgb(73,73,73); font-family:Verdana,宋体,sans-serif; font-size:12px; border:1px solid rgb(192,192,192); border-collapse:collapse; width:519px"><tbody style="padding:0px; margin:0px"><tr style="padding:0px; margin:0px"><td valign="top" width="130" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">属性[类型]</td><td valign="top" width="180" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">功能</td><td valign="top" width="259" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">备注</td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="130" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">Duration[long]</td><td valign="top" width="180" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">属性为动画持续时间</td><td valign="top" width="259" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">时间以毫秒为单位</td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="130" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">fillAfter [boolean]</td><td valign="top" width="438" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">当设置为true ,该动画转化在动画结束后被应用</td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="130" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">fillBefore[boolean]</td><td valign="top" width="438" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">当设置为true ,该动画转化在动画开始前被应用</td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="130" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><p align="left" style="margin-top:5px; margin-bottom:5px; padding-top:5px; padding-bottom:5px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:19px; font-family:verdana">interpolator</p></td><td valign="top" width="180" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">指定一个动画的插入器</td><td valign="top" width="259" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">有一些常见的插入器<br style="padding:0px; margin:0px" />accelerate_decelerate_interpolator<br style="padding:0px; margin:0px" />加速-减速 动画插入器<br style="padding:0px; margin:0px" />accelerate_interpolator<br style="padding:0px; margin:0px" />加速-动画插入器<br style="padding:0px; margin:0px" />decelerate_interpolator<br style="padding:0px; margin:0px" />减速- 动画插入器<br style="padding:0px; margin:0px" />其他的属于特定的动画效果</td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="130" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">repeatCount[int]</td><td valign="top" width="180" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">动画的重复次数</td><td valign="top" width="259" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"> </td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="130" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">RepeatMode[int]</td><td valign="top" width="180" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">定义重复的行为</td><td valign="top" width="259" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">1:重新开始 <wbr> 2:plays backward</wbr></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="130" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">startOffset[long]</td><td valign="top" width="438" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">动画之间的时间间隔,从上次动画停多少时间开始执行下个动画</td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="130" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">zAdjustment[int]</td><td valign="top" width="180" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">定义动画的Z Order的改变</td><td valign="top" width="259" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">0:保持Z Order不变<br style="padding:0px; margin:0px" />1:保持在最上层<br style="padding:0px; margin:0px" />-1:保持在最下层</td></tr></tbody></table><table border="1" cellspacing="0" cellpadding="0" style="margin:0px; padding:0px; color:rgb(73,73,73); font-family:Verdana,宋体,sans-serif; font-size:12px; border:1px solid rgb(192,192,192); border-collapse:collapse; width:519px"><tbody style="padding:0px; margin:0px"><tr style="padding:0px; margin:0px"><td valign="top" width="568" colspan="3" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><p align="center" style="margin-top:5px; margin-bottom:5px; padding-top:5px; padding-bottom:5px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:19px; font-family:verdana">表二</p></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="285" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">XML节点</td><td valign="top" width="284" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">功能说明</td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="285" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">alpha</td><td valign="top" width="284" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><strong>渐变透明度动画效果</strong></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="568" colspan="3" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><strong><alpha<br style="padding:0px; margin:0px" />android:fromAlpha=”0.1″<br style="padding:0px; margin:0px" />android:toAlpha=”1.0″<br style="padding:0px; margin:0px" />android:duration=”3000″ /></strong></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="83" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">fromAlpha</td><td valign="top" width="202" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><p align="left" style="margin-top:5px; margin-bottom:5px; padding-top:5px; padding-bottom:5px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:19px; font-family:verdana">属性为动画起始时透明度</p></td><td valign="top" rowspan="2" width="284" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><p style="margin:5px auto; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal">0.0表示完全透明<br style="padding:0px; margin:0px" />1.0表示完全不透明<br style="padding:0px; margin:0px" />以上值取0.0-1.0之间的float数据类型的数字</p><p style="margin:5px auto; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal">duration为动画持续时间,ms单位</p></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="83" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><blockquote style="border:2px solid rgb(239,239,239); color:rgb(51,51,51); margin:0px; padding:4px; font-style:italic; background-color:initial"><p style="margin:5px auto; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal">toAlpha</p></blockquote></td><td valign="top" width="202" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><p style="margin:5px auto; padding-top:0px; padding-bottom:0px; border:0px; list-style:none; word-wrap:normal; word-break:normal">属性为动画结束时透明度</p></td></tr></tbody></table><table border="1" cellspacing="0" cellpadding="0" style="margin:0px; padding:0px; color:rgb(73,73,73); font-family:Verdana,宋体,sans-serif; font-size:12px; border:1px solid rgb(192,192,192); border-collapse:collapse; width:519px"><tbody style="padding:0px; margin:0px"><tr style="padding:0px; margin:0px"><td valign="top" width="568" colspan="4" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><p align="center" style="margin-top:5px; margin-bottom:5px; padding-top:5px; padding-bottom:5px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:19px; font-family:verdana">表三</p></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="285" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">scale</td><td valign="top" width="284" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><strong>渐变尺寸伸缩动画效果</strong></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="568" colspan="4" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><strong><scale<br style="padding:0px; margin:0px" />android:interpolator= “@android:anim/accelerate_decelerate_interpolator”<br style="padding:0px; margin:0px" />android:fromXScale=”0.0″<br style="padding:0px; margin:0px" />android:toXScale=”1.4″<br style="padding:0px; margin:0px" />android:fromYScale=”0.0″<br style="padding:0px; margin:0px" />android:toYScale=”1.4″<br style="padding:0px; margin:0px" />android:pivotX=”50%”<br style="padding:0px; margin:0px" />android:pivotY=”50%”<br style="padding:0px; margin:0px" />android:fillAfter=”false”<br style="padding:0px; margin:0px" />android:startOffset=“700”<br style="padding:0px; margin:0px" />android:duration=”700″<br style="padding:0px; margin:0px" />android:repeatCount=”10″ /></strong></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="121" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">fromXScale[float] fromYScale[float]</td><td valign="top" width="180" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">为动画起始时,X、Y坐标上的伸缩尺寸</td><td valign="top" rowspan="2" width="268" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">0.0表示收缩到没有<br style="padding:0px; margin:0px" />1.0表示正常无伸缩<br style="padding:0px; margin:0px" />值小于1.0表示收缩<br style="padding:0px; margin:0px" />值大于1.0表示放大</td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="121" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">toXScale [float]<br style="padding:0px; margin:0px" />toYScale[float]</td><td valign="top" width="180" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">为动画结束时,X、Y坐标上的伸缩尺寸</td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="121" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">pivotX[float]<br style="padding:0px; margin:0px" />pivotY[float]</td><td valign="top" width="180" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">为动画相对于物件的X、Y坐标的开始位置</td><td valign="top" width="268" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">属性值说明:从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置</td></tr><tr style="padding:0px; margin:0px"><td width="121" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"> </td><td width="164" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"> </td><td width="16" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"> </td><td width="268" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"> </td></tr></tbody></table><table border="1" cellspacing="0" cellpadding="0" style="margin:0px; padding:0px; color:rgb(73,73,73); font-family:Verdana,宋体,sans-serif; font-size:12px; border:1px solid rgb(192,192,192); border-collapse:collapse; width:519px"><tbody style="padding:0px; margin:0px"><tr style="padding:0px; margin:0px"><td valign="top" width="568" colspan="4" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><p align="center" style="margin-top:5px; margin-bottom:5px; padding-top:5px; padding-bottom:5px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:19px; font-family:verdana">表四</p></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="285" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">translate</td><td valign="top" width="284" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><strong>画面转换位置移动动画效果</strong></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="568" colspan="4" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><strong><translate<br style="padding:0px; margin:0px" />android:fromXDelta=”30″<br style="padding:0px; margin:0px" />android:toXDelta=”-80″<br style="padding:0px; margin:0px" />android:fromYDelta=”30″<br style="padding:0px; margin:0px" />android:toYDelta=”300″<br style="padding:0px; margin:0px" />android:duration=”2000″ /></strong></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="83" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">fromXDelta<br style="padding:0px; margin:0px" />toXDelta</td><td valign="top" width="296" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">为动画、结束起始时 X坐标上的位置</td><td valign="top" width="189" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"> </td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="83" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">fromYDelta<br style="padding:0px; margin:0px" />toYDelta</td><td valign="top" width="296" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">为动画、结束起始时 Y坐标上的位置</td><td valign="top" width="189" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"> </td></tr><tr style="padding:0px; margin:0px"><td width="83" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"> </td><td width="202" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"> </td><td width="94" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"> </td><td width="189" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"> </td></tr></tbody></table><table border="1" cellspacing="0" cellpadding="0" style="margin:0px; padding:0px; color:rgb(73,73,73); font-family:Verdana,宋体,sans-serif; font-size:12px; border:1px solid rgb(192,192,192); border-collapse:collapse; width:519px"><tbody style="padding:0px; margin:0px"><tr style="padding:0px; margin:0px"><td valign="top" width="568" colspan="4" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><p align="center" style="margin-top:5px; margin-bottom:5px; padding-top:5px; padding-bottom:5px; border:0px; list-style:none; word-wrap:normal; word-break:normal; line-height:19px; font-family:verdana">表五</p></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="285" colspan="3" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">rotate</td><td valign="top" width="284" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><strong>画面转移旋转动画效果</strong></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="568" colspan="4" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse"><strong><rotate<br style="padding:0px; margin:0px" />android:interpolator=”@android:anim/accelerate_decelerate_interpolator”<br style="padding:0px; margin:0px" />android:fromDegrees=”0″<br style="padding:0px; margin:0px" />android:toDegrees=”+350″<br style="padding:0px; margin:0px" />android:pivotX=”50%”<br style="padding:0px; margin:0px" />android:pivotY=”50%”<br style="padding:0px; margin:0px" />android:duration=”3000″ /></strong></td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="92" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">fromDegrees</td><td valign="top" width="161" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">为动画起始时物件的角度</td><td valign="top" rowspan="2" width="315" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">说明<br style="padding:0px; margin:0px" />当角度为负数——表示逆时针旋转<br style="padding:0px; margin:0px" />当角度为正数——表示顺时针旋转<br style="padding:0px; margin:0px" />(负数from——to正数:顺时针旋转)<br style="padding:0px; margin:0px" />(负数from——to负数:逆时针旋转)<br style="padding:0px; margin:0px" />(正数from——to正数:顺时针旋转)<br style="padding:0px; margin:0px" />(正数from——to负数:逆时针旋转)</td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="92" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">toDegrees</td><td valign="top" width="161" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">属性为动画结束时物件旋转的角度 可以大于360度</td></tr><tr style="padding:0px; margin:0px"><td valign="top" width="92" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">pivotX<br style="padding:0px; margin:0px" />pivotY</td><td valign="top" width="161" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">为动画相对于物件的X、Y坐标的开始位</td><td valign="top" width="315" colspan="2" style="margin:0px; padding:0px; font-family:Verdana,宋体,sans-serif; line-height:18px; border:1px solid rgb(192,192,192); border-collapse:collapse">说明:以上两个属性值 从0%-100%中取值<br style="padding:0px; margin:0px" />50%为物件的X或Y方向坐标上的中点位置</td></tr></tbody></table><span style="color:rgb(73,73,73); font-family:Verdana,宋体,sans-serif; line-height:18px"></span>下面一个完整的动画实例res/anim/alpha.xml</p><p><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"><?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:shareInterpolator="true">  <span style="white-space:pre"> </span><alpha        android:fromAlpha="0.0"        android:toAlpha="1.0"         android:duration="3000"        /> <span style="white-space:pre"></span></set></span></p><p><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important">java代码实例</span></p><p><span class="token p" style="color:rgb(90,90,90); font-family:'microsoft yahei'; font-size:18px; line-height:29.7px; white-space:pre-wrap; outline:none!important"></span></p><pre name="code" class="java">public void anim(View v){
		Animation animation =null;
		switch (v.getId()) {
		case R.id.alpha:  //渐变透明度动画效果
			 animation = AnimationUtils.loadAnimation(this, R.anim.alpha);  //绑定动画
			animation.setFillAfter(true);  //停留在最后的状态
			imageView.startAnimation(animation); //启动动画
			break;
		case R.id.scale:  //渐变尺寸伸缩动画效果
			 animation = AnimationUtils.loadAnimation(this, R.anim.scale);  //绑定动画
			animation.setFillAfter(true);  //停留在最后的状态
			imageView.startAnimation(animation); //启动动画
			break;
		case R.id.translate:  //画面转换位置移动动画效果
			 animation = AnimationUtils.loadAnimation(this, R.anim.translat);  //绑定动画
			animation.setFillAfter(true);  //停留在最后的状态
			imageView.startAnimation(animation); //启动动画
			break;
		case R.id.rotate:   //画面转移旋转动画效果
			animation = AnimationUtils.loadAnimation(this, R.anim.rotate);  //绑定动画
			animation.setFillAfter(true);  //停留在最后的状态
			imageView.startAnimation(animation); //启动动画
			break;
		default:
			break;
		}

interpolator的解释

interpolator定义一个动画的变化率(the rate of change)。这使得基本的动画效果(alpha, scale, translate, rotate)得以加速,减速,重复等。

 

Interpolator 定义了动画的变化速度,可以实现匀速、正加速、负加速、无规则变加速等。Interpolator 是基类,封装了所有 Interpolator 的共同方法,它只有一个方法,即 getInterpolation (float input),该方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:

 

AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator 在动画的以均匀的速率改变

 

Frame Animation

Frame Animation是顺序播放事先做好的图像,跟电影类似。不同于animation package, Android SDK提供了另外一个类AnimationDrawable来定义、使用Frame Animation。

Frame Animation可以在XML Resource定义(还是存放到res/anim文件夹下),也可以使用AnimationDrawable中的API定义。由于Tween Animation与Frame Animation有着很大的不同,因此XML定义的格式也完全不一样,其格式是:首先是animation-list根节点,animation-list根节点中包含多个item子节点,每个item节点定义一帧动画,当前帧的drawable资源和当前帧持续的时间。下面对节点的元素加以说明: 

 

XML属性 说明
drawable 当前帧引用的drawable资源
duration 当前帧显示的时间(毫秒为单位)
oneshot 如果为true,表示动画只播放一次停止在最后一帧上,如果设置为false表示动画循环播放。
variablePadding If true, allows the drawable’s padding to change based on the current state that is selected.
visible 规定drawable的初始可见性,默认为flase;

下面定义xml文件:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/girl_1" android:duration="200" />
    <item android:drawable="@drawable/girl_2" android:duration="200" />
    <item android:drawable="@drawable/girl_3" android:duration="200" />
    <item android:drawable="@drawable/girl_4" android:duration="200" />
    <item android:drawable="@drawable/girl_5" android:duration="200" />
    <item android:drawable="@drawable/girl_6" android:duration="200" />
    <item android:drawable="@drawable/girl_7" android:duration="200" />
    <item android:drawable="@drawable/girl_8" android:duration="200" />
    <item android:drawable="@drawable/girl_9" android:duration="200" />
    <item android:drawable="@drawable/girl_10" android:duration="200" />
    <item android:drawable="@drawable/girl_11" android:duration="200" />
</animation-list>
相关java代码

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bui_anim_frameanimation);
		
		TextView textView = (TextView) this.findViewById(R.id.textView);
        textView.setBackgroundResource(R.drawable.frame);//绑定Frame动画图形
        final AnimationDrawable drawable = (AnimationDrawable) textView.getBackground();
        
        getMainLooper().myQueue().addIdleHandler(new MessageQueue.IdleHandler() {			
			public boolean queueIdle() {
				drawable.start();//启动动画
				return false;
			}
		});

有一点需要强调的是:启动Frame Animation动画的代码rocketAnimation.start();不能在OnCreate()中,因为在OnCreate()中AnimationDrawable还没有完全的与ImageView绑定,在OnCreate()中启动动画,就只能看到第一张图片。这里实在拖曳事件中实现的。这里使用消息队列解决该问题;

下面,阅读Android SDK中对AnimationDrawable的介绍,有个简单的了解:

 

AnimationDrawable

获取、设置动画的属性  
int getDuration() 获取动画的时长
int getNumberOfFrames() 获取动画的帧数
boolean isOneShot()

 

Void setOneShot(boolean oneshot)

获取oneshot属性
设置oneshot属性
void inflate(Resurce r,XmlPullParser p,
AttributeSet attrs)
 
增加、获取帧动画
Drawable getFrame(int index) 获取某帧的Drawable资源
void addFrame(Drawable frame,int duration) 为当前动画增加帧(资源,持续时长)
动画控制
void start() 开始动画
void run() 外界不能直接掉调用,使用start()替代
boolean  isRunning() 当前动画是否在运行
void stop() 停止当前动画

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值