相关博客:
《Android自定义控件三部曲文章索引》
:http://blog.csdn.net/harvic880925/article/details/50995268
上篇给大家讲了有关AnimatorSet的代码实现方法,这篇我们就分别来看看如何利用xml来实现ValueAnimator、ObjectAnimator和AnimatorSet;
在文章最后,将利用AnimatorSet来实现一个路径动画,效果图如下:
(这里实现的是一个动画菜单,在点击菜单按钮时,弹出各个菜单)
一、联合动画的XML实现
在xml中对应animator总共有三个标签,分别是
1、animator
(1)、animator所有字段及意义
下面是完整的animator所有的字段及取值范围:
<code class="language-java hljs has-numbering" style="display: block; padding: 0px; color: inherit; Box-sizing: border-Box; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><animator android:duration=<span class="hljs-string" style="color: rgb(0,136,0); Box-sizing: border-Box;">"int"</span> android:valueFrom=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"float | int | color"</span> android:valueTo=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"float | int | color"</span> android:startOffset=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"int"</span> android:repeatCount=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"int"</span> android:repeatMode=[<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"repeat"</span> | <span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"reverse"</span>] android:valueType=[<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"intType"</span> | <span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"floatType"</span>] android:interpolator=[<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@android:interpolator/XXX"</span>]/></code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li></ul>
- android:duration:每次动画播放的时长
- android:valueFrom:初始动化值;取值范围为float,int和color,如果取值为float对应的值样式应该为89.0,取值为Int时,对应的值样式为:89;当取值为clolor时,对应的值样式为 #333333;
- android:valueTo:动画结束值;取值范围同样是float,int和color这三种类型的值;
- android:startOffset:动画激活延时;对应代码中的startDelay(long delay)函数;
- android:repeatCount:动画重复次数
- android:repeatMode:动画重复模式,取值为repeat和reverse;repeat表示正序重播,reverse表示倒序重播
- android:valueType:表示参数值类型,取值为intType和floatType;与android:valueFrom、android:valueTo相对应。如果这里的取值为intType,那么android:valueFrom、android:valueTo的值也就要对应的是int类型的数值。如果这里的数值是floatType,那么android:valueFrom、android:valueTo的值也要对应的设置为float类型的值。非常注意的是,如果android:valueFrom、android:valueTo的值设置为color类型的值,那么不需要设置这个参数;
- android:interpolator:设置加速器;有关系统加速器所对应的xml值对照表如下:
(2)、将xml加载到程序中
在定义了一个xml后,我们需要将其加载到程序中,使用的方法如下:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(MyActivity.<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">this</span>,R.animator.animator); valueAnimator.start();</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li></ul>
通过loadAnimator将animator动画的xml文件,加载进来,根据类型进行强转。
(3)、简单示例
下面我们就举个例子来看看如何来使用xml生成对应的animator动画
先看看整体效果图:
在效果图中可以看到,我们生成了一个动画,动态了改变了当前控件的坐标位置。
我们先在res/animator文件夹下生成一个动画的xml文件:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-pi" style="color: rgb(0,102); Box-sizing: border-Box;"><?xml version="1.0" encoding="utf-8"?></span> <span class="hljs-tag" style="color: rgb(0,136);">animator</span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,102);">xmlns:android</span>=<span class="hljs-value" style="Box-sizing: border-Box; color: rgb(0,0);">"http://schemas.android.com/apk/res/android"</span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,102);">android:valueFrom</span>=<span class="hljs-value" style="Box-sizing: border-Box; color: rgb(0,0);">"0"</span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,102);">android:valueTo</span>=<span class="hljs-value" style="Box-sizing: border-Box; color: rgb(0,0);">"300"</span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,102);">android:duration</span>=<span class="hljs-value" style="Box-sizing: border-Box; color: rgb(0,0);">"1000"</span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,102);">android:valueType</span>=<span class="hljs-value" style="Box-sizing: border-Box; color: rgb(0,0);">"intType"</span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,102);">android:interpolator</span>=<span class="hljs-value" style="Box-sizing: border-Box; color: rgb(0,0);">"@android:anim/bounce_interpolator"</span>/></span></code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li></ul>
在这里,我们将valueType设置为intType,所以对应的Android:valueFrom、android:valueTo都必须是int类型的值;插值器使用bounce回弹插值器
然后看看加载到程序中过程:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',R.animator.animator); valueAnimator.addUpdateListener(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">new</span> ValueAnimator.AnimatorUpdateListener() { <span class="hljs-annotation" style="color: rgb(155,133,157); Box-sizing: border-Box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">public</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">void</span> <span class="hljs-title" style="Box-sizing: border-Box;">onAnimationUpdate</span>(ValueAnimator animation) { <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">int</span> offset = (<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">int</span>)animation.getAnimatedValue(); mTv1.layout( offset,offset,mTv1.getWidth()+offset,mTv1.getHeight() + offset); } }); valueAnimator.start();</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li></ul>
由于我们xml中根属性是<animator/>
所以它对应的是ValueAnimator,所以在加载后,将其强转为valueAnimator;然后对其添加控件监听。在监听时,动态改变当前textview的位置。有关这些代码就不再细讲了,如果看到前面的文章,这段代码应该是无比熟悉的。
最后的效果就是开头时所演示的效果。
源码在文章底部给出
有关android:valueFrom、android:valueTo取值为color属性时的用法,这里就不讲了,因为在xml中使用color属性,我也不会用;尝试了下,不成功,也不想尝试了,没什么太大意义,下面我们会讲如何在objectanimator中使用color属性;
2、objectAnimator
(1)字段意义及使用方法
同样,我们先来看看它的所有标签的意义:
意义:
-android:propertyName:对应属性名,即ObjectAnimator所需要操作的属性名。
其它字段的意义与animator的意义与取值是一样的,下面再重新列举一下。
-android:duration:每次动画播放的时长
-android:valueFrom:初始动化值;取值范围为float,int和color;
-android:valueTo:动画结束值;取值范围同样是float,int和color这三种类型的值;
-android:startOffset:动画激活延时;对应代码中的startDelay(long delay)函数;
-android:repeatCount:动画重复次数
-android:repeatMode:动画重复模式,取值为repeat和reverse;repeat表示正序重播,reverse表示倒序重播
-android:valueType:表示参数值类型,取值为intType和floatType;与android:valueFrom、android:valueTo相对应。如果这里的取值为intType,那么android:valueFrom、android:valueTo的值也就要对应的是int类型的数值。如果这里的数值是floatType,那么android:valueFrom、android:valueTo的值也要对应的设置为float类型的值。非常注意的是,如果android:valueFrom、android:valueTo的值设置为color类型的值,那么不需要设置这个参数;
-android:interpolator:设置加速器;下面我们就看看如何使用:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(MyActivity.<span class="hljs-keyword" style="color: rgb(0,R.animator.object_animator); animator.setTarget(mTv1); animator.start();</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li></ul>同样是使用loadAnimator加载对应的xml动画。然后使用animator.setTarget(mTv1);绑定上动画目标。因为在xml中,没有设置目标的参数,所以我们必须通过代码将目标控件与动画绑定。
(2)、使用示例
我们先写一个动画的xml:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><?xml version=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"1.0"</span> encoding=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"utf-8"</span>?> <objectAnimator xmlns:android=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"http://schemas.android.com/apk/res/android"</span> android:propertyName=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"TranslationY"</span> android:duration=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"2000"</span> android:valueFrom=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"0.0"</span> android:valueTo=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"400.0"</span> android:interpolator=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@android:anim/accelerate_interpolator"</span> android:valueType=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"floatType"</span> android:repeatCount=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"1"</span> android:repeatMode=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"reverse"</span> android:startOffset=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"2000"</span>/></code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li></ul>在这个xml中,我们定义了更改属性为TranslationY,即改变纵坐标;时长为2000毫秒。从0变到400;使用的插值器是加速插值器,对应的值类型为float类型。
有些同学可能会问,为什么是float类型,因为setTranslationY函数的参数是float类型的,声明如下:Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">void</span> <span class="hljs-title" style="Box-sizing: border-Box;">setTranslationY</span>(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">float</span> translationY)</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li></ul>最后是设置重复次数和重复模式。将动画激活延时设置为2000毫秒;
然后是加载动画:效果图如下:
在点击后,延时2000毫秒后,开始运行。逆序重复运行一次。
源码在文章底部给出(3)、使用color属性示例
这里我们就演示一下如何使用android:valueFrom、android:valueTo的color属性用法,
我们建立一个objectAnimator的动画文件:4035" class="prettyprint" name="code" style="white-space: nowrap; word-wrap: break-word; Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0); Box-sizing: border-Box;">"BackgroundColor"</span> android:duration=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"5000"</span> android:valueFrom=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"#ffff00ff"</span> android:valueTo=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"#ffffff00"</span>/></code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li></ul>设置属性名为BackgroundColor,即对应的set函数为setBackgroundColor(int color);
android:valueFrom和android:valueTo的取值都为颜色值,即#开头的八位数值;即ARGB值;
使用方法不变:效果图如下:
从效果图中可以看到,虽然实现了颜色变化,但会一直闪;所以直接利用xml实现的动画效果并不怎么好,所以如果想要实现颜色变化,还是利用代码来实现吧。前面的文章中,我们已经讲过如何利用ValueAnimator和ObjectAnimator来实现颜色过渡和原理了。大家可以翻看下。
源码在文章底部给出3、set
这个是AnimatorSet所对应的标签。它只有一个属性:
android:ordering:表示动画开始顺序。together表示同时开始动画,sequentially表示逐个开始动画;-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0); Box-sizing: border-Box;">"utf-8"</span>?> <set xmlns:android=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"http://schemas.android.com/apk/res/android"</span> android:ordering=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"together"</span>> <objectAnimator android:propertyName=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"x"</span> android:duration=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"500"</span> android:valueFrom=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"0"</span> android:valueTo=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"400"</span> android:valueType=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"floatType"</span>/> <objectAnimator android:propertyName=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"y"</span> android:duration=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"300"</span> android:valueType=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"floatType"</span>/> </set></code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li></ul>
加载方式为:同样是通过loadAnimator加载动画,然后将其强转为AnimatorSet;(2)、示例
在res/animator文件夹下新建一个文件(set_animator.xml):
Box这里有两个objectAnimator动画,一个改变值x坐标,一个改变值y坐标;取值分别为0-400和0-300;
然后在代码中加载:动画效果如下:
源码在文章底部给出4、总结
最后总结一下,所有animator标签及取值范围如下:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',102);">sequentially</span>"]></span> <span class="hljs-tag" style="color: rgb(0,136);">objectAnimator </span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,0);">"string"</span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,0);">"int"</span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,0);">"float | int | color"</span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,102);">reverse</span>"] <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,102);">floatType</span>"]/></span> <span class="hljs-tag" style="color: rgb(0,136);">animator </span> <span class="hljs-attribute" style="Box-sizing: border-Box; color: rgb(102,136);">set</span>></span> ... <span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;"></<span class="hljs-title" style="Box-sizing: border-Box; color: rgb(0,136);">set</span>></span> <span class="hljs-tag" style="color: rgb(0,136);">set</span>></span></code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li><li style="Box-sizing: border-Box; padding: 0px 5px;">19</li><li style="Box-sizing: border-Box; padding: 0px 5px;">20</li><li style="Box-sizing: border-Box; padding: 0px 5px;">21</li><li style="Box-sizing: border-Box; padding: 0px 5px;">22</li><li style="Box-sizing: border-Box; padding: 0px 5px;">23</li><li style="Box-sizing: border-Box; padding: 0px 5px;">24</li><li style="Box-sizing: border-Box; padding: 0px 5px;">25</li><li style="Box-sizing: border-Box; padding: 0px 5px;">26</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li><li style="Box-sizing: border-Box; padding: 0px 5px;">19</li><li style="Box-sizing: border-Box; padding: 0px 5px;">20</li><li style="Box-sizing: border-Box; padding: 0px 5px;">21</li><li style="Box-sizing: border-Box; padding: 0px 5px;">22</li><li style="Box-sizing: border-Box; padding: 0px 5px;">23</li><li style="Box-sizing: border-Box; padding: 0px 5px;">24</li><li style="Box-sizing: border-Box; padding: 0px 5px;">25</li><li style="Box-sizing: border-Box; padding: 0px 5px;">26</li></ul>各字段的取值意义在上面讲解时已经给出,大家可以翻回去看看。
二、开篇示例——AnimatorSet应用
在讲完了XML使用方法之后,AnimatorSet的部分就完全结束了,下面我们就利用学到的知识来看一下开篇时的那个效果是如何实现的吧。
我们先来分析下这个效果,在用户点击按钮时,把菜单弹出来;弹出来的时候,动画一点从小变到大,一边透明度从0变到1.关键问题是,怎么样实现各个菜单以当前点击按钮为圆心排列在圆形上;1、原理
在开始写代码之前,我们先讲讲,如何根据圆半径来定位每个图片的位置,先看下图:
在上面的图中,我们可以清晰的看到,假如当前菜单与Y轴的夹角是a度,那么这个菜单所移动的X轴距离为radius * sin(a);Y轴的移动距离为radius * cos(a);
这是非常简单的三角函数的计算。想必这块大家理解起来是没有问题的。
那么第一个问题来了,这个夹角a是多少度呢?
很显然,这里所有的菜单的夹角之和是90度。我们总共有五个菜单项,把90度夹角做了4等分。所以夹角a的度数为90/4 = 22;所以这五个菜单,第一个菜单的夹角是0度,第二个菜单的夹角是22度,第三个菜单的夹角是22*2度,第四个夹角是22*3度,第五个的夹角是22*4度.
我们假设index表示当前菜单的位置索引,从0开始,即第一个菜单的索引是0,第二个菜单的索引是1,第三个菜单的索引是2……,而当前的菜单与y轴的夹角恰好占了22度的index份;所以当前菜单与Y轴的夹角为22 * index;这个公式非常重要,大家在这里一定要理解,下面代码中会用到。
第二个问题来了,如何求对应角度的sin,cos值
想必很多同学都知道,Java中有一个Math类,它其中有四个函数:Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-javadoc" style="color: rgb(136,0); Box-sizing: border-Box;">/** * 求对应弧度的正弦值 */</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">double</span> sin(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">double</span> d) <span class="hljs-javadoc" style="color: rgb(136,0); Box-sizing: border-Box;">/** * 求对应弧度的余弦值 */</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">double</span> cos(<span class="hljs-keyword" style="color: rgb(0,0); Box-sizing: border-Box;">/** * 求对应弧度的正切值 */</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">double</span> tan(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">double</span> d)</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li></ul>这里要非常注意的是,这三个函数的输入参数不是度数,而是对应的度数的弧度值!
角度与其对应的弧度值对应关系如下:
在Math中有两种方法可以得到弧度值:
第一种方法:在Math中,Math.PI不仅代表圆周率π,也代表180度角所对应的弧度值。所以Math.sin(Math.PI)就表示180度的正弦值,Math.sin(Math.PI/2)就表示90度的正弦值。
第二种方法:根据度数获得弧度值
在Math中也提供了一个方法这个函数就是Math中根据度数得到弧度值的函数,参数angdeg指度数,返回值是对应的弧度值。
所以比如我们要求22度对应的弧度值就是Math.toRadians(22);所以如果我们要求22度所对应的正弦值就是Math.sin(Math.toRadians(22))
在讲了如何根据半径求得每个菜单项的位置之后,我们来看看示例工程的代码。2、布局代码(main.xml)
布局代码很简单,就是利用FrameLayout将所有的菜单都盖在按钮的下面,效果图如下:
对应代码为:Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',0); Box-sizing: border-Box;">"utf-8"</span>?> <FrameLayout xmlns:android=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"http://schemas.android.com/apk/res/android"</span> android:layout_width=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"match_parent"</span> android:layout_height=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"match_parent"</span> android:layout_marginBottom=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"10dp"</span> android:layout_marginRight=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"10dp"</span>> <Button android:id=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@+id/menu"</span> style=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@style/MenuStyle"</span> android:background=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@drawable/menu"</span>/> <Button android:id=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@+id/item1"</span> style=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@style/MenuItemStyle"</span> android:background=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@drawable/circle1"</span> android:visibility=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"gone"</span>/> <Button android:id=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@+id/item2"</span> style=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@drawable/circle2"</span> android:visibility=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@+id/item3"</span> style=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@drawable/circle3"</span> android:visibility=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@+id/item4"</span> style=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@drawable/circle4"</span> android:visibility=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@+id/item5"</span> style=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"@drawable/circle5"</span> android:visibility=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"gone"</span>/> </FrameLayout></code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li><li style="Box-sizing: border-Box; padding: 0px 5px;">19</li><li style="Box-sizing: border-Box; padding: 0px 5px;">20</li><li style="Box-sizing: border-Box; padding: 0px 5px;">21</li><li style="Box-sizing: border-Box; padding: 0px 5px;">22</li><li style="Box-sizing: border-Box; padding: 0px 5px;">23</li><li style="Box-sizing: border-Box; padding: 0px 5px;">24</li><li style="Box-sizing: border-Box; padding: 0px 5px;">25</li><li style="Box-sizing: border-Box; padding: 0px 5px;">26</li><li style="Box-sizing: border-Box; padding: 0px 5px;">27</li><li style="Box-sizing: border-Box; padding: 0px 5px;">28</li><li style="Box-sizing: border-Box; padding: 0px 5px;">29</li><li style="Box-sizing: border-Box; padding: 0px 5px;">30</li><li style="Box-sizing: border-Box; padding: 0px 5px;">31</li><li style="Box-sizing: border-Box; padding: 0px 5px;">32</li><li style="Box-sizing: border-Box; padding: 0px 5px;">33</li><li style="Box-sizing: border-Box; padding: 0px 5px;">34</li><li style="Box-sizing: border-Box; padding: 0px 5px;">35</li><li style="Box-sizing: border-Box; padding: 0px 5px;">36</li><li style="Box-sizing: border-Box; padding: 0px 5px;">37</li><li style="Box-sizing: border-Box; padding: 0px 5px;">38</li><li style="Box-sizing: border-Box; padding: 0px 5px;">39</li><li style="Box-sizing: border-Box; padding: 0px 5px;">40</li><li style="Box-sizing: border-Box; padding: 0px 5px;">41</li><li style="Box-sizing: border-Box; padding: 0px 5px;">42</li><li style="Box-sizing: border-Box; padding: 0px 5px;">43</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li><li style="Box-sizing: border-Box; padding: 0px 5px;">19</li><li style="Box-sizing: border-Box; padding: 0px 5px;">20</li><li style="Box-sizing: border-Box; padding: 0px 5px;">21</li><li style="Box-sizing: border-Box; padding: 0px 5px;">22</li><li style="Box-sizing: border-Box; padding: 0px 5px;">23</li><li style="Box-sizing: border-Box; padding: 0px 5px;">24</li><li style="Box-sizing: border-Box; padding: 0px 5px;">25</li><li style="Box-sizing: border-Box; padding: 0px 5px;">26</li><li style="Box-sizing: border-Box; padding: 0px 5px;">27</li><li style="Box-sizing: border-Box; padding: 0px 5px;">28</li><li style="Box-sizing: border-Box; padding: 0px 5px;">29</li><li style="Box-sizing: border-Box; padding: 0px 5px;">30</li><li style="Box-sizing: border-Box; padding: 0px 5px;">31</li><li style="Box-sizing: border-Box; padding: 0px 5px;">32</li><li style="Box-sizing: border-Box; padding: 0px 5px;">33</li><li style="Box-sizing: border-Box; padding: 0px 5px;">34</li><li style="Box-sizing: border-Box; padding: 0px 5px;">35</li><li style="Box-sizing: border-Box; padding: 0px 5px;">36</li><li style="Box-sizing: border-Box; padding: 0px 5px;">37</li><li style="Box-sizing: border-Box; padding: 0px 5px;">38</li><li style="Box-sizing: border-Box; padding: 0px 5px;">39</li><li style="Box-sizing: border-Box; padding: 0px 5px;">40</li><li style="Box-sizing: border-Box; padding: 0px 5px;">41</li><li style="Box-sizing: border-Box; padding: 0px 5px;">42</li><li style="Box-sizing: border-Box; padding: 0px 5px;">43</li></ul>其中的style代码为:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><resources> <style name=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"MenuStyle"</span>> <item name=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"android:layout_width"</span>><span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">50</span>dp</item> <item name=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"android:layout_height"</span>><span class="hljs-number" style="color: rgb(0,0); Box-sizing: border-Box;">"android:layout_gravity"</span>>right|bottom</item> </style> <style name=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"MenuItemStyle"</span>> <item name=<span class="hljs-string" style="color: rgb(0,102); Box-sizing: border-Box;">45</span>dp</item> <item name=<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"android:layout_gravity"</span>>right|bottom</item> </style> </resources></code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li></ul>布局是没什么难度的,下面我们就来看看MyActivity中的处理。
3、MyActivity.java
(1)、先看看框架部分:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',136); Box-sizing: border-Box;">public</span> <span class="hljs-class" style="Box-sizing: border-Box;"><span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">class</span> <span class="hljs-title" style="Box-sizing: border-Box; color: rgb(102,102);">MyActivity</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">extends</span> <span class="hljs-title" style="Box-sizing: border-Box; color: rgb(102,102);">Activity</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">implements</span> <span class="hljs-title" style="Box-sizing: border-Box; color: rgb(102,102);">View</span>.<span class="hljs-title" style="Box-sizing: border-Box; color: rgb(102,102);">OnClickListener</span>{</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">private</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">static</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">final</span> String TAG = <span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"MainActivity"</span>; <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">private</span> Button mMenuButton; <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">private</span> Button mItemButton1; <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">private</span> Button mItemButton2; <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">private</span> Button mItemButton3; <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">private</span> Button mItemButton4; <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">private</span> Button mItemButton5; <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">boolean</span> mIsMenuOpen = <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">false</span>; <span class="hljs-annotation" style="color: rgb(155,157); Box-sizing: border-Box;">@Override</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">void</span> <span class="hljs-title" style="Box-sizing: border-Box;">onCreate</span>(Bundle savedInstanceState) { <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">super</span>.onCreate(savedInstanceState); setContentView(R.layout.main); initView(); } <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">void</span> <span class="hljs-title" style="Box-sizing: border-Box;">initView</span>() { mMenuButton = (Button) findViewById(R.id.menu); mMenuButton.setOnClickListener(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">this</span>); mItemButton1 = (Button) findViewById(R.id.item1); mItemButton1.setOnClickListener(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">this</span>); mItemButton2 = (Button) findViewById(R.id.item2); mItemButton2.setOnClickListener(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">this</span>); mItemButton3 = (Button) findViewById(R.id.item3); mItemButton3.setOnClickListener(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">this</span>); mItemButton4 = (Button) findViewById(R.id.item4); mItemButton4.setOnClickListener(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">this</span>); mItemButton5 = (Button) findViewById(R.id.item5); mItemButton5.setOnClickListener(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">this</span>); } <span class="hljs-annotation" style="color: rgb(155,136); Box-sizing: border-Box;">void</span> <span class="hljs-title" style="Box-sizing: border-Box;">onClick</span>(View v) { <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span> (v == mMenuButton) { <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span> (!mIsMenuOpen) { mIsMenuOpen = <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">true</span>; doAnimateOpen(mItemButton1,<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">0</span>,102); Box-sizing: border-Box;">5</span>,102); Box-sizing: border-Box;">300</span>); doAnimateOpen(mItemButton2,102); Box-sizing: border-Box;">1</span>,102); Box-sizing: border-Box;">300</span>); doAnimateOpen(mItemButton3,102); Box-sizing: border-Box;">2</span>,102); Box-sizing: border-Box;">300</span>); doAnimateOpen(mItemButton4,102); Box-sizing: border-Box;">3</span>,102); Box-sizing: border-Box;">300</span>); doAnimateOpen(mItemButton5,102); Box-sizing: border-Box;">4</span>,102); Box-sizing: border-Box;">300</span>); } <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">else</span> { mIsMenuOpen = <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">false</span>; doAnimateClose(mItemButton1,102); Box-sizing: border-Box;">300</span>); doAnimateClose(mItemButton2,102); Box-sizing: border-Box;">300</span>); doAnimateClose(mItemButton3,102); Box-sizing: border-Box;">300</span>); doAnimateClose(mItemButton4,102); Box-sizing: border-Box;">300</span>); doAnimateClose(mItemButton5,102); Box-sizing: border-Box;">300</span>); } } <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">else</span> { Toast.makeText(<span class="hljs-keyword" style="color: rgb(0,<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"你点击了"</span> + v,Toast.LENGTH_SHORT).show(); } } ……………… } </code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li><li style="Box-sizing: border-Box; padding: 0px 5px;">19</li><li style="Box-sizing: border-Box; padding: 0px 5px;">20</li><li style="Box-sizing: border-Box; padding: 0px 5px;">21</li><li style="Box-sizing: border-Box; padding: 0px 5px;">22</li><li style="Box-sizing: border-Box; padding: 0px 5px;">23</li><li style="Box-sizing: border-Box; padding: 0px 5px;">24</li><li style="Box-sizing: border-Box; padding: 0px 5px;">25</li><li style="Box-sizing: border-Box; padding: 0px 5px;">26</li><li style="Box-sizing: border-Box; padding: 0px 5px;">27</li><li style="Box-sizing: border-Box; padding: 0px 5px;">28</li><li style="Box-sizing: border-Box; padding: 0px 5px;">29</li><li style="Box-sizing: border-Box; padding: 0px 5px;">30</li><li style="Box-sizing: border-Box; padding: 0px 5px;">31</li><li style="Box-sizing: border-Box; padding: 0px 5px;">32</li><li style="Box-sizing: border-Box; padding: 0px 5px;">33</li><li style="Box-sizing: border-Box; padding: 0px 5px;">34</li><li style="Box-sizing: border-Box; padding: 0px 5px;">35</li><li style="Box-sizing: border-Box; padding: 0px 5px;">36</li><li style="Box-sizing: border-Box; padding: 0px 5px;">37</li><li style="Box-sizing: border-Box; padding: 0px 5px;">38</li><li style="Box-sizing: border-Box; padding: 0px 5px;">39</li><li style="Box-sizing: border-Box; padding: 0px 5px;">40</li><li style="Box-sizing: border-Box; padding: 0px 5px;">41</li><li style="Box-sizing: border-Box; padding: 0px 5px;">42</li><li style="Box-sizing: border-Box; padding: 0px 5px;">43</li><li style="Box-sizing: border-Box; padding: 0px 5px;">44</li><li style="Box-sizing: border-Box; padding: 0px 5px;">45</li><li style="Box-sizing: border-Box; padding: 0px 5px;">46</li><li style="Box-sizing: border-Box; padding: 0px 5px;">47</li><li style="Box-sizing: border-Box; padding: 0px 5px;">48</li><li style="Box-sizing: border-Box; padding: 0px 5px;">49</li><li style="Box-sizing: border-Box; padding: 0px 5px;">50</li><li style="Box-sizing: border-Box; padding: 0px 5px;">51</li><li style="Box-sizing: border-Box; padding: 0px 5px;">52</li><li style="Box-sizing: border-Box; padding: 0px 5px;">53</li><li style="Box-sizing: border-Box; padding: 0px 5px;">54</li><li style="Box-sizing: border-Box; padding: 0px 5px;">55</li><li style="Box-sizing: border-Box; padding: 0px 5px;">56</li><li style="Box-sizing: border-Box; padding: 0px 5px;">57</li><li style="Box-sizing: border-Box; padding: 0px 5px;">58</li><li style="Box-sizing: border-Box; padding: 0px 5px;">59</li><li style="Box-sizing: border-Box; padding: 0px 5px;">60</li><li style="Box-sizing: border-Box; padding: 0px 5px;">61</li><li style="Box-sizing: border-Box; padding: 0px 5px;">62</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li><li style="Box-sizing: border-Box; padding: 0px 5px;">19</li><li style="Box-sizing: border-Box; padding: 0px 5px;">20</li><li style="Box-sizing: border-Box; padding: 0px 5px;">21</li><li style="Box-sizing: border-Box; padding: 0px 5px;">22</li><li style="Box-sizing: border-Box; padding: 0px 5px;">23</li><li style="Box-sizing: border-Box; padding: 0px 5px;">24</li><li style="Box-sizing: border-Box; padding: 0px 5px;">25</li><li style="Box-sizing: border-Box; padding: 0px 5px;">26</li><li style="Box-sizing: border-Box; padding: 0px 5px;">27</li><li style="Box-sizing: border-Box; padding: 0px 5px;">28</li><li style="Box-sizing: border-Box; padding: 0px 5px;">29</li><li style="Box-sizing: border-Box; padding: 0px 5px;">30</li><li style="Box-sizing: border-Box; padding: 0px 5px;">31</li><li style="Box-sizing: border-Box; padding: 0px 5px;">32</li><li style="Box-sizing: border-Box; padding: 0px 5px;">33</li><li style="Box-sizing: border-Box; padding: 0px 5px;">34</li><li style="Box-sizing: border-Box; padding: 0px 5px;">35</li><li style="Box-sizing: border-Box; padding: 0px 5px;">36</li><li style="Box-sizing: border-Box; padding: 0px 5px;">37</li><li style="Box-sizing: border-Box; padding: 0px 5px;">38</li><li style="Box-sizing: border-Box; padding: 0px 5px;">39</li><li style="Box-sizing: border-Box; padding: 0px 5px;">40</li><li style="Box-sizing: border-Box; padding: 0px 5px;">41</li><li style="Box-sizing: border-Box; padding: 0px 5px;">42</li><li style="Box-sizing: border-Box; padding: 0px 5px;">43</li><li style="Box-sizing: border-Box; padding: 0px 5px;">44</li><li style="Box-sizing: border-Box; padding: 0px 5px;">45</li><li style="Box-sizing: border-Box; padding: 0px 5px;">46</li><li style="Box-sizing: border-Box; padding: 0px 5px;">47</li><li style="Box-sizing: border-Box; padding: 0px 5px;">48</li><li style="Box-sizing: border-Box; padding: 0px 5px;">49</li><li style="Box-sizing: border-Box; padding: 0px 5px;">50</li><li style="Box-sizing: border-Box; padding: 0px 5px;">51</li><li style="Box-sizing: border-Box; padding: 0px 5px;">52</li><li style="Box-sizing: border-Box; padding: 0px 5px;">53</li><li style="Box-sizing: border-Box; padding: 0px 5px;">54</li><li style="Box-sizing: border-Box; padding: 0px 5px;">55</li><li style="Box-sizing: border-Box; padding: 0px 5px;">56</li><li style="Box-sizing: border-Box; padding: 0px 5px;">57</li><li style="Box-sizing: border-Box; padding: 0px 5px;">58</li><li style="Box-sizing: border-Box; padding: 0px 5px;">59</li><li style="Box-sizing: border-Box; padding: 0px 5px;">60</li><li style="Box-sizing: border-Box; padding: 0px 5px;">61</li><li style="Box-sizing: border-Box; padding: 0px 5px;">62</li></ul>这部分代码很简单,就是利用findviewById来找到每个菜单的实例,然后对他们添加点击响应:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',136); Box-sizing: border-Box;">void</span> <span class="hljs-title" style="Box-sizing: border-Box;">onClick</span>(View v) { <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span> (v == mMenuButton) { <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span> (!mIsMenuOpen) { mIsMenuOpen = <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">true</span>; doAnimateOpen(mItemButton1,102); Box-sizing: border-Box;">300</span>); ………… } <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">else</span> { mIsMenuOpen = <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">false</span>; doAnimateClose(mItemButton1,102); Box-sizing: border-Box;">300</span>); ………… } } <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">else</span> { Toast.makeText(<span class="hljs-keyword" style="color: rgb(0,Toast.LENGTH_SHORT).show(); } }</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li></ul>其中弹出主菜单的按钮是mMenuButton,当点击mMenuButton时,利用mIsMenuOpen来标识当前是否已经弹出菜单;如果没有弹出,则利用
doAnimateOpen(mItemButton1,5,300)
将mItemButton1弹出来;其它按钮类似。如果已经弹出来,则利用doAnimateClose(mItemButton1,300);
将mItemButton1收回。
下面我们就分别来看看doAnimateOpen()和doAnimateClose()的代码;(2)、doAnimateOpen()——弹出菜单
先贴出完整代码:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',136); Box-sizing: border-Box;">void</span> <span class="hljs-title" style="Box-sizing: border-Box;">doAnimateOpen</span>(View view,<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">int</span> index,136); Box-sizing: border-Box;">int</span> total,136); Box-sizing: border-Box;">int</span> radius) { <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span> (view.getVisibility() != View.VISIBLE) { view.setVisibility(View.VISIBLE); } <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">double</span> degree = Math.toRadians(<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">90</span>)/(total - <span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">1</span>) * index; <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">int</span> translationX = -(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">int</span>) (radius * Math.sin(degree)); <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">int</span> translationY = -(<span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">int</span>) (radius * Math.cos(degree)); AnimatorSet set = <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">new</span> AnimatorSet(); <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//包含平移、缩放和透明度动画</span> set.playTogether( ObjectAnimator.ofFloat(view,0); Box-sizing: border-Box;">"translationX"</span>,translationX),ObjectAnimator.ofFloat(view,0); Box-sizing: border-Box;">"translationY"</span>,translationY),0); Box-sizing: border-Box;">"scaleX"</span>,102); Box-sizing: border-Box;">0</span>f,102); Box-sizing: border-Box;">1</span>f),0); Box-sizing: border-Box;">"scaleY"</span>,0); Box-sizing: border-Box;">"alpha"</span>,102); Box-sizing: border-Box;">1</span>)); <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//动画周期为500ms</span> set.setDuration(<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">1</span> * <span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">500</span>).start(); }</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li><li style="Box-sizing: border-Box; padding: 0px 5px;">19</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li><li style="Box-sizing: border-Box; padding: 0px 5px;">19</li></ul>我们倒过来看,先看动画部分:
这里构造的动画是利用translationX和translationY将控件移动到指定位置。同时,scaleX、scaleY、alpha都从0变到1;最关键的部分是如何得到translationX和translationY的值。
在这部分的开篇,我们首先讲了,如何讲了我们来看看在代码中如何去做的:首先,是求得两个菜单的夹角,即公式里的a值。Math.toRadians(90)/(total - 1)
表示90度被分成了total-1份,其中每一份的弧度值;
我们前面讲过,假设每一份的弧度值是22度,那么当前菜单与Y轴的夹角就是22 * index度。这里类似,当前菜单与y轴的弧度值就是Math.toRadians(90)/(total - 1) * index
在求得夹角以后,直接利用translationX = radius * sin(a)就可以得到x轴的移动距离,但又因为菜单是向左移动了translationX距离。所以根据坐标系向下为正,向右为正的原则。这里的移动距离translationX应该是负值。我们需要的translationY,因为是向上移动,所以也是负值:在理解了弹出的部分之后,收回的代码就好理解了(3)、doAnimateClose()——收回菜单
收回菜单就是把弹出菜单的动画反过来,让它从translateX,translateY的位置上回到0点,scaleX、scaleY、alpha的值从1变到0即可:
这段代码是很容易理解的,但我在这里求degree的时候,换了一种方法:其实这句代码与上面的是同一个意思。
还记得,我们在讲原理的时候,提到过Math.PI不仅表示圆周率,也表示180度所对应的弧度。
所以Math.toRadians(90)就等于Math.PI/2,这样,这两个公式就是完全一样的了。
源码在文章底部给出
好了,到这里有关AnimatorSet的部分就讲完了,下篇给大家讲讲有关viewGroup动画相关的知识。源码内容:
1、BlogXMLAnimator:第一部分:联合动画的XML实现对应源码
2、BlogAnimatorSetDemo:第二部分:开篇示例——AnimatorSet应用对应源码原文链接:https://www.f2er.com/xml/295242.html如果本文有帮到你,记得加关注哦
源码下载地址:
csdn:http://download.csdn.net/detail/harvic880925/9448719
github:
请大家尊重原创者版权,转载请标明出处:http://www.jb51.cc/article/p-uqfhltxl-ban.html谢谢