属性动画之ObjectAnimator

先看看属性动画的继承结构,属性动画位于android.animation包中,基类为Animator

Animator

AnimatorSet

ValueAnimator

ObjectAnimator

TimeAnimator

下面是ObjectAnimator的一个列子,在move2方法里展示了三种播放动画的方式。(有两种方式被注释了的)。

package com.example.animationdemo;


import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button button = null;
	private ImageView imageView = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) findViewById(R.id.button);
		imageView = (ImageView) findViewById(R.id.imageview);
		
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();
			}
		});

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//在这里观察分别调用move1  和  move2 的区别。
				//move1();
				move2();
			}
		});

	}

	private void move1() {
		// Animation动画不适合做交互性的动画,只适合一些简单的显示性的动画,比如缩放,位移等。
		//Animation的动画完成后对应的图片不会有监听事件了。

		// 其中的参数意义为:第一个和第三个参数代表初始时X,Y的位置,第二个和第四个参数为最终位置的X,Y的位置。
		// 200的单位是像素。
		TranslateAnimation animation = new TranslateAnimation(0, 200, 0, 0);
		// 设置动画的时间
		animation.setDuration(1000);
		// 设置该参数之后,可以使动画完成后停留在最后的位置不动。
		animation.setFillAfter(true);
		imageView.startAnimation(animation);
	}
	/**
	 * 该方法展示了三种播放动画的方法。
	 */
	@SuppressLint("NewApi")
	private void move2(){
		//ofFloat函数中的参数意义为: 所要操作的对象,所要操作对象的属性,初始位置,最终位置。
		//通常这里可以操作的属性比如说translationX,都是有get和set方法的一些属性。
		//ObjectAnimator.ofFloat(imageView, "translationX", 0F, 200F).setDuration(1000).start();
		
		//运行之后,发现这三行代码是同时执行的,表明动画是异步执行的。
//		ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F).setDuration(1000).start();
//		ObjectAnimator.ofFloat(imageView, "translationX", 0F, 200F).setDuration(1000).start();
//		ObjectAnimator.ofFloat(imageView, "translationY", 0F, 200F).setDuration(1000).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(1000).start();
		
		ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "rotation", 0, 360F);
		ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationX", 0, 200F);
		ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "translationY", 0, 200F);
		AnimatorSet set = new AnimatorSet();
		//set.playSequentially(animator1,animator2,animator3);
		set.play(animator2).with(animator3);
		set.play(animator1).after(animator2);
		set.setDuration(1000);
		set.start();
		
		
		
		
	}

}

下面是布局文件:

<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=".MainActivity" >

    <ImageView 
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        />
    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageview"
        android:layout_marginTop="20dp"
        android:text="move"
        />
</RelativeLayout>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值