<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.objectanimator.MainActivity" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Button" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="Button" /> </RelativeLayout>
MainActivity
<pre name="code" class="java">package com.example.objectanimator; import android.animation.Animator; import android.animation.Animator.AnimatorListener; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.AnimationSet; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private Button button; private ImageView imageView; private Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); button1 = (Button) findViewById(R.id.button1); imageView = (ImageView) findViewById(R.id.imageView); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /** * 三个动画同时进行,方法一 */ // ObjectAnimator.ofFloat(imageView, "translationX", // 0f,200f).setDuration(2000).start(); // ObjectAnimator.ofFloat(imageView, "translationY", // 0f,200f).setDuration(2000).start(); // ObjectAnimator.ofFloat(imageView, "rotation", // 0f,360f).setDuration(2000).start(); /** * 三个动画同时进行,方法二 */ // PropertyValuesHolder p1 = // PropertyValuesHolder.ofFloat("rotation", 0f,360f); // PropertyValuesHolder p2 = // PropertyValuesHolder.ofFloat("translationX", 0f,200f); // PropertyValuesHolder p3 = // PropertyValuesHolder.ofFloat("translationY", 0f,200f); // ObjectAnimator.ofPropertyValuesHolder(imageView, // p1,p2,p3).setDuration(2000).start(); /** * 三个动画同时进行,方法三 */ // ObjectAnimator ofFloat1 = ObjectAnimator.ofFloat(imageView, // "translationX", 0f,200f); // ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(imageView, // "translationY", 0f,200f); // ObjectAnimator ofFloat3 = ObjectAnimator.ofFloat(imageView, // "rotation", 0f,360f); // AnimatorSet set = new AnimatorSet(); // set.playTogether(ofFloat1,ofFloat2,ofFloat3); // set.setDuration(2000); // set.start(); /** * 三个动画依次、依次进行 */ // ObjectAnimator ofFloat1 = ObjectAnimator.ofFloat(imageView, // "translationX", 0f,200f); // ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(imageView, // "translationY", 0f,200f); // ObjectAnimator ofFloat3 = ObjectAnimator.ofFloat(imageView, // "rotation", 0f,360f); // AnimatorSet set = new AnimatorSet(); // set.playSequentially(ofFloat1,ofFloat2,ofFloat3); // set.setDuration(2000); // set.start(); /** * 动画一和三一起执行,然后执行动画二 */ ObjectAnimator ofFloat1 = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f); ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 200f); ObjectAnimator ofFloat3 = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f); AnimatorSet set = new AnimatorSet(); set.play(ofFloat1).with(ofFloat3); set.play(ofFloat2).after(ofFloat3); set.setDuration(2000); set.start(); } }); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ObjectAnimator animator = ObjectAnimator.ofFloat(button1, "alpha", 0f,1f); animator.setDuration(1000); //添加监听方法一 // animator.addListener(new AnimatorListener() { // @Override // public void onAnimationStart(Animator animation) { // // } // // @Override // public void onAnimationEnd(Animator animation) { // //用的最多 // Toast.makeText(MainActivity.this, "end anim", 0).show(); // } // // @Override // public void onAnimationCancel(Animator animation) { // // } // // // @Override // public void onAnimationRepeat(Animator animation) { // // } // }); //添加监听方法二 animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); Toast.makeText(MainActivity.this, "end anim", 0).show(); } }); animator.start(); } }); } }
activity_main.xml
<pre name="code" class="html"><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.objectanimatordemo.MainActivity" > <ImageView android:id="@+id/imageView_b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/b" /> <ImageView android:id="@+id/imageView_c" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/c" /> <ImageView android:id="@+id/imageView_d" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/d" /> <ImageView android:id="@+id/imageView_e" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/e" /> <ImageView android:id="@+id/imageView_f" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/f" /> <ImageView android:id="@+id/imageView_g" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/g" /> <ImageView android:id="@+id/imageView_h" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingTop="5dp" android:src="@drawable/h" /> <ImageView android:id="@+id/imageView_a" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/a" /> </FrameLayout>
<pre name="code" class="java">package com.example.objectanimatordemo; import java.util.ArrayList; import java.util.List; import android.animation.ObjectAnimator; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.BounceInterpolator; import android.widget.ImageView; public class MainActivity extends Activity implements View.OnClickListener { private int[] res = { R.id.imageView_a, R.id.imageView_b, R.id.imageView_c, R.id.imageView_d, R.id.imageView_e, R.id.imageView_f, R.id.imageView_g, R.id.imageView_h}; private List<ImageView> imageViewList = new ArrayList<ImageView>(); private boolean flag = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); for (int i = 0; i < res.length; i++) { ImageView imageView = (ImageView) findViewById(res[i]); imageView.setOnClickListener(this); imageViewList.add(imageView); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.imageView_a: if (flag) { startAnim(); }else { closeAnim(); } break; default: break; } } private void closeAnim() { for (int i = 1; i < res.length; i++) { ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i), "translationY",i*100,0F); animator.setInterpolator(new BounceInterpolator()); animator.setDuration(1000); animator.start(); } flag = true; } private void startAnim() { for (int i = 1; i < res.length; i++) { ObjectAnimator animator = ObjectAnimator.ofFloat(imageViewList.get(i), "translationY",0F,i*100); animator.setInterpolator(new BounceInterpolator()); animator.setDuration(1000); animator.start(); } flag = false; } }
************************************************valueAnimator**********************************
总而言之就是两点:
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" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="start anim" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffff00" android:padding="10dp" android:text="Hello qijian" /> </LinearLayout>
package com.harvic.myapp; import android.animation.*; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.animation.LinearInterpolator; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MyActivity extends Activity { private TextView tv; private Button btnStart; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.tv); btnStart = (Button) findViewById(R.id.btn); btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /** * 二、ValueAnimator简单使用之实例使用ValueAnimator */ // doAnimation(); /** * 三、常用方法之1:ofInt与ofFloat */ doOfFloatAnimation(); } }); tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MyActivity.this, "clicked me", Toast.LENGTH_SHORT).show(); } }); } /** * 二、1.ValueAnimator简单使用 */ private void doAnimation() { ValueAnimator animator = ValueAnimator.ofInt(0, 400); animator.setDuration(1000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int curValue = (Integer) animation.getAnimatedValue(); tv.layout(curValue, curValue, curValue + tv.getWidth(), curValue + tv.getHeight()); } }); animator.start(); } /** * 三、常用方法之1:ofInt与ofFloat */ private void doOfFloatAnimation() { ValueAnimator animator = ValueAnimator.ofFloat(0f, 400f, 50f, 300f); animator.setDuration(3000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { Float curValueFloat = (Float) animation.getAnimatedValue(); int curValue = curValueFloat.intValue(); tv.layout(curValue, curValue, curValue + tv.getWidth(), curValue + tv.getHeight()); } }); animator.start(); } }