【问题标题】:Android - Animation, hiding and showing a menu barAndroid - 动画、隐藏和显示菜单栏
【发布时间】:2015-05-14 00:29:34
【问题描述】:

我在创建动画时遇到问题。我在操作栏上有一个按钮,可以点击它,或者显示或隐藏菜单栏。到目前为止,它使用 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);
        }
    }
}

【问题讨论】:

    标签: android animation


    【解决方案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;
        }
    }
    

    【讨论】:

    • 我的屏幕有这个菜单栏和一个列表视图。菜单栏向右隐藏,但有时不正确显示,在我单击显示菜单后,它不显示,并且在我滚动列表视图后它显示。你知道为什么吗?
    • @DouglasFornaro 发布一些代码,我会看看我能做什么
    【解决方案2】:

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

    【讨论】:

    • 你有什么例子可以帮忙吗?
    猜你喜欢
    • 2011-10-27
    • 2019-04-24
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    相关资源
    最近更新 更多