Android-动画小结

1.基本的Tween动画

常见的四种动画:AlphaAnimation,RotateAnimation,ScaleAnimation,TranslateAnimation动画,举例如下:

@Click
	void btnAlpha(){
		AlphaAnimation alphaAnimation = (AlphaAnimation) AnimationUtils
				.loadAnimation(this,R.anim.alpha);
		imageView.startAnimation(alphaAnimation);
	}
	
	@Click
	void btnRotate(){
		// 定义旋转动画,旋转一周持续1分钟,重复三次,在物体的中心位置
		RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
				Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		rotateAnimation.setDuration(3000);
		rotateAnimation.setRepeatCount(3);
		// 启动动画
		imageView.startAnimation(rotateAnimation);
	}
	
	@Click
	void btnScale(){
		// 定义缩放动画,从中心坐标开始,缩放1.5倍大小,持续1分钟,重复三次
		ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1.5f, 1,
				1.5f, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		scaleAnimation.setDuration(3000);
		scaleAnimation.setRepeatCount(3);
		// 启动动画
		imageView.startAnimation(scaleAnimation);
	}
	
	@Click
	void btnTranslate(){
		// 定义移动动画,都从自身坐标开始,移动2个位置,持续1分钟,重复三次
		TranslateAnimation translateAnimation = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 2,
				Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 2);
		translateAnimation.setDuration(3000);
		translateAnimation.setRepeatCount(3);
		// 启动动画
		imageView.startAnimation(translateAnimation);
	}
	
	@Click
	void btnComplex(){
		// 设置复杂的操作步骤,点击按钮complex后,会运行四种动画效果叠加
		AnimationSet sets = new AnimationSet(false);
		// 定义渐变动画
		AlphaAnimation _animation1 = new AlphaAnimation(1f, 0.1f);
		_animation1.setDuration(3000);
		// 定义旋转动画,在物体的中心位置
		RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360,
				Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		rotateAnimation1.setDuration(3000);
		// 定义缩放动画,从中心坐标开始,缩放1.5倍大小
		ScaleAnimation scaleAnimation1 = new ScaleAnimation(1, 1.5f, 1,
				1.5f, Animation.RELATIVE_TO_SELF, 0.5f,
				Animation.RELATIVE_TO_SELF, 0.5f);
		scaleAnimation1.setDuration(3000);
		// 定义移动动画,都从自身坐标开始,移动2个位置
		TranslateAnimation translateAnimation1 = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 2,
				Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 2);
		translateAnimation1.setDuration(3000);
		// 启动动画
		sets.addAnimation(_animation1);
		sets.addAnimation(rotateAnimation1);
		sets.addAnimation(scaleAnimation1);
		sets.addAnimation(translateAnimation1);
		imageView.startAnimation(sets);
	}

其中,alpha.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromAlpha="1"
    android:repeatCount="3"
    android:toAlpha="0.3" >
</alpha>


其实,一般我们在用基本动画的时候,我们通常使用xml的方式,比如:

1.1alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/accelerate_interpolator"
	android:fillAfter="true"
	android:fillBefore="false">
	<alpha
		android:fromAlpha="1.0"
		android:toAlpha="0.0"
		android:startOffset="1000"
		android:duration="1000" />

</set>

1.2scale.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/accelerate_interpolator">

	<scale android:fromXScale="1.0"
		android:toXScale="0.0"
		android:fromYScale="1.0"
		android:toYScale="0.0"
		android:pivotX="50%"
		android:pivotY="50%"
		android:duration="2000" />

</set>

1.3rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/accelerate_interpolator">

	<rotate android:fromDegrees="0"
		android:toDegrees="+360"
		android:pivotX="50%"
		android:pivotY="50%"
		android:duration="1000" />
</set>

1.4translate.xml文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/accelerate_interpolator">

	<translate
		android:fromXDelta="0%p"
		android:toXDelta="100%p"
		android:fromYDelta="0%p"
		android:toYDelta="100%p"
		android:duration="1000" />

</set>

其中的100%p中的“p”,指的是相对父控件的位置。

所有的这些xml文件,我们都可以通过下列方式进行加载:

Animation animation = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
imageView.startAnimation(animation);

同时,如果一个动画中包含多个动画,我们直接在xml中的set标签中添加即可,比如:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/accelerate_interpolator"
	android:shareInterpolator="true"
	android:fillAfter="true">

	<alpha
		android:fromAlpha="1.0"
		android:toAlpha="0.0"
		android:startOffset="1000"
		android:fillAfter="true"
		android:duration="2000" />

	<scale android:fromXScale="1.0"
		android:toXScale="0.5"
		android:fromYScale="1.0"
		android:toYScale="0.5"
		android:pivotX="50%"
		android:pivotY="50%"
		android:startOffset="1000"
		android:duration="2000" />
</set>

另外:我们还需要注意动画的加速器设置, Interpolator定义了动画变化的速率或规律,详见参考文档:http://developer.android.com/reference/android/view/animation/Interpolator.html

2.ObjectAnimator动画

举例:

@SuppressLint("NewApi")
	@Click
	void btnObjectA1(){
		ObjectAnimator anim1 = ObjectAnimator.ofFloat(textView, "rotation", 0f, 360f);
		ObjectAnimator anim2 = ObjectAnimator.ofFloat(textView, "alpha", 0f, 1f);
		Animator[] items = new Animator[2];
		items[0]=anim1;
		items[1]=anim2;
		AnimatorSet animSet = new AnimatorSet();
		animSet.playSequentially(items);
		animSet.setDuration(1000);
		animSet.start();
	}


3.Activity跳转动画

/**
	 * 从左向右
	 */
	@Click
	void btnList1(){
		Intent intent=new Intent(this,ActivityList1_.class);
		startActivity(intent);
		overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right); 
	}
	
	/**
	 * 淡入淡出
	 */
	@Click
	void btnList2(){
		Intent intent=new Intent(this,ActivityList2_.class);
		startActivity(intent);
		overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out); 
	}
	
	/**
	 * 缩小放大
	 */
	@Click
	void btnList3(){
		Intent intent=new Intent(this,ActivityList2_.class);
		startActivity(intent);
		overridePendingTransition(R.anim.zoomin,R.anim.zooout); 
	}

其中,zoomin和zooout为自定义的xml动画文件,在anim文件夹下。

zoomin.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:zAdjustment="top" >

    <scale
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%p"
        android:pivotY="50%p"
        android:toXScale="0.5"
        android:toYScale="0.5" />

    <alpha
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromAlpha="1.0"
        android:toAlpha="0" />

</set>

zooout.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator" >

    <scale
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXScale="2.0"
        android:fromYScale="2.0"
        android:pivotX="50%p"
        android:pivotY="50%p"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

部分界面截图:

Android-动画小结_第1张图片

项目下载:http://download.csdn.net/detail/nuptboyzhb/7262241

4.AnimationDrawable动画

还有一种帧动画,也即是AnimationDrawable动画,它是通过一帧帧的播放图片产生的动画:

我们可以在android项目中的/res/drawable/目录下,建立一个帧动画的xml文件:

<animation-list android:id="@+id/selected" android:oneshot="false">
    <item android:drawable="@drawable/wheel0" android:duration="50" />
    <item android:drawable="@drawable/wheel1" android:duration="50" />
    <item android:drawable="@drawable/wheel2" android:duration="50" />
    <item android:drawable="@drawable/wheel3" android:duration="50" />
    <item android:drawable="@drawable/wheel4" android:duration="50" />
    <item android:drawable="@drawable/wheel5" android:duration="50" />
 </animation-list>

其中的oneshot属性,是指播放次数。如果是false的话,就一直播放。true即播放一次,并且停在最后一帧。如果想要那种播放一次即可消失的效果,那么我们最后一帧可以设置一个透明的图片。好了,下面看一下怎么样在Java代码中使用:

// Load the ImageView that will host the animation and
 // set its background to our AnimationDrawable XML resource.
 ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
 img.setBackgroundResource(R.drawable.spin_animation);

 // Get the background, which has been compiled to an AnimationDrawable object.
 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

 // Start the animation (looped playback by default).
 frameAnimation.start();

这样,就能够一帧帧播放出图片了。

参考文档:

http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

5.Fragment切换动画

FragmentTransaction的setCustomAnimations设置fragment的动画。
看一下官方说明:

  
  
  
  
abstract  FragmentTransaction setCustomAnimations(int enter, int exit, int popEnter, int popExit)
Set specific animation resources to run for the fragments that are entering and exiting in this transaction.
abstract  FragmentTransaction setCustomAnimations(int enter, int exit)
Set specific animation resources to run for the fragments that are entering and exiting in this transaction.


6.ViewPager切换Fragment时候的动画:

mPager.setPageTransformer(true, new ZoomOutPageTransformer());
也即是通过setPageTransformer方法设置动画。

可以参见官方文档:http://developer.android.com/training/animation/screen-slide.html


7.控件之间的切换动画原理

一般思路是,对第一个控件设置一个动画,对该动画设置一个动画的事件监听器

setAnimationListener(Animation.AnimationListener listener)

比如:

setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// 动画播放开始
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// 动画重复

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				// 动画播放结束
			}
		});

在动画结束时,将控件设置为不可见,并将原来覆盖在它上面的控件,设置可见,并开始启动动画。最终的效果就是控件切换的效果。

8.ListView动画

http://www.cnblogs.com/xitang/archive/2013/07/19/3199753.html


更多动画属性,参见官方文档:http://developer.android.com/reference/android/view/animation/Animation.html

你可能感兴趣的:(Android-动画小结)