Douglas Fornaro
Douglas Fornaro

Reputation: 2037

Android - Animation, hiding and showing a menu bar

I have a problem to create an animation. I have a button on the action bar that clicking on it, or displays or hides a menu bar. So far it's displaying or hiding using GONE and VISIBLE. I'd like to add an animation, this menu is just below the action bar, so when I click to hide the menu, I'd like it moved up, hiding it. By clicking to show the menu, I'd like it moved down, showing it. Another problem is that the rest of the layout should follow the movement that is chosen. Does anyone know how to do this? Thank you!

Edit:

public class HideAnimation extends Animation {
    int targetHeight;
    int orgHeight;
    View view;

    public HideAnimation(View view, int targetHeight) {
        this.view = view;
        this.targetHeight = targetHeight;
        orgHeight=view.getHeight();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) ((orgHeight-targetHeight) * (1-interpolatedTime))+targetHeight;
        view.requestLayout();
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
} 

public class ShowAnimation extends Animation {
    int targetHeight;
    View view;

    public ShowAnimation(View view, int targetHeight) {
        this.view = view;
        this.targetHeight = targetHeight;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) (targetHeight * interpolatedTime);
        view.requestLayout();
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

public void collapseControlMenu(boolean showEffect) {
    final View controlScreen = (View) findViewById(R.id.volume_fragment);
    if (controlScreen != null) {
        MainApp.mShowControl = false;
        if (showEffect) {

            /*ObjectAnimator anim = ObjectAnimator.ofFloat(controlScreen, "translationY", -ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height));
            anim.setDuration(500);
            anim.start();
            ObjectAnimator anim2 = ObjectAnimator.ofFloat(mContactFragment.getListView(), "translationY", -ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height));
            anim2.setDuration(500);
            anim2.start();*/

            Animation ani = new HideAnimation(controlScreen, 0);
            ani.setDuration(500);
            controlScreen.startAnimation(ani);

            /*ViewPropertyAnimator anim = controlScreen.animate().translationYBy(0).translationY(-ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height));
            anim.setDuration(500);
            anim.start();
            if (!isContactsFragmentVisible()) { // Recents
                anim = mRecentFragment.getListView().animate();
            } else {
                anim = mContactFragment.getListView().animate();
            }
            anim.translationYBy(0).translationY(-ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height));
            anim.setDuration(500);
            anim.start();*/

        } else {
            controlScreen.setVisibility(View.GONE);
        }
    }
}

public void expandControlMenu(boolean showEffect) {
    final View controlScreen = (View) findViewById(R.id.volume_fragment);
    if (controlScreen != null) {
        MainApp.mShowControl = true;
        setControlOptions();
        if (showEffect) {

            /*ObjectAnimator anim = ObjectAnimator.ofFloat(controlScreen, "translationY", 0);
            anim.setDuration(500);
            anim.start();
            ObjectAnimator anim2 = ObjectAnimator.ofFloat(mContactFragment.getListView(), "translationY", 0);
            anim2.setDuration(500);
            anim2.start();*/

            Animation ani = new ShowAnimation(controlScreen, (int) ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height));
            ani.setDuration(500);
            controlScreen.startAnimation(ani);

            /*ViewPropertyAnimator anim = controlScreen.animate().translationYBy(-ResourcesUtils.getpixels(getApplicationContext(), R.dimen.volume_bar_height)).translationY(0);
            anim.setDuration(500);
            anim.start();
            if (!isContactsFragmentVisible()) { // Recents
                anim = mRecentFragment.getListView().animate();
            } else {
                anim = mContactFragment.getListView().animate();
            }
            anim.translationY(0);
            anim.setDuration(500);
            anim.start();*/

        } else {
            controlScreen.setVisibility(View.VISIBLE);
        }
    }
}

Upvotes: 0

Views: 228

Answers (2)

Kai
Kai

Reputation: 15476

The code below will hide the target view:

public class HideAnimation extends Animation {
    int targetHeight;
    int orgHeight;
    View view;

    public HideAnimation(View view, int targetHeight) {
        this.view = view;
        this.targetHeight = targetHeight;
        orgHeight=view.getHeight();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) ((orgHeight-targetHeight) * (1-interpolatedTime))+targetHeight;
        view.requestLayout();
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
} 

And do this in your code to start the animation:

Animation ani = new HideAnimation(view, 0/* target layout height */);
ani.setDuration(300/* animation time */);
view.startAnimation(ani);

To show the target view again, use the following:

public class ShowAnimation extends Animation {
    int targetHeight;
    View view;

    public ShowAnimation(View view, int targetHeight) {
        this.view = view;
        this.targetHeight = targetHeight;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        view.getLayoutParams().height = (int) (targetHeight * interpolatedTime);
        view.requestLayout();
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

Upvotes: 1

Sahar Avr
Sahar Avr

Reputation: 131

If you want the rest of the layout animate as well, your best option would be ObjectAnimator.

Upvotes: 0

Related Questions