How to animate a piece of android to and from screen using support library

I'm trying to show a snippet inside my activity that has a menu and it should slide from the right side of the screen until the user dismiss it by clicking the button again or hitting the back button, then it should slide from where they came from, both actions with animation , sure. The problem is the fragment is sliding, but I can't get it to slide out with animation, it just pops out, disappears. I've read some similar posts but haven't found any working solution for my situation.

Here's the code that should get the job done:

private void showMenuFragment() {

    if (this.frMenu == null) {
        this.frMenu = new MenuFragment();               
        FragmentTransaction transaction = this.getSupportFragmentManager()
                .beginTransaction();                    
        transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right);
        transaction.replace(R.id.fr_side_menu, this.frMenu);
        transaction.addToBackStack(MenuFragment.class.getName());
        transaction.commit();
    }
    else {
        this.frMenu = null;
        this.onBackPressed();
    }       
}

      

animations:

Slide_in_right.xml file

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="500"
        android:fromXDelta="100%"
        android:toXDelta="0%" >
    </translate>

</set>

      

Slide_out_right.xml file

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="0%"
        android:toXDelta="100%" >
    </translate>

</set>

      

Please, help!

Thanks in advance.

+3


source to share


2 answers


Android docs explain, for setCustomAnimations(int enter, int exit)

These animations will not play when the back stack appears.

So, you have to use the longer form , which has additional parameters for popEnter

and popExit

:



transaction.setCustomAnimations(R.anim.slide_in_right, 
                                R.anim.slide_out_right, 
                                R.anim.slide_in_left, 
                                R.anim.slide_out_left);

      

To do it right, you can define slide_in_left

and slide_out_left

, but it depends on what you are going to do ...

+3


source


After some research, I found a very simple solution using a completely different approach, hope it helps someone.

What is happening is an animation inside and outside of the layout containing the information that is inside the fragment, but without the fragment. The layout visibility is initially set to "GONE" and is displayed in response to user action such as scrolling or pressing a button.

Here's a complete example:

1 - User actions processing actions:



public class MainActivity extends Activity {

    private LinearLayout ll;
    private float startX;
    private Animation animLeft;
    private Animation animRight;

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

        ll = (LinearLayout) findViewById(R.id.slider);
        ll.setVisibility(View.GONE);

        animLeft = AnimationUtils.loadAnimation(this, R.anim.anim_left);
        animRight = AnimationUtils.loadAnimation(this, R.anim.anim_right);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            startX = event.getX();
            break;
        }
        case MotionEvent.ACTION_UP: {
            float endX = event.getX();

            if (endX < startX) {
                System.out.println("Move left");
                ll.setVisibility(View.VISIBLE);
                ll.startAnimation(animLeft);
            } 
            else {
                ll.startAnimation(animRight);
                ll.setVisibility(View.GONE);
            }
        }

        }
        return true;
    }

}

      

2 - Layout containing a sliding "menu":

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_bright" >

    <LinearLayout
        android:id="@+id/slider"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:background="@android:color/darker_gray"
        android:content="@+id/content"
        android:gravity="bottom|center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Surviving with android" />
    </LinearLayout>

</RelativeLayout>

      

+1


source







All Articles