在上一篇介绍了属性动画的 透明度、旋转、平移、比例 的效果,这一篇写组合动画,什么事组合动画,就是把上面所讲的动画效果同时使用。
我们需要用到一个AnimatorSet把多个ObjectAnimation动画组合起来。
package com.example.administrator.myanimator;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button Aniset;
private TextView textView;
AnimatorSet set;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Aniset = (Button) findViewById(R.id.setAniBtn);
textView = (TextView) findViewById(R.id.tvTest);
//透明度动画
ObjectAnimator alphaAni = ObjectAnimator.ofFloat(textView, "alpha", 1, 0, 1);
//旋转动画
ObjectAnimator rotationAni = ObjectAnimator.ofFloat(textView, "rotation", 0, 360, 360);
//平移动画
float curTranslation = textView.getTranslationX();
ObjectAnimator translationAni = ObjectAnimator.ofFloat(textView, "translationX", curTranslation, -500f, curTranslation);
set = new AnimatorSet();
//在进行平移之后,旋转和透明度变化同时进行 set.play(rotationAni).with(alphaAni).after(translationAni);
set.setDuration(5*1000);
Aniset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//开始组合动画
set.start();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!"
android:textSize="30dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="组合动画"
android:id="@+id/setAniBtn"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="58dp" />
</RelativeLayout>