標籤:
package com.example.administrator.mystudent.PropertyAnimation;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.animation.PropertyValuesHolder;import android.os.Bundle;import android.view.View;import android.view.animation.AnimationSet;import android.view.animation.BounceInterpolator;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageView;import com.example.administrator.mystudent.R;/** * Created by hyang on 2016/8/26. * 屬性動畫學習 */public class PropertyAnimationActivity extends android.app.Activity{ private ImageView img; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_property_animation); img= (ImageView) findViewById(R.id.iv_img); button= (Button) findViewById(R.id.btn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // setAnimation1(); //setAnimation2(); //setAnimation3(); setAnimation4(); } }); } /** * Tween動畫 */ private void setAnimation1(){ TranslateAnimation animation=new TranslateAnimation(0,200,0,0); animation.setDuration(3000); //animation.setFillAfter(true);//停留在動畫完成的位置 img.startAnimation(animation); } /** * 屬性動畫 * 方式1 */ private void setAnimation2(){ ObjectAnimator.ofFloat(img,"translationX",0,200f).setDuration(3000).start(); ObjectAnimator.ofFloat(img,"rotation",0,360f).setDuration(3000).start(); } /** * 屬性動畫 * 方式2 */ private void setAnimation3(){ PropertyValuesHolder p1=PropertyValuesHolder.ofFloat("translationX",0,200f); PropertyValuesHolder p2=PropertyValuesHolder.ofFloat("rotation",0,360f); ObjectAnimator.ofPropertyValuesHolder(img,p1,p2).setDuration(3000).start(); } /** * 屬性動畫 * 方式3 * 有動畫順序 */ private void setAnimation4(){ ObjectAnimator objectAnimator1=ObjectAnimator.ofFloat(img,"translationX",0,200f); ObjectAnimator objectAnimator2=ObjectAnimator.ofFloat(img,"rotation",0,360f); //建立android提供的幾種動畫效果(在Android的APIdemo中) objectAnimator1.setInterpolator(new BounceInterpolator()); AnimatorSet animatorSet=new AnimatorSet(); //animatorSet.playSequentially(objectAnimator1,objectAnimator2); //按順序播放動畫 animatorSet.play(objectAnimator1).after(objectAnimator2); animatorSet.setDuration(3000); animatorSet.start(); }}
Android屬性動畫