android 动画 ——视图动画(View Animation)

android动画分为视图动画(View Animation)、属性动画(Property Animation)

想看属性动画(Property Animation):请移步至http://blog.csdn.net/u013424496/article/details/51700312

这里我们来说下视图动画(View Animation)的纯代码写法,还有一种是xml调用,

对于xml调用可以去看 http://blog.csdn.net/u013424496/article/details/51144171

相对与属性动画视图动画使用环境:

view animation system提供的能力只能够为View添加动画。因此如果你想为非View对象添加动画,就必须自己去实现,
view animation system在View动画的展现方面也是有约束的,只暴露了View的很少方面。比如View支持缩放和旋转,但不支持背景颜色的动画。
view animation system的另一劣势是,其改变的是View的绘制效果,真正的View的属性保持不变,比如无论你在对话中如何缩放Button的大小,Button的有效点击区域还是没有应用到动画时的区域,其位置与大小都不变。
但是View animation system只需花费很少时间创建而且只需很少的代码。如果View 动画完成了你所有的动作,或者你存在的代码已经达到了你想要的效果,就没必要使用property 动画系统了。

图解类结构

大致分为4种:缩放ScaleAnimation、平移TranslateAnimation、渐变AlphaAnimation、旋转RotateAnimation,(当然非得说还有一个种也能凑出来AnimationSet可以让将前面4个视图动画组合起来应用到某个View上)

下面就来一一简单聊聊这几个动画的纯代码实现方式吧

animation.setFillAfter(true);//让动画结束的是时候保持现状,不会回到动画开始的显示状态

缩放ScaleAnimation

1)
//以View左上角作为缩放中心,水平方向扩大一倍,垂直方向缩小为原来的一半
		float fromXScale = 1.0f;
		float toScaleX = 2.0f;
		float fromYScale = 1.0f;
		float toScaleY = 0.5f;
		Animation animation = new ScaleAnimation(fromXScale, toScaleX, fromYScale, toScaleY);
		//设置动画持续时间
		animation.setDuration(3000);
		//通过View的startAnimation方法将动画立即应用到View上
		textView.startAnimation(animation);

效果图:
2)
//以View中心点作为缩放中心,水平方向和垂直方向都缩小为原来的一半
float fromXScale = 1.0f;
float toScaleX = 0.5f;
float fromYScale = 1.0f;
float toScaleY = 0.5f;
float pivotX = textView.getWidth() / 2;
float pivotY = textView.getHeight() / 2;
Animation animation = new ScaleAnimation(
    fromXScale, toScaleX,
    fromYScale, toScaleY,
    pivotX, pivotY
);
    //设置动画持续时间
    animation.setDuration(3000);
    //通过View的startAnimation方法将动画立即应用到View上
    textView.startAnimation(animation);

效果图:
3)
//以View中心点作为缩放中心,水平方向和垂直方向都缩小为原来的一半
float fromXScale = 1.0f;
float toScaleX = 0.5f;
float fromYScale = 1.0f;
float toScaleY = 0.5f;
int pivotXType = Animation.RELATIVE_TO_SELF;
float pivotXValue = 0.5f;
int pivotYType = Animation.RELATIVE_TO_SELF;
float pivotYValue = 0.5f;
Animation animation = new ScaleAnimation(
    fromXScale, toScaleX,
    fromYScale, toScaleY,
    pivotXType, pivotXValue,
    pivotYType, pivotYValue
);
//设置动画持续时间
animation.setDuration(3000);
//通过View的startAnimation方法将动画立即应用到View上
textView.startAnimation(animation);

效果图:

平移TranslateAnimation

有两种构造方法:
1)
		int fromXDelta = 0;
		int toXDelta = getResources().getDisplayMetrics().widthPixels / 2;
		int fromYDelta = 0;
		int toYDelta = 0;
		//让动画在水平位置上沿X轴平移toXDelta个像素
		Animation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
		//设置动画持续时间为5000毫秒
		animation.setDuration(5000);
		textView.startAnimation(animation);
效果图:
部分代码介绍:
  • fromXDelta 表示动画开始时View相对于原来位置X轴方向的偏移坐标

  • toXDelta 表示动画结束时View相对于原来位置X轴方向的偏移坐标

  • fromYDelta 表示动画开始时View相对于原来位置Y轴方向的偏移坐标

  • toYDelta 表示动画结束时View相对于原来位置Y轴方向的偏移坐标

2)
//设置fromX
int fromXType = Animation.ABSOLUTE;
float fromXValue = textView.getX();
//设置toX
int toXType = Animation.RELATIVE_TO_PARENT;
float toXValue = 0.5f;
//设置fromY
int fromYType = Animation.ABSOLUTE;
float fromYValue = textView.getY();
//设置toY
int toYType = Animation.RELATIVE_TO_SELF;
float toYValue = 3.0f;
//创建动画
Animation animation = new TranslateAnimation(
        fromXType, fromXValue,
        toXType, toXValue,
        fromYType, fromYValue,
        toYType, toYValue);
//设置动画持续时间为3000毫秒
animation.setDuration(3000);
//通过View的startAnimation方法将动画立即应用到View上
textView.startAnimation(animation);

效果图:

部分代码介绍:

fromXType和fromXValue进行说明,fromXType的取值类型决定了如何设置fromXValue的值。fromXType的取值有三种,分别是:ABSOLUTE、RELATIVE_TO_PARENT和RELATIVE_TO_SELF。

  • ABSOLUTE 
    当fromXType取值为ABSOLUTE时,表示fromXValue的值是在该View的父控件的坐标系的绝对值,比如fromXValue为200,表示动画开始时,View的左侧到其父控件左侧的距离是200个像素。

  • RELATIVE_TO_PARENT 
    当fromXType取值为RELATIVE_TO_PARENT时,表示fromXValue的值是相对于其父控件尺寸的百分比。比如fromXValue为0,表示动画开始时,View的左侧紧靠父控件的左侧;fromXValue为0.5时,表示动画开始时,View的左侧位置在父控件水平方向中间的位置;fromXValue为1时,表示动画开始时,View的左侧位置与父控件的右侧位置完全重合。

  • RELATIVE_TO_SELF 
    当fromXType取值为RELATIVE_TO_SELF时,表示fromXValue的值是相对于其自身尺寸的百分比。比如fromXValue为0,表示动画开始时,View的X坐标和初始位置的X坐标相同;fromXValue为0.5时,表示动画开始时,View的左侧位置在初始View状态下水平方向中间的位置,即向右偏移了View宽度的一半;fromXValue为1时,表示动画开始时,View的左侧位置正好与初始View状态下的右侧位置重合,即向右偏移了正好View的宽度大小的距离。

渐变AlphaAnimation

代码:
//1.0表示完全不透明,0.0表示完全透明
float fromAlpha = 0.0f;
float toAlpha = 1.0f;
//1.0 => 0.0表示View从完全不透明渐变到完全透明
Animation animation = new AlphaAnimation(fromAlpha, toAlpha);
//设置动画持续时间为3000毫秒
animation.setDuration(5000);
//通过View的startAnimation方法将动画立即应用到View上
textView.startAnimation(animation);


效果图:




旋转RotateAnimation

1)
//以View左上角为旋转轴,创建旋转60度的动画
Animation animation = new RotateAnimation(0, 60);
//设置动画持续时间
animation.setDuration(3000);
//通过View的startAnimation方法将动画立即应用到View上
textView.startAnimation(animation);
效果图:


2)hava problem(这种方式中我在使用的时候涉及到一个问题

解决在onCreate()过程中获取View的width和Height为0的4种方法

<pre name="code" class="html">	//以View中心点作为旋转轴,创建旋转90度的动画
		textView.post(new Runnable() {
			
			@Override
			public void run() {
				float pivotX = textView.getWidth() / 2;
				float pivotY = textView.getHeight() / 2;
				Animation animation = new RotateAnimation(0, 90, pivotX, pivotY);
				//设置动画持续时间
				animation.setDuration(3000);
				//通过View的startAnimation方法将动画立即应用到View上
				textView.startAnimation(animation);
			}
		});

 效果图: 
3)
//以View的父控件中心点作为旋转轴,创建旋转360度的动画
int pivotXType = Animation.RELATIVE_TO_PARENT;
float pivotXValue = 0.5f;
int pivotYType = Animation.RELATIVE_TO_PARENT;
float pivotYValue = 0.5f;
Animation animation = new RotateAnimation(
    0, 360,
    pivotXType, pivotXValue,
    pivotYType, pivotYValue
);
//设置动画持续时间
animation.setDuration(3000);
//通过View的startAnimation方法将动画立即应用到View上
textView.startAnimation(animation);

效果图:

总结:主要就是三部吧:
  1)创建Animation某个子类的实例
  2)通过Animation的setDuration方法设置动画持续时间
  3)最后通过View的startAnimation方法启动动画
对了还有最后一个(说算也不算的。。)

AnimationSet
	//初始化 Translate动画  
		TranslateAnimation	translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);  
		//初始化 Alpha动画  
		AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);  
		  
		//动画集  
		AnimationSet set = new AnimationSet(true);  
		set.addAnimation(translateAnimation);  
		set.addAnimation(alphaAnimation);  
		  
		//设置动画时间 (作用到每个动画)  
		set.setDuration(3000);  
		//通过View的startAnimation方法将动画立即应用到View上
		textView.startAnimation(set);  

效果图





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值