package com.dysania.artorandroid.chapter07;

import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.IntEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * Created by DysaniazzZ on 06/07/2017.
 */

public class PropertyAnimActivity extends AppCompatActivity {

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, PropertyAnimActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property_anim);

        findViewById(R.id.tv_object_animator).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                ObjectAnimator.ofFloat(v, "translationY", -v.getHeight(), v.getHeight()).start();    //默认时间是100ms

                ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater
                        .loadAnimator(v.getContext(), R.animator.object_anim_example);
                objectAnimator.setTarget(v);
                objectAnimator.start();
            }
        });

        findViewById(R.id.tv_value_animator).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(v.getContext(), R.animator.value_anim_example);
                valueAnimator.setTarget(v);
                valueAnimator.start();
            }
        });

        findViewById(R.id.tv_set_animator).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                AnimatorSet set = new AnimatorSet();
                //set.playSequentially()这个方法是按照顺序依次播放动画
                //set.playTogether()这个方法是一起播放所有的动画
                set.playTogether(
                        ObjectAnimator.ofFloat(v, "rotationY", 0, 360),
                        ObjectAnimator.ofFloat(v, "translationY", 0, 90),
                        ObjectAnimator.ofFloat(v, "scaleX", 0.5f, 2.0f),
                        ObjectAnimator.ofFloat(v, "scaleY", 0.5f, 2.0f),
                        ObjectAnimator.ofFloat(v, "alpha", 1.0f, 0.5f, 1.0f)
                );
                set.setDuration(1500).start();
            }
        });

        findViewById(R.id.btn_test1).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewWrapper viewWrapper = new ViewWrapper(v);
                ObjectAnimator.ofInt(viewWrapper, "width", 800).setDuration(1000).start();
            }
        });

        final IntEvaluator evaluator = new IntEvaluator();
        findViewById(R.id.btn_test2).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100);
                valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        //获取当前动画的进度（浮点数，0~1之间）
                        float animatedFraction = animation.getAnimatedFraction();
                        //调用整型估值器，计算出宽度赋值给目标View
                        v.getLayoutParams().width = evaluator.evaluate(animatedFraction, v.getWidth(), 800);
                        v.requestLayout();
                    }
                });
                valueAnimator.setDuration(1000).start();
            }
        });
    }

    public static class ViewWrapper {

        private View mTargetView;

        public ViewWrapper(View targetView) {
            mTargetView = targetView;
        }

        public void setWidth(int width) {
            mTargetView.getLayoutParams().width = width;
            mTargetView.requestLayout();
        }

        public int getWidth() {
            return mTargetView.getLayoutParams().width;
        }
    }
}

@@ -4032,24 +4032,183 @@
  %7D);%0A    %7D%0A%0A
+    @Override%0A    protected void onDestroy() %7B%0A        super.onDestroy();%0A        //TODO %E9%A1%B5%E9%9D%A2%E5%85%B3%E9%97%AD%E6%97%B6%E9%9C%80%E8%A6%81%E5%B0%86%E6%97%A0%E9%99%90%E5%BE%AA%E7%8E%AF%E7%9A%84%E5%B1%9E%E6%80%A7%E5%8A%A8%E7%94%BB%E5%81%9C%E6%AD%A2%EF%BC%8C%E9%81%BF%E5%85%8D%E5%87%BA%E7%8E%B0%E5%86%85%E5%AD%98%E6%B3%84%E9%9C%B2%0A        //ValueAnimator.cancel();%0A    %7D%0A%0A
     public s

stop the infinite animation when exit