android动画基础:tween动画

阅读更多
工程结构图:
[img]
android动画基础:tween动画_第1张图片
[/img]


四个动画的xml文件:


    
    





    
    


 




    
    







    
    






MainActivity:
package com.zzl.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 1,透明动画效果
		Animation animation1 = AnimationUtils.loadAnimation(MainActivity.this,
				R.anim.alpha);
		// 2,移动动画效果
		Animation animation2 = AnimationUtils.loadAnimation(MainActivity.this,
				R.anim.translate);
		// 3,缩放动画效果
		Animation animation3 = AnimationUtils.loadAnimation(MainActivity.this,
				R.anim.scale);
		// 4,旋转动画效果
		Animation animation4 = AnimationUtils.loadAnimation(MainActivity.this,
				R.anim.rotate);

		// 如果设为 true,表示设置动画完成后保持动画后的状态
		animation1.setFillAfter(true);
		// animation2.setFillAfter(true);

		/**
		 * 用代码添加一个旋转动画
		 */

		Animation animation = new RotateAnimation(0, 360,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		animation.setDuration(10000);
		ImageView iv = (ImageView) findViewById(R.id.iv);
		iv.setAnimation(animation);
	}
}





Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。

第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似。



下面就讲一下Tweene Animations。

AlphaAnimation



通过代码实现 AlphaAnimation,如下:
    //初始化  
    Animation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);  
    //设置动画时间            alphaAnimation.setDuration(3000);  
                    this.startAnimation(alphaAnimation);  

其中AlphaAnimation类第一个参数fromAlpha表示动画起始时的透明度, 第二个参数toAlpha表示动画结束时的透明度。

setDuration用来设置动画持续时间。








RotateAnimation
代码:
    Animation rotateAnimation = new RotateAnimation(0f, 360f);  
                    rotateAnimation.setDuration(1000);  
                    this.startAnimation(rotateAnimation);  

其中RotateAnimation类第一个参数fromDegrees表示动画起始时的角度, 第二个参数toDegrees表示动画结束时的角度。

另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y 坐标的开始位置pivotXValue、pivotYValue等。






ScaleAnimation

代码:
    //初始化  
    Animation scaleAnimation = new ScaleAnimation(0.1f, 1.0f,0.1f,1.0f);  
    //设置动画时间  
    scaleAnimation.setDuration(500);  
                    this.startAnimation(scaleAnimation);  

ScaleAnimation类中

第一个参数fromX ,第二个参数toX:分别是动画起始、结束时X坐标上的伸缩尺寸。

第三个参数fromY ,第四个参数toY:分别是动画起始、结束时Y坐标上的伸缩尺寸。

另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y 坐标的开始位置pivotXValue、pivotYValue等。



TranslateAnimation
    //初始化  
    Animation translateAnimation = new TranslateAnimation(0.1f, 100.0f,0.1f,100.0f);  
    //设置动画时间                translateAnimation.setDuration(1000);  
                                this.startAnimation(translateAnimation);  

TranslateAnimation类



第一个参数fromXDelta ,第二个参数toXDelta:分别是动画起始、结束时X坐标。

第三个参数fromYDelta ,第四个参数toYDelta:分别是动画起始、结束时Y坐标。
  • android动画基础:tween动画_第2张图片
  • 大小: 30 KB
  • 查看图片附件

你可能感兴趣的:(android)