View动画的XML与Java代码实现的两种方式,交叉着用

AnimationSet 动画集合
Android 提供了AnimationSet 动画集合用于将多种补间动画联合起来一起使用,这样就能实现更多复杂的动画效果。动画集合既可以使用AnimationSet 类来定义也可以在XML 文件中使用<set>节点来定义。
一、 使用XML 文件实现动画集合
在res/anim 目录下创建xml 文件,set_demo.xml。文件清单如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="false" >
    <rotate
     xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="2"
        android:repeatMode="reverse"
        android:toDegrees="360" >
    </rotate>
    <scale
     xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXScale="20%"
        android:fromYScale="20%"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="2"
        android:repeatMode="reverse"
        android:toXScale="200%"
        android:toYScale="200%" >
    </scale>
</set>

在Activity 中添加如下方法,实现业务功能:
	/**
	 * 动画合集 集合
	 */
	public void set(View view){
		Animation set = AnimationUtils.loadAnimation(this, R.anim.set_demo);
		iv.startAnimation(set);
	}
     通过上面的代码我们发现,代码逻辑跟实现普通的代码其实是一模一样的,无非就是使用到的动画资源文件不一样罢了。
二、 使用Java 代码实现动画集合
使用Java 代码实现动画集合的核心代码如下所示:
/**
	 * 动画合集 集合
	 */
	public void set(View view){
		AnimationSet set = new AnimationSet(false);
		RotateAnimation ra = new RotateAnimation(0, 360, 
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		//动画播放的时间长度
		ra.setDuration(2000);
								//设置重复播放的次数
		ra.setRepeatCount(Animation.INFINITE);
								//设置重复播放的模式
		ra.setRepeatMode(Animation.REVERSE);
		ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		//动画播放的时间长度
		sa.setDuration(2000);
						//设置重复播放的次数
		sa.setRepeatCount(Animation.INFINITE);
						//设置重复播放的模式
		sa.setRepeatMode(Animation.REVERSE);
						//让iv播放aa动画
		TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
				0, 
				Animation.RELATIVE_TO_SELF,
				1f,
				Animation.RELATIVE_TO_SELF,
				0,
				Animation.RELATIVE_TO_SELF,
				1f);
		//动画播放的时间长度
		ta.setDuration(2000);
				//设置重复播放的次数
		ta.setRepeatCount(Animation.INFINITE);
				//设置重复播放的模式
		ta.setRepeatMode(Animation.REVERSE);
		set.addAnimation(ta);
		set.addAnimation(sa);
		set.addAnimation(ra);
		iv.startAnimation(set);
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值