Android中补间动画的实现

下面要说的是补间动画的实现。

补间动画包括五种:

1、淡入淡出
2、旋转
3、平移
4、缩放
5、组合
这些动画效果可以在代码中实现,也可以通过布局文件来实现。
下面是我们在代码中的实现:
package com.example.text02;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private ImageView iv;

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

	// 淡入淡出
	public void dan(View view) {
		// 声明一个AlphaAnimation对象,fromAlpha:起始透明度,toAlpha:结束透明度
		AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0.0f);
		// 设置透明度之间的间隔
		alphaAnimation.setDuration(2000);
		// 设置重复次数
		alphaAnimation.setRepeatCount(3);
		// 设置动画的重复模式
		alphaAnimation.setRepeatMode(Animation.REVERSE);
		// 持久化保存结束时的样子,结束时还原
		alphaAnimation.setFillAfter(true);
		// 启动动画
		// alphaAnimation.start();错误的启动
		iv.startAnimation(alphaAnimation);
	}
	
	//旋转
	public void turn(View view) {
		/**
		 * float fromDegree:起始角度 float toDegree:结束角度 第3,4参数:设置选中的中心点中x轴的大小
		 * 第5,6参数:设置旋转的中心点中y轴的大小 x和y轴的交叉点是旋转的中心点
		 */
		RotateAnimation rotateAnimation = new RotateAnimation(90, 270,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotateAnimation.setDuration(3000);
		rotateAnimation.setRepeatMode(Animation.REVERSE);
		// rotateAnimation.setFillAfter(true);
		iv.startAnimation(rotateAnimation);
	}
	//移动
	public void move(View view) {
		/**
		 * 其中所有的value值表示的是相对于自身长度的百分比:percent,
		 * 移动距离 = 自身长度 * value;
		 */
		TranslateAnimation translateAnimation = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);
		translateAnimation.setDuration(2000);
		translateAnimation.setRepeatMode(Animation.REVERSE);
		translateAnimation.setFillAfter(true);
		iv.startAnimation(translateAnimation);
	}
	//缩放
	public void suofang(View view){
		/**
		 * 第1,2参数:表示x轴的起始和结束值(给出的值都是倍数关系,如果1则表示原来的大小,如果2.0f则表示原来大小的两倍,如果0.5f则缩小了一倍)
		 * 同理第3,4参数,表示y轴的起始和结束的值
		 * 第5,6参数:第5个参数就是圆心的参照类型,如果类型是相对类型则第六个参数就是0.5f倍,x轴的倍数的大小
		 * 同理第7,8参数,第七个参数就是圆心的参照类型  第八个参数确定的是y轴倍数的大小
		 */
		ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		scaleAnimation.setDuration(2000);
		scaleAnimation.setFillAfter(true);
		iv.startAnimation(scaleAnimation);
	}
	
	//组合
	public void zuhe(View view){
		//旋转加缩放
		RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
		
		//参数表示:如果为true表示动画的集合中每种动画与集合采用相同的加速度
		//false:则表示每一种动画都可以使用自己的加速器效果
		AnimationSet set = new AnimationSet(true);
		set.addAnimation(rotateAnimation);
		set.addAnimation(scaleAnimation);
		//设置集合的持续时间
		set.setDuration(2000);
		set.setRepeatMode(Animation.RESTART);
		set.setFillAfter(true);
		iv.setAnimation(set);
	}

}

运行结果:

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值