Android属性动画基础

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;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值