package com.xunao.effectdemo.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.xunao.effectdemo.R;

import java.util.Random;

/**
 * author : 程中强
 * e-mail : 740479946@qq.com
 * date : 2022/8/121:40
 * desc :
 * version: 1.0
 */
public class AnimActivity extends Activity implements View.OnClickListener {

	Button btn_danru,btn_suofang,btn_xuanzhuan,btn_yidong;
	ImageView imv;
	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_anim);
		imv = findViewById(R.id.imv);
		imv.setOnClickListener(this);
		btn_danru = findViewById(R.id.danru);
		btn_danru.setOnClickListener(this);
		btn_suofang = findViewById(R.id.suofang);
		btn_suofang.setOnClickListener(this);
		btn_xuanzhuan = findViewById(R.id.xuanzhuan);
		btn_xuanzhuan.setOnClickListener(this);
		btn_yidong = findViewById(R.id.yidong);
		btn_yidong.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO 自动生成的方法存根
		switch (view.getId()) {


			case R.id.danru://淡入淡出

				AnimationSet aset=new AnimationSet(true);
				AlphaAnimation aa=new AlphaAnimation(1,0);
				aa.setDuration(2000);
				aset.addAnimation(aa);
				btn_danru.startAnimation(aset);
				break;


			case R.id.xuanzhuan://旋转效果
				AnimationSet aset_2=new AnimationSet(true);
				RotateAnimation aa_2=new RotateAnimation(0,360,
						Animation.RELATIVE_TO_SELF,0.5f,
						Animation.RELATIVE_TO_SELF,0.5f);
				aa_2.setDuration(2000);
				aset_2.addAnimation(aa_2);
				btn_xuanzhuan.startAnimation(aset_2);
				break;


			case R.id.suofang://缩放效果
				AnimationSet aset_3=new AnimationSet(true);
				ScaleAnimation aa_3=new ScaleAnimation(1, 0.1f, 1, 0.1f,Animation.RELATIVE_TO_SELF,1f,Animation.RELATIVE_TO_SELF,1f);
				aa_3.setDuration(2000);
				aset_3.addAnimation(aa_3);
				btn_suofang.startAnimation(aset_3);
				break;

			case R.id.yidong://移动效果
				AnimationSet aset_4=new AnimationSet(true);
				TranslateAnimation aa_4=new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT, 1,
						Animation.RELATIVE_TO_PARENT, 0,Animation.RELATIVE_TO_PARENT, 1);
				aa_4.setDuration(2000);
				aset_4.addAnimation(aa_4);
				btn_yidong.startAnimation(aset_4);
				break;
			case R.id.imv:
				getAnim();
				break;
		}
	}

	void getAnim(){
		Random random=new Random();
		int num = random.nextInt(5);
		Log.e("MyTag","num:"+num);
		imv.clearAnimation();
		switch (num){
			case 0://淡入淡出
//				AnimationSet aset=new AnimationSet(true);
//				AlphaAnimation aa=new AlphaAnimation(1,0);
//				aa.setDuration(2000);
//				aset.addAnimation(aa);
//				imv.startAnimation(aset);

				Animation translateAnimation = new TranslateAnimation(-20, 20, 0, 0);

				translateAnimation.setDuration(100);//每次时间

				translateAnimation.setRepeatCount(3);//重复次数

				/**倒序重复REVERSE 正序重复RESTART**/

				translateAnimation.setRepeatMode(Animation.REVERSE);

				imv.startAnimation(translateAnimation);


				break;

			case 1://旋转效果
				AnimationSet aset_2=new AnimationSet(true);
				RotateAnimation aa_2=new RotateAnimation(0,360,
						Animation.RELATIVE_TO_SELF,0.5f,
						Animation.RELATIVE_TO_SELF,0.5f);
				aa_2.setDuration(2000);
				aset_2.addAnimation(aa_2);
				imv.startAnimation(aset_2);
				break;

			case 2://翻转效果

				ObjectAnimator visToInvis = ObjectAnimator.ofFloat(imv, "rotationY", 0f, 90f);
				visToInvis.setDuration(500);
				visToInvis.setInterpolator(new AccelerateInterpolator());
				final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(imv, "rotationY", -90f, 0f);

				invisToVis.setDuration(500);

				invisToVis.setInterpolator(new DecelerateInterpolator());

				visToInvis.addListener(new AnimatorListenerAdapter() {

					@Override

					public void onAnimationEnd(Animator anim) {

						imv.setVisibility(View.GONE);

						invisToVis.start();

						imv.setVisibility(View.VISIBLE);

					}

				});

				visToInvis.start();

				break;
			case 3://移动效果
				Animation aset_4 = new TranslateAnimation(0, 0, -20, 20);

				aset_4.setDuration(100);//每次时间

				aset_4.setRepeatCount(3);//重复次数

				/**倒序重复REVERSE 正序重复RESTART**/

				aset_4.setRepeatMode(Animation.REVERSE);

				imv.startAnimation(aset_4);

//				AnimationSet aset_4=new AnimationSet(true);
//				TranslateAnimation aa_4=new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT, 1,
//						Animation.RELATIVE_TO_PARENT, 0,Animation.RELATIVE_TO_PARENT, 1);
//				aa_4.setDuration(2000);
//				aset_4.addAnimation(aa_4);
//				imv.startAnimation(aset_4);
				break;

			case 4:
				Animation animation4 = AnimationUtils.loadAnimation(AnimActivity.this, R.anim.anim_small);
				imv.startAnimation(animation4);
				break;
		};
	}

}
