欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

属性动画(学习)

程序员文章站 2022-03-17 13:12:17
...
package com.wangbin.animtion_property;

import android.animation.AnimatorSet;
import android.animation.FloatEvaluator;
import android.animation.ObjectAnimator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


/*
 * 补间动画只会改变view的显示效果
 * 不会改变view的值
 * 但是属性动画   移动了一个button 然后再后面还能点击
 *
 * */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private Button btn_one;
    private Button btn_two;
    private Button btn_three;
    private Button btn_four;
    private Button btn_test;
    private LinearLayout ly_root;
    private ImageView img_babi;
    private int width;
    private int height;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();
    }

    private void bindViews() {
        ly_root = (LinearLayout) findViewById(R.id.ly_root);
        btn_one = (Button) findViewById(R.id.btn_one);
        btn_two = (Button) findViewById(R.id.btn_two);
        btn_three = (Button) findViewById(R.id.btn_three);
        btn_four = (Button) findViewById(R.id.btn_four);
        btn_test = (Button) findViewById(R.id.btn_test);
        img_babi = (ImageView) findViewById(R.id.img_babi);

        btn_one.setOnClickListener(this);
        btn_two.setOnClickListener(this);
        btn_three.setOnClickListener(this);
        btn_four.setOnClickListener(this);
        btn_test.setOnClickListener(this);
        img_babi.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_one:
                lineAnimator();
                break;
            case R.id.btn_two:
                scaleAnimator();
                break;
            case R.id.btn_three:
                raAnimator();
                break;
            case R.id.btn_four:
                circleAnimator();
                break;
            case R.id.btn_test:
                alphAnimator();
                break;
            case R.id.img_babi:
                Toast.makeText(MainActivity.this, "啦啦啦", Toast.LENGTH_SHORT).show();
                break;
        }
    }


    //定义一个修改ImageView位置的方法
    private void moveView(View view, int rawX, int rawY) {
        int left = rawX - img_babi.getWidth() / 2;
        int top = rawY - img_babi.getHeight();
        int width = left + view.getWidth();
        int height = top + view.getHeight();
        view.layout(left, top, width, height);
    }

    private void TestAnimator() {
        width = ly_root.getWidth();
        height = ly_root.getHeight();

        ValueAnimator anim = ValueAnimator.ofFloat(100, 10000);
        anim.setDuration(300);//设置动画延迟播放的时间  ms
      /*  anim.setRepeatCount(2);//设置循环次数
        anim.setRepeatMode(ValueAnimator.REVERSE);*/

        /*
         * 属性动画设置监听事件
         * */
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float currentValue = (float) animation.getAnimatedValue();
                Log.d(TAG, "当前的值是:==========" + currentValue);
                int y = (Integer) animation.getAnimatedValue(); //
                int x = width / 2;
                moveView(img_babi, x, y);

            }
        });
        anim.setInterpolator(new LinearInterpolator());
        anim.start();

    }
    //定义属性动画的方法:

    //按轨迹方程来运动
    //

    //仅仅表示的是数值的平滑过渡一个数值到另一个数值的平滑过渡
    //中间可以再加上很多的参数,但是基本的属性都是不会变的
    //使用的是可变长参数,但是总的时间是不发生改变的
    /*
     *
     * */

    //使用线性移动
    private void lineAnimator() {
        width = ly_root.getWidth();
        height = ly_root.getHeight();
        // ValueAnimator xValue = ValueAnimator.ofInt(height,0,height / 4,height / 2,height / 4 * 3 ,height);

        ValueAnimator xValue = ValueAnimator.ofInt(height, 0, height / 4, height / 2, width / 4 * 3);//设置一个平滑的改变的数字
        xValue.setDuration(3000L); //设置的时间是
        xValue.setRepeatCount(1);//设置的是循环的次数  从0开始  默认就是0
        xValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
      /*          int value = (int) animation.getAnimatedValue();
                Log.d(TAG, "现在的值为=======" + value);*/
                long duration = animation.getDuration();
                long currentPlayTime = animation.getCurrentPlayTime();

                //   Log.d(TAG, "设置的时间============="+ currentPlayTime);

                // 轨迹方程 x = width / 2
                int y = (Integer) animation.getAnimatedValue();//获得当前的运动时间 从动画刚开始运行就开始计算  然后根据设置的延长时间  还有就是 循环次数
                // 循环次数 * 一次的时间  == 总计用时
                int x = width / 2;
                Log.d(TAG, "x======" + x + "======" + "y======" + y);
                moveView(img_babi, x, y);
            }
        });
        //设置插值器

        xValue.setInterpolator(new LinearInterpolator());
        xValue.start();
    }

    //缩放效果
    private void scaleAnimator() {

        //这里故意用两个是想让大家体会下组合动画怎么用而已~
        final float scale = 0.5f;
        AnimatorSet scaleSet = new AnimatorSet();
        ValueAnimator valueAnimatorSmall = ValueAnimator.ofFloat(1.0f, scale);
        valueAnimatorSmall.setDuration(500);


        ValueAnimator valueAnimatorLarge = ValueAnimator.ofFloat(scale, 1.0f);
        valueAnimatorLarge.setDuration(500);

        valueAnimatorSmall.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float scale = (Float) animation.getAnimatedValue();
                img_babi.setScaleX(scale);
                img_babi.setScaleY(scale);
            }
        });
        valueAnimatorLarge.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float scale = (Float) animation.getAnimatedValue();
                img_babi.setScaleX(scale);
                img_babi.setScaleY(scale);
            }
        });

        scaleSet.play(valueAnimatorLarge).after(valueAnimatorSmall);
        scaleSet.start();

        //其实可以一个就搞定的
//        ValueAnimator vValue = ValueAnimator.ofFloat(1.0f, 0.6f, 1.2f, 1.0f, 0.6f, 1.2f, 1.0f);
//        vValue.setDuration(1000L);
//        vValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                float scale = (Float) animation.getAnimatedValue();
//                img_babi.setScaleX(scale);
//                img_babi.setScaleY(scale);
//            }
//        });
//        vValue.setInterpolator(new LinearInterpolator());
//        vValue.start();
    }


    //旋转的同时透明度变化
    private void raAnimator() {
        ValueAnimator rValue = ValueAnimator.ofInt(0, 360);
        rValue.setDuration(1000L);
        rValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int rotateValue = (Integer) animation.getAnimatedValue();
                img_babi.setRotation(rotateValue);
                float fractionValue = animation.getAnimatedFraction();
                img_babi.setAlpha(fractionValue);
            }
        });
        rValue.setInterpolator(new DecelerateInterpolator());
        rValue.start();
    }

    //圆形旋转
    protected void circleAnimator() {
        width = ly_root.getWidth();
        height = ly_root.getHeight();
        final int R = width / 4;
        ValueAnimator tValue = ValueAnimator.ofFloat(0, (float) (2.0f * Math.PI));
        tValue.setDuration(1000);
        tValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 圆的参数方程 x = R * sin(t) y = R * cos(t)
                float t = (Float) animation.getAnimatedValue();
                int x = (int) (R * Math.sin(t) + width / 2);
                int y = (int) (R * Math.cos(t) + height / 2);
                moveView(img_babi, x, y);
            }
        });
        tValue.setInterpolator(new DecelerateInterpolator());
        tValue.start();
    }


    //透明度变化
    protected  void  alphAnimator(){

        width = ly_root.getWidth();
        height = ly_root.getHeight();


        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(img_babi, "alpha", 1f, 0f, 1f);
        objectAnimator.setDuration(3000);
        objectAnimator.start();

    }
}