如何从右向左滑动视图?

How to slide a view from right to left?

我正在尝试从右向左滑动一个视图,其可见性在单击一个按钮时为 GONE 并在单击另一个按钮时反转。我尝试了以下解决方案。但它要求视图为 VISIBLE 并且它将 view 从一个位置滑动到另一个位置。我想像 navigation drawer 一样有滑动效果,但有 view。我怎样才能做到这一点?

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

   <translate
        android:fromXDelta="0%p"
        android:toXDelta="75%p"
        android:duration="800" />
</set>

imageView = (ImageView) findViewById(R.id.img);

// Load the animation like this
animSlide = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.slide);

// Start the animation like this
imageView.startAnimation(animSlide);

应通过支持 (android) 包中提供的 Transition API 动画显示可见性更改:

private void toggle() {
    View imageView = findViewById(R.id.imageView);
    ViewGroup parent = findViewById(R.id.parent);

    Transition transition = new Slide(Gravity.LEFT);
    transition.setDuration(600);
    transition.addTarget(R.id.imageView);

    TransitionManager.beginDelayedTransition(parent, transition);
    imageView.setVisibility(show ? View.VISIBLE : View.GONE);
}

这是结果:

这里 my answer 包含更多信息。