本文介绍了五种不同的动画实现方式,其实归根结底,就是下面几种:

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 stubTranslateAnimation 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 stubObjectAnimator 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 stubPropertyValuesHolder 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 stubValueAnimator va = ValueAnimator.ofFloat(0.0f, 1.0f);va.addUpdateListener(new AnimatorUpdateListener() {public void onAnimationUpdate(ValueAnimator animation) {// TODO Auto-generated method stubfloat value = (Float) animation.getAnimatedValue();mImage04.setTranslationX(sTranslation * value);mImage04.setRotation(180 * value);}});va.setDuration(1000);va.start();}private void playAnima05() {// TODO Auto-generated method stubmImage05.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



更多相关文章

  1. 《第一行代码--Android》学习笔记--第一章:开始起航:思路
  2. android有用代码片段(1)
  3. android视频截屏&手机录屏实现代码
  4. Android 中 画图与动画旋转效果(中级)
  5. Android应用数据存储几种方式(3)
  6. Android混淆代码
  7. AdapterViewFlipper实现翻滚广告效果

随机推荐

  1. Android清单文件详解(四) ---- backupAge
  2. Android移动应用开发基础2003291341
  3. Android应用开发提高系列(4)――Android动
  4. 学习Android从0开始之ActionBar(活动条)之
  5. Android(安卓)使用意图播放本地视频
  6. Android(安卓)TextView setText卡顿问题
  7. Android Dev Guide -> User Interface
  8. Android手机的那点事
  9. Gartner:Android将在2012年成为第二大手
  10. 20172323 2017-2018-2《程序设计与数据结