上一节介绍了AlphaAnimation动画的用法,本节将介绍另一种形式的动画-TranslateAnimation(位移动画)。
public class
TranslateAnimation
extends Animation
java.lang.Object
↳ android.view.animation.Animation
↳ android.view.animation.TranslateAnimation
位移动画也是Animation类的子类,常用构造方法如下:
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)。有四个参数:
fromXDelta:起始X坐标
toXDelta: 结束X坐标
fromYDelta:起始Y坐标
toYDelta: 结束Y坐标
同样可以通过两种方式实现位移动画,通过实例进行演示。
代码方式实现
1.主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="demo.androidwar.com.translatedemo.MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"