Facebook智能手机收发私信必须用Messenger

Animations定义:Animations提供了一系列的动画效果,这些效果可以应用在绝大多数的控件。

Animations从总体来看分为两大类:

1、Tweened Animations 该类提供了旋转、移动、伸展和淡出的效果

2、Frame-by-FrameAnimations 该类可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个个的显示

Tweened Animations的分类:

1、Alpha:淡入淡出效果

2、Scale:缩放效果

3、Rotate:旋转效果

4、Translate:移动效果


使用Tweened Animations的步骤:

1、创建一个AnimationSet对象

2、根据需要创建相应 的Animation对象

3、根据动画的需要,为Animation对象设置相应的数据

4、将Animation对象添加到AnimationSet对象当中

5、使用控件对象开始执行AnimationSet

例如:为控件绑定Alpha动画的过程

			//创建一个AnimationSet对象
			AnimationSet animationSet = new AnimationSet(true);
			//创建一个AlphaAnimation对象
			AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
			//设置动画执行的时间(单位:毫秒)
			alphaAnimation.setDuration(1000);
			//将AlphaAnimation对象添加到AnimationSet当中
			animationSet.addAnimation(alphaAnimation);
			//使用ImageView的startAnimation方法开始执行动画
			imageView.startAnimation(animationSet);

实现该四种动画代码如下:

public class MainActivity extends Activity {
	private ImageView imageView = null;
	private Button rotateButton = null;
	private Button scaleButton = null;
	private Button alphaButton = null;
	private Button translateButton = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		imageView = (ImageView) findViewById(R.id.imageViewId);

		rotateButton = (Button) findViewById(R.id.rotateButtonId);
		rotateButton.setOnClickListener(new RotateButtonListener());

		scaleButton = (Button) findViewById(R.id.scaleButtonId);
		scaleButton.setOnClickListener(new ScaleButtonListener());

		alphaButton = (Button) findViewById(R.id.alphaButtonId);
		alphaButton.setOnClickListener(new AlphaButtonListener());

		translateButton = (Button) findViewById(R.id.translateButtonId);
		translateButton.setOnClickListener(new TranslateButtonListener());
	}

	private class RotateButtonListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			AnimationSet animationSet = new AnimationSet(true);
			RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
					Animation.RELATIVE_TO_PARENT, 1f,
					Animation.RELATIVE_TO_PARENT, 0f);
			rotateAnimation.setDuration(5000);
			animationSet.addAnimation(rotateAnimation);
			imageView.startAnimation(animationSet);
		}
	}

	private class ScaleButtonListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			AnimationSet animationSet = new AnimationSet(true);
			ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			animationSet.addAnimation(scaleAnimation);
			animationSet.setStartOffset(1000);
			animationSet.setFillAfter(true);
			animationSet.setFillBefore(false);
			animationSet.setDuration(2000);
			imageView.startAnimation(animationSet);
		}

	}

	private class AlphaButtonListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			AnimationSet animationSet = new AnimationSet(true);
			AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
			alphaAnimation.setDuration(1000);
			animationSet.addAnimation(alphaAnimation);
			imageView.startAnimation(animationSet);
		}

	}

	private class TranslateButtonListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			AnimationSet animationSet = new AnimationSet(true);
			TranslateAnimation translateAnimation = new TranslateAnimation(
					Animation.RELATIVE_TO_SELF, 0f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0f,
					Animation.RELATIVE_TO_SELF, 1.0f);
			translateAnimation.setDuration(1000);
			animationSet.addAnimation(translateAnimation);
			imageView.startAnimation(animationSet);
		}
	}
}

Facebook智能手机收发私信必须用Messenger


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

对于上面使用动画的方法,在Java代码中使用,不利于代码的重复使用,更不利于代码的维护,所以就出现了下面的方法,在xml代码中定义动画。其步骤为:

1、在res文件夹下定义一个anim的文件夹

2、创建xml文件,并加入set标签,该标签如下:

<set xmlns:android="http://schemas.android.com/apk/res/android"
	android:interpolator="@android:anim/accelerate_interpolator">
</set>

3、再该标签中加入Alpha、Scale、Rotate、Translate标签

4、在代码当中使用AnimationUtils装载xml文件,并生成Animation对象

具体实现如下:

Facebook智能手机收发私信必须用Messenger

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

</set>

rotate.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="5000" />
</set>

scale.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>

translate.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="50%" 
		android:toXDelta="100%" 
		android:fromYDelta="0%" 
		android:toYDelta="100%" 
		android:duration="2000" />

</set>

MainActivity.java代码:

public class MainActivity extends Activity {
	private ImageView imageView = null;
	private Button rotateButton = null;
	private Button scaleButton = null;
	private Button alphaButton = null;
	private Button translateButton = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		imageView = (ImageView) findViewById(R.id.imageViewId);

		rotateButton = (Button) findViewById(R.id.rotateButtonId);
		rotateButton.setOnClickListener(new RotateButtonListener());

		scaleButton = (Button) findViewById(R.id.scaleButtonId);
		scaleButton.setOnClickListener(new ScaleButtonListener());

		alphaButton = (Button) findViewById(R.id.alphaButtonId);
		alphaButton.setOnClickListener(new AlphaButtonListener());

		translateButton = (Button) findViewById(R.id.translateButtonId);
		translateButton.setOnClickListener(new TranslateButtonListener());
	}

	private class RotateButtonListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
			imageView.startAnimation(animation);
		}
	}

	private class ScaleButtonListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.sacle);
			imageView.startAnimation(animation);
		}

	}

	private class AlphaButtonListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
			imageView.startAnimation(animation);
		}

	}

	private class TranslateButtonListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			Animation animation = (Animation) AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
			imageView.startAnimation(animation);
		}

	}
}


Facebook智能手机收发私信必须用Messenger,布布扣,bubuko.com

Facebook智能手机收发私信必须用Messenger

上一篇:Android Volley完全解析(一),初识Volley的基本用法


下一篇:【Android】activity 切换到后台onCreate重复执行的问题