假如,我们想要一个控件在变大的同时向左移动,我们可以通过以下几个步骤来实现:
1、在res文件夹下新建一个anim文件夹(存放动画xml文件的文件夹一般都这么命名);
2、创建两个xml文件,假设命名为分别命名为scale_big.xml、trans_left.xml
scale_big.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false"
android:fillAfter="true">
<scale
android:duration="150"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.1"
android:toYScale="1.1"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
/>
<!--
fromXScale[float] fromYScale[float] 为动画起始时,X、Y坐标上的伸缩尺寸;
0.0表示收缩到没有 ;1.0表示正常无伸缩.
值小于1.0表示收缩. 值大于1.0表示放大
toXScale [float] toYScale[float] 为动画结束时,X、Y坐标上的伸缩尺寸
pivotX[float] pivotY[float] 为动画相对于物件的X、Y坐标的开始位置
属性值说明:从0%-100%中取值,50%为
物件的X或Y方向坐标上的中点位置
-->
</set>
trans_left.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="-10"
android:duration="150"
android:interpolator="@android:anim/decelerate_interpolator"
>
</translate>
3、在java代码中加载这些动画(transLeftAnim同理,略去)
Animation scaleBigAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_big);
animationSet = new AnimationSet(true);
animationSet.addAnimation(scaleBigAnim);
animationSet.addAnimation(transLeftAnim);
5、执行该动画
<span style="white-space:pre"> </span>textView.startAnimation(animationSet);
就这样,我们可以实现在变大的时候并同时左移的动画效果,关键是明白这个原理,就可以实现rotate、scale、translate或者alpha等同时变换的组合动画。