首页 > 系统 > Android > 正文

Android属性动画基础

2019-11-08 00:04:12
字体:
来源:转载
供稿:网友
package com.twac.animationtest2;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.animation.PRopertyValuesHolder;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {	private ImageView mImageView;	private Button mButton1, mButton2, mButton3, mButton4, mButton5;	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		mImageView = (ImageView) findViewById(R.id.image01);		mButton1 = (Button) findViewById(R.id.button01);		mButton2 = (Button) findViewById(R.id.button02);		mButton3 = (Button) findViewById(R.id.button03);		mButton4 = (Button) findViewById(R.id.button04);		mButton5 = (Button) findViewById(R.id.button05);		mButton1.setOnClickListener(this);		mButton2.setOnClickListener(this);		mButton3.setOnClickListener(this);		mButton4.setOnClickListener(this);		mButton5.setOnClickListener(this);		mImageView.setOnClickListener(this);	}	@Override	public void onClick(View arg0) {		switch (arg0.getId()) {		// *********方法一*********		case R.id.button01:			ObjectAnimator.ofFloat(mImageView, "TranslationX", 0f, 200f)					.setDuration(1000).start();			ObjectAnimator.ofFloat(mImageView, "TranslationY", 0f, 200f)					.setDuration(1000).start();			ObjectAnimator.ofFloat(mImageView, "rotation", 0f, 360f)					.setDuration(1000).start();			break;		// *********方法二*********		case R.id.button02:			PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",					0f, 360f);			PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat(					"TranslationY", 0f, 200f);			PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat(					"TranslationX", 0f, 200f);			ObjectAnimator.ofPropertyValuesHolder(mImageView, p1, p2, p3)					.setDuration(2000).start();			break;		// *********方法三*********		case R.id.button03:			ObjectAnimator oa1 = ObjectAnimator.ofFloat(mImageView,					"TranslationX", 0f, 200f);			ObjectAnimator oa2 = ObjectAnimator.ofFloat(mImageView,					"TranslationY", 0f, 200f);			ObjectAnimator oa3 = ObjectAnimator.ofFloat(mImageView, "rotation",					0f, 360f);			AnimatorSet set1 = new AnimatorSet();			set1.playTogether(oa1, oa2, oa3);			set1.setDuration(2000);			set1.start();			break;		// *********方法四*********		case R.id.button04:			ObjectAnimator animation1 = ObjectAnimator.ofFloat(mImageView,					"TranslationX", 0f, 200f);			ObjectAnimator animation2 = ObjectAnimator.ofFloat(mImageView,					"TranslationY", 0f, 200f);			ObjectAnimator animation3 = ObjectAnimator.ofFloat(mImageView,					"rotation", 0f, 360f);			AnimatorSet set2 = new AnimatorSet();			set2.playSequentially(animation1, animation2, animation3);			set2.setDuration(2000);			set2.start();			break;		// *********方法五 *********		case R.id.button05:			ObjectAnimator animation11 = ObjectAnimator.ofFloat(mImageView,					"TranslationX", 0f, 200f);			ObjectAnimator animation22 = ObjectAnimator.ofFloat(mImageView,					"TranslationY", 0f, 200f);			ObjectAnimator animation33 = ObjectAnimator.ofFloat(mImageView,					"rotation", 0f, 360f);			AnimatorSet set3 = new AnimatorSet();			set3.play(animation11).with(animation33);			set3.play(animation22).after(animation33);			set3.setDuration(2000);			set3.start();			break;		case R.id.image01:			Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT)					.show();			break;		default:			break;		}	}}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表