package one.bw.com.donghua;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image_view);
}
//位移动画
public void trans(View view) {
//参数1,,控件,,参数2,,x或y轴,,参数3,,动画的起始位置,,参数4,,动画的结束位置
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "x", imageView.getTop(), 300);
animator.setDuration(3000);//动画执行的时间
animator.setInterpolator(new AccelerateInterpolator());//加速的插值器
animator.start();//启动动画
//动画的监听事件
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
Toast.makeText(MainActivity.this,"动画结束",Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
}
//旋转动画
public void rotrate(View view) {
//参数1...控件,,参数2...x或y轴,,,参数3..起始角度,,,参数4...旋转角度
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotationY", 0, 360);
animator.setDuration(3000);
// animator.setRepeatCount(5);//动画执行的次数
//animator.setRepeatMode(ValueAnimator.REVERSE);//动画执行的模式
animator.start();
}
//透明动画
public void alpha(View view) {
//参数1...控件,,参数2...动画的类型,,,参数3..起始清晰度,,,参数4...结束清晰度
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "alpha", 0, 1);
animator.setDuration(3000);
animator.start();
}
//缩放动画
public void scale(View view) {
//参数1...控件,,参数2...动画的类型,,,参数3..起始图片大小,,,参数4...结束图片大小
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "scaleX", 0, 2);
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "scaleY", 0, 2);
animator.setDuration(3000);
animator.start();
animator1.setDuration(3000);
animator1.start();
}
//组合动画
public void zuhe_02(View view) {
ObjectAnimator y = ObjectAnimator.ofFloat(imageView, "y", 0, 250);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 0.5f, 2);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 0.5f, 2);
ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1.0f);
ObjectAnimator rotationX = ObjectAnimator.ofFloat(imageView, "rotationX", 0, 360);
ObjectAnimator rotationY = ObjectAnimator.ofFloat(imageView, "rotationY", 0, 360);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(y).with(scaleX).with(scaleY).with(alpha);
animatorSet.setDuration(5000);
animatorSet.start();
//动画的执行过程监听
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
long currentPlayTime = animation.getCurrentPlayTime();//获取动画需要执行的总时长
if(currentPlayTime>1000){
//逻辑操作
}
}
});
}}