您的位置:首页 > 移动开发 > Objective-C

属性动画之ObjectAnimator

2016-07-18 21:32 295 查看
先看看属性动画的继承结构,属性动画位于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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: