本文介绍了五种不同的动画实现方式,其实归根结底,就是下面几种:
1.AnimationSet(该方式使用所有android版本)但是使用不够灵活
下面的几种均只支持3.0以上的andorid系统
2.ObjectAnimator
3.PropertyValuesHolder
4.ValueAnimator
5.ViewPropertyAnimator,个人认为,对于不太的复杂的动画效果,该方法最为简单。
如:一行代码实现旋转加平移的效果
mView.animate().translationX(sTranslation).rotation(180).setDuration(1000).start();
话不多说,上代码:
AndroidTestActivity.java
package com.ville.test;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class AndroidTestActivity extends Activity {
/** Called when the activity is first created. */
private ImageView mImage01;
private ImageView mImage02;
private ImageView mImage03;
private ImageView mImage04;
private ImageView mImage05;
private static int sTranslation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImage01 = (ImageView) findViewById(R.id.ImageView01);
mImage02 = (ImageView) findViewById(R.id.ImageView02);
mImage03 = (ImageView) findViewById(R.id.ImageView03);
mImage04 = (ImageView) findViewById(R.id.ImageView04);
mImage05 = (ImageView) findViewById(R.id.ImageView05);
sTranslation = getResources().getDisplayMetrics().widthPixels*4/5;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
playAnima01();
playAnima02();
playAnima03();
playAnima04();
playAnima05();
break;
}
return super.onTouchEvent(event);
}
private void playAnima01() {
// TODO Auto-generated method stub
TranslateAnimation transAnim = new TranslateAnimation(0, sTranslation, 0, 0);
transAnim.setDuration(1000);
RotateAnimation rotateAnim = new RotateAnimation(0f, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(1000);
AnimationSet as = new AnimationSet(false);
as.addAnimation(rotateAnim);
as.addAnimation(transAnim);
as.setFillAfter(true);
mImage01.startAnimation(as);
}
private void playAnima02() {
// TODO Auto-generated method stub
ObjectAnimator transAnim = ObjectAnimator.ofFloat(mImage02, "translationX", 0, sTranslation);
transAnim.setDuration(1000);
ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(mImage02, "Rotation", 0, 180);
rotateAnim.setDuration(1000);
AnimatorSet as = new AnimatorSet();
as.playTogether(transAnim,rotateAnim);
as.start();
}
private void playAnima03() {
// TODO Auto-generated method stub
PropertyValuesHolder pvhTrans =
PropertyValuesHolder.ofFloat("translationX", 0, sTranslation + 50, sTranslation);
PropertyValuesHolder pvhRotate =
PropertyValuesHolder.ofFloat("Rotation", 0, 180);
ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(mImage03, pvhRotate, pvhTrans);
oa.setDuration(1000);
oa.start();
}
private void playAnima04() {
// TODO Auto-generated method stub
ValueAnimator va = ValueAnimator.ofFloat(0.0f, 1.0f);
va.addUpdateListener(new AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
// TODO Auto-generated method stub
float value = (Float) animation.getAnimatedValue();
mImage04.setTranslationX(sTranslation * value);
mImage04.setRotation(180 * value);
}
});
va.setDuration(1000);
va.start();
}
private void playAnima05() {
// TODO Auto-generated method stub
mImage05.setTranslationX(0);
mImage05.setRotation(0);
mImage05.animate().translationX(sTranslation).rotation(180).setDuration(1000).start();
}
}
布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="@string/hello" />
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/ImageView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/ImageView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/ImageView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/ImageView05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
这里是源代码,有兴趣的朋友可以下载:
AnimationDemo : http://download.csdn.net/detail/zjc08125/5169810
原文地址:http://blog.csdn.net/zjc08125/article/details/8705964