如何与 imageView 和 recyclerView 一起制作 videoView 动画?

How to animate videoView along with the imageView and recyclerView?

我想在 imageView 和 recyclerView 上方的 videoView 上使用动画。起初,videoView 的可见性消失了,当我单击 recyclerView 上的项目时,我想让 videoView 可见,动画向下滑动,并且还使用 imageView 的动画来 slideUp 并使它走了。我怎样才能做到这一点。我已经给出了布局前后的快照。

动画前:

动画后:

这是我针对类似情况所做的。在您的布局中,您需要将两个视图一个放在另一个之上(在它们必须出现的确切位置)。在 onCreate 中将全局布局侦听器添加到 videoView / imageView 一次,然后只需更改相应视图的可见性。此代码可以与 imageView 一起重用,但请记住,您不能同时更改两个视图的可见性,因为它们会重叠。也许您需要等待第一个动画结束然后显示下一个。由你决定。

private static final int ANIMATION_DURATION = 350;

private void addVideoViewAnimation(final View videoView) {
    videoView.setTag(videoView.getVisibility());
    videoView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int newVisibility = videoView.getVisibility();
            if ((int) videoView.getTag() != newVisibility) {
                videoView.setTag(newVisibility);

                TranslateAnimation animation;
                if (newVisibility == View.VISIBLE) {
                    animation = new TranslateAnimation(0, 0, -videoView.getHeight(), 0);
                    animation.setInterpolator(new DecelerateInterpolator());
                } else {
                    animation = new TranslateAnimation(0, 0, 0, -videoView.getHeight());
                    animation.setInterpolator(new AccelerateInterpolator());
                }
                animation.setDuration(ANIMATION_DURATION);
                videoView.startAnimation(animation);
            }
        }
    });
}