【Android学习】Android动画效果-Animations(二)

【Android学习】Android动画效果--Animations(2)

在Android开发中,很可能需要用到一些复杂的动画,而前面所学到的四个基本动画不能满足需要,则此时这个控件需要绑定多个动画,比如某张图片需要在缩小的同时淡出,这时就需要绑定Alpha和Scale。也就是AnimationSet。

当然,有时候也需要用到Interpolator,而对于Interpolator:

Interpolator定义了动画变化的速率,在Animations框架当中定义了以下几种Interpolator

1、AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候加速

2、AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速

3、CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线

4、DecelerateInterpolator:在动画开始的时候速率改变比较慢,然后开始减速

5、LinearInterpolator:动画以匀速开始改变

在绑定多种动画以及设定Interpolator有两种方法:

1、在java代码中设定:

			AnimationSet animationSet = new AnimationSet(false);
			AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
			alpha.setInterpolator(new DecelerateInterpolator());
			RotateAnimation rotate = new RotateAnimation(0, 360,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			rotate.setInterpolator(new AccelerateInterpolator());
			animationSet.addAnimation(alpha);
			animationSet.addAnimation(rotate);
			animationSet.setDuration(2000);
			animationSet.setStartOffset(500);
			imageView.startAnimation(animationSet);

2、在xml文件中设定:

alpha.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:shareInterpolator="true">
		
	<alpha 
		android:fromAlpha="1.0"
		android:toAlpha="0.0"
		android:startOffset="500"
		android:duration="2000" />

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

MainActivity.java:

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


<------------------------------------------------------------------------------>

Frame-By-Frame Animations的使用方法

1、在res/drawable当中创建一个XML文件,用于定义Animations的动画序列

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
	android:oneshot="false">
	<item android:drawable="@drawable/animation1" android:duration="500" />
	<item android:drawable="@drawable/animation2" android:duration="500" />
	<item android:drawable="@drawable/animation3" android:duration="500" />
	<item android:drawable="@drawable/animation4" android:duration="500" />
</animation-list>

2、为ImageView设置背景资源

imageView.setBackgroundResource(R.drawable.anim_nv);

3、通过InageView得到AnimationDrawable

AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();

4、开始执行动画

animationDrawable.start();

具体实现如下:

MainAcitivity.java:

public class MainActivity extends Activity {
	private Button button = null;
	private ImageView imageView = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView)findViewById(R.id.imageViewId);
        button = (Button)findViewById(R.id.buttonId);
        button.setOnClickListener(new ButtonListener());
    }
    
    private class ButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			imageView.setBackgroundResource(R.drawable.anim_nv);
			AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
			animationDrawable.start();
		}
    	
    }
}

anim_nv.xml:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
	android:oneshot="false">
	<item android:drawable="@drawable/animation1" android:duration="500" />
	<item android:drawable="@drawable/animation2" android:duration="500" />
	<item android:drawable="@drawable/animation3" android:duration="500" />
	<item android:drawable="@drawable/animation4" android:duration="500" />
</animation-list>

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<Button android:id="@+id/buttonId" 
	    android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:layout_alignParentBottom="true"
		android:text="测试动画效果" />

		
	<LinearLayout 
	    android:orientation="vertical"
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent">

		<ImageView 
		    android:id="@+id/imageViewId"
			android:layout_width="wrap_content" 
			android:layout_height="wrap_content"
			android:layout_centerInParent="true" 
			android:layout_marginTop="100dip"
			/>
	</LinearLayout>
</RelativeLayout>