Translate动画
这个动画是最常使用到的,主要就是将控件从一个位置移动到另一个位置,并且还可以在这其中增加一定的效果,下面我们将采用两种方式实现动画,首选的是利用XML来制作动画,其次就是利用代码。
首先我们在Resources中新建一个名为anim的文件夹,然后在该文件夹下新建两个xml,分别命名为in_from_bottom和out_from_bottom,然后我们将下面的代码写入其中:
in_from_bottom:
1 <set xmlns:android="http://schemas.android.com/apk/res/android" 2 android:interpolator="@android:anim/bounce_interpolator"> 3 <translate android:startOffset="500" android:fromYDelta="0" android:toYDelta="80%p" android:duration="1000" /> 4 </set>
out_from_bottom:
1 <set xmlns:android="http://schemas.android.com/apk/res/android" 2 android:interpolator="@android:anim/bounce_interpolator"> 3 <translate android:startOffset="500" android:fromYDelta="80%p" android:toYDelta="0" android:duration="1000"/> 4 </set>
其中set标签表示一个动画集合,该标签下可以包含多个不同的动画,这样就可以将他们组合成一个动画,这里我们不需要过多的了解它,主要是理解translate标签,这个标签代表的就是滑动动画,其中各个属性的说明如下所示:
Interpolator:表示下面的动画的过渡形式,比如逐渐变慢或者逐渐变快。
startOffset:表示动画开始前的延迟(单位毫秒)。
fromYDelta:表示动画开始的位置(其中80%p表示屏幕的80%的高度部分,对应的还有fromXDelta属性)。
toYDelta:表示动画结束的位置(对应的还有toXDelta属性)。
Duration:表示动画持续的时间(单位毫秒)。
介绍完了具体属性,下面就是利用这个动画。首先我们新建一个活动,然后将其视图的xml改成如下所示:
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android" 3 p1:layout_width="match_parent" 4 p1:layout_height="match_parent" 5 p1:id="@+id/relativeLayout1" 6 p1:padding="5dp"> 7 <TextView 8 p1:text="会动的TextView" 9 p1:layout_width="match_parent" 10 p1:layout_height="wrap_content" 11 p1:id="@+id/tvAnim" 12 p1:gravity="center" 13 p1:padding="5dp" 14 p1:background="#00f" 15 p1:textSize="30dp" /> 16 <Button 17 p1:text="消 失" 18 p1:layout_width="wrap_content" 19 p1:layout_height="wrap_content" 20 p1:id="@+id/btnHide" 21 p1:layout_alignParentBottom="true" /> 22 <Button 23 p1:text="出 现" 24 p1:layout_width="wrap_content" 25 p1:layout_height="wrap_content" 26 p1:layout_toRightOf="@id/btnHide" 27 p1:id="@+id/btnShow" 28 p1:layout_alignParentBottom="true" /> 29 </RelativeLayout>