sagar suri
sagar suri

Reputation: 4731

How to slide a view from right to left?

I am trying to slide a view from right to left whose visibility is GONE on click of a button and reverse on click of another button. I have tried the below solution. But it requires the view to be VISIBLE and it will slide the view from one position to another. I want to have a sliding effect as the navigation drawer does but with a view. How can I achieve that?

<?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);

Upvotes: 0

Views: 738

Answers (1)

ashakirov
ashakirov

Reputation: 12350

Visibility changes should be animated via Transition API which is available in support (androix) package :

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);
}

Here is result:

enter image description here

Here is my answer with more info.

Upvotes: 1

Related Questions