Android 多个animation同时执行

假如,我们想要一个控件在变大的同时向左移动,我们可以通过以下几个步骤来实现:


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);


4、创建AnimationSet

		animationSet = new AnimationSet(true);
		animationSet.addAnimation(scaleBigAnim);
		animationSet.addAnimation(transLeftAnim);

5、执行该动画

<span style="white-space:pre">		</span>textView.startAnimation(animationSet);


就这样,我们可以实现在变大的时候并同时左移的动画效果,关键是明白这个原理,就可以实现rotate、scale、translate或者alpha等同时变换的组合动画。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值