Android基础之补间动画入门

版权声明:本文为博主原创文章,未经博主允许不得转载。

# 动画的分类:
1.帧动画:将多张图片进行叠加,在不同的时间点播放不同的图片.[浪费资源 非常麻烦]
2.补间动画:只要指定时间的起点图片 还有时间结束点的图片 中间的过程由系统来实现.
Translate 平移
Scale 缩放
Alpha 透明度
Rotate 旋转
动画集 AnimationSet
下面是activity_main.xml文件
<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" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="translate"
            android:text="平移" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="scale"
            android:text="缩放" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="alpha"
            android:text="透明度" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="rotate"
            android:text="旋转" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="set"
            android:text="动画集" />
    </LinearLayout>

    <!-- 就是动画的执行对象 -->
    <ImageView
        android:id="@+id/control_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/p1" />

</LinearLayout>
下面是具体的实现
public class MainActivity extends Activity {

	private ImageView mIv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mIv = (ImageView) findViewById(R.id.control_iv);
	}

	/**
	 * 平移动画
	 */
	public void translate(View v) {
		// Animation 补间动画的父类
		// 补间动画 起点(x,y) 终点(x,y)
		// 每个值前面都有一个type:
		// RELATIVE_TO_SELF 相对于自己 如果是x轴移动 就是自己的宽度的倍数 如果是y 则是自己高度的倍数
		// RELATIVE_TO_PARENT 如果是x轴移动 就是父容器的宽度的倍数 如果是y 则是父容器高度的倍数
		// ABSOLUTE 移动一个绝对值 单位是px.
		Animation anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
				Animation.ABSOLUTE, 20, Animation.RELATIVE_TO_PARENT, 0.5f,
				Animation.RELATIVE_TO_SELF, 0);
		// 设置动画的时间
		anim.setDuration(3000);
		// 默认就是匀速运动
		// anim.setInterpolator(new AccelerateInterpolator());//加速
		// anim.setInterpolator(new DecelerateInterpolator());//减速
		// anim.setInterpolator(new AccelerateDecelerateInterpolator());//先加后减
		// 设置重复执行动画 执行的次数=n+1; INFINITE 无限循环
		anim.setRepeatCount(Animation.INFINITE);
		// 默认的重复是一直复制着之前的动画,我们可以也使用反转
		anim.setRepeatMode(Animation.REVERSE);

		// 让操作动画的控件停留在最后那个位置 再次播放动画 最后那帧的效果还在
		anim.setFillAfter(true);
		// 开始播放动画
		mIv.startAnimation(anim);
	}

	/**
	 * 缩放动画
	 */
	public void scale(View v) {
		// 1.创建一个动画对象
		// fromX, fromY,缩放前控件倍数
		// toX,toY 缩放后控件倍数
		// 后面4个参数就是缩放中心
		Animation anim = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f, 
				Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		anim.setDuration(2000);
		anim.setFillAfter(true);
		mIv.startAnimation(anim);
	}

	/**
	 * 透明度
	 */
	public void alpha(View v) {
		Animation anim = new AlphaAnimation(1.0f, 0);
		anim.setDuration(2000);
		anim.setFillAfter(true);
		mIv.startAnimation(anim);
	}

	/**
	 * 旋转
	 */
	public void rotate(View v) {
		//fromDegrees, toDegrees 旋转角度 一般没限制
		Animation anim = new RotateAnimation(
				500, 0, 
				Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		anim.setDuration(2000);
		anim.setFillAfter(true);
		mIv.startAnimation(anim);
	}

	/**
	 * 动画集
	 * 	飞机一边转动(越转越快)一边隐藏
	 */
	public void set(View v) {
		Animation rotate = new RotateAnimation(
				0,360, 
				Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		rotate.setInterpolator(new AccelerateInterpolator());
		
		Animation alpha = new AlphaAnimation(1.0f, 0);
		rotate.setInterpolator(new DecelerateInterpolator());
		
		//创建一个动画集 将多个动画合并在一起	
		//shareInterpolator 分享速率器	
		//false代表每个动画按照他自己的轨迹运动
		//true代表以set速率器为准
		AnimationSet set=new AnimationSet(true);
		set.addAnimation(rotate);
		set.addAnimation(alpha);
		
		set.setDuration(4000);
		set.setFillAfter(true);
		mIv.startAnimation(set);
	}

}

还有xml文件的实现
res/anim/my_alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0.5"
    android:toAlpha="0"
    android:duration="3000"
    android:fillAfter="true"  />
res/anim/my_rotate.xml
<rotate  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="3000"
    android:fillAfter="true"  />
res/anim/my_scale.xml
<?xml version="1.0" encoding="utf-8"?>
<scale  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="2.0"
    android:fromYScale="2.0"
    android:toXScale="1.0"
    android:toYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="3000"
    android:fillAfter="true"  />
<!-- fromXScale,fromYScale,toXScale,toYScale 一般使用float类型 -->
<!-- (pivotX,pivotY)中心坐标一般使用百分比 -->

res/anim/my_translate.xml
<?xml version="1.0" encoding="utf-8"?>
<translate  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="50%p"
    android:toYDelta="300%"
    android:duration="3000"
    android:repeatCount="2"
    android:fillAfter="true"  />
<!-- 从左上顶点  慢慢移动   (父容器中间,自己本身的3倍) -->
res/anim/my_set.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 在xml文件中 每个动画都要设置时间  因为set标签的设置无效 -->
    <rotate
        android:duration="3000"
        android:fillAfter="true"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />

    <alpha
        android:duration="3000"
        android:fillAfter="true"
        android:fromAlpha="0.5"
        android:toAlpha="0" />

</set>
然后在代码中使用
public class MainActivity extends Activity {

	private ImageView mIv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mIv = (ImageView) findViewById(R.id.control_iv);
	}

	/**
	 * 平移动画
	 */
	public void translate(View v) {
		// 加载动画
		Animation anim = AnimationUtils
				.loadAnimation(this, R.anim.my_translate);
		// 开始播放动画
		mIv.startAnimation(anim);
	}

	/**
	 * 缩放动画
	 */
	public void scale(View v) {
		// 加载动画
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_scale);
		// 开始播放动画
		mIv.startAnimation(anim);
	}

	/**
	 * 透明度
	 */
	public void alpha(View v) {
		// 加载动画
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_alpha);
		// 开始播放动画
		mIv.startAnimation(anim);
	}

	/**
	 * 旋转
	 */
	public void rotate(View v) {
		// 加载动画
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate);
		// 开始播放动画
		mIv.startAnimation(anim);
	}

	/**
	 * 动画集 飞机一边转动(越转越快)一边隐藏
	 */
	public void set(View v) {
		// 加载动画
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_set);
		// 开始播放动画
		mIv.startAnimation(anim);
	}

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值