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

上滑消失下滑显示效果时的动画效果

程序员文章站 2022-05-02 20:06:52
...

写一个动画类

public class AnimatorUtil {

    private static LinearOutSlowInInterpolator FAST_OUT_SLOW_IN_INTERPOLATOR = new LinearOutSlowInInterpolator();

    private static AccelerateInterpolator LINER_INTERPOLATOR = new AccelerateInterpolator();

    //包含缩放和平移

    // 显示view
    public static void scaleShow(View view, ViewPropertyAnimatorListener viewPropertyAnimatorListener) {
        view.setVisibility(View.VISIBLE);
        ViewCompat.animate(view)
                .scaleX(1.0f)
                .scaleY(1.0f)
                .alpha(1.0f)
                .setDuration(800)
                .setListener(viewPropertyAnimatorListener)
                .setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR)
                .start();
    }

    // 隐藏view
    public static void scaleHide(View view, ViewPropertyAnimatorListener viewPropertyAnimatorListener) {
        ViewCompat.animate(view)
                .scaleX(0.0f)
                .scaleY(0.0f)
                .alpha(0.0f)
                .setDuration(800)
                .setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR)
                .setListener(viewPropertyAnimatorListener)
                .start();
    }

    // 显示view
    public static void translateShow(View view, ViewPropertyAnimatorListener viewPropertyAnimatorListener) {
        view.setVisibility(View.VISIBLE);
        ViewCompat.animate(view)
                .translationY(0)
                .setDuration(400)
                .setListener(viewPropertyAnimatorListener)
                .setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR)
                .start();
    }

    // 隐藏view
    public static void translateHide(View view, ViewPropertyAnimatorListener viewPropertyAnimatorListener) {
        view.setVisibility(View.VISIBLE);
        ViewCompat.animate(view)
                .translationY(360)
                .setDuration(400)
                .setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR)
                .setListener(viewPropertyAnimatorListener)
                .start();
    }
}

2.

当想让其隐藏时调用  img代表控件   isclick表示目前是否在显示,用于显示隐藏的判断条件里

AnimatorUtil.translateHide(img, new StateListener() {
    @Override
    public void onAnimationStart(View view) {
        super.onAnimationStart(view);
        isclick = true;   
    }
});

显示时

AnimatorUtil.translateShow(img, new StateListener() {
    @Override
    public void onAnimationStart(View view) {
        super.onAnimationStart(view);
        isclick = false;
    }
});

所用参数  

private boolean isAnimateIng = false;   用于显示隐藏的判断条件里,就是等动画状态

class StateListener implements ViewPropertyAnimatorListener {
    @Override
    public void onAnimationStart(View view) {
        isAnimateIng = true;
    }

    @Override
    public void onAnimationEnd(View view) {
        isAnimateIng = false;
    }

    @Override
    public void onAnimationCancel(View view) {
        isAnimateIng = false;
    }
}