Android - 动画,隐藏和显示菜单栏

时间:2015-05-12 23:50:58

标签: android animation

创建动画时遇到问题。我在操作栏上有一个单击它的按钮,或者显示或隐藏菜单栏。到目前为止,它使用GONE和VISIBLE显示或隐藏。我想添加一个动画,这个菜单就在操作栏的下方,所以当我点击隐藏菜单时,我希望它向上移动,隐藏它。通过单击显示菜单,我希望它向下移动,显示它。另一个问题是布局的其余部分应遵循所选择的移动。有谁知道如何做到这一点?谢谢!

修改

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

2 个答案:

答案 0 :(得分:1)

以下代码将隐藏目标视图:

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

在代码中执行此操作以启动动画:

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

要再次显示目标视图,请使用以下命令:

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

答案 1 :(得分:0)

如果您希望布局的其余部分也有动画效果,那么您最好的选择是ObjectAnimator

相关问题