【问题标题】:Using Animation to swipe views使用动画滑动视图
【发布时间】:2011-08-24 14:24:29
【问题描述】:

我有一个可以识别滑动手势(向上和向下)的 FrameLayout。

例如:如果执行向上滑动,我应该为当前视图(即 MATCH_PARENT x MATCH_PARENT)设置动画,以在新视图从底部出现的同时上升。

我可以通过动画实现这一点吗?

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    我是这样解决的:

    private void swipeUp() {
        current.currentPage++;
    
        final View hidingView = currentView;
        TranslateAnimation hide = new TranslateAnimation(0, 0, 0, -getHeight());
        hide.setAnimationListener(new AnimationListenerAdapter() {
            @Override
            public void onAnimationEnd(Animation animation) {
                hidingView.setVisibility(View.GONE);
            }
        });
        hide.setDuration(1000);
        hidingView.startAnimation(hide);
    
        TranslateAnimation show = new TranslateAnimation(0, 0, getHeight(), 0);
        show.setFillAfter(true);
        show.setDuration(1000);
    
        View nextView = getView();
        addView(nextView, createLP());
    
        nextView.startAnimation(show);
        currentView = nextView;
    }
    

    【讨论】:

    • 将视图设置为 GONE 并不是让动画正常工作的好方法,但如果它要运行多次,您需要确保确实处理掉了之前的视图。否则,您将面临性能和内存问题的风险,因为 View.GONE 实际上并没有处理视图。
    • 好的,我将使用 removeView 代替。
    • 先做View.GONE,然后动画完成后,remoteView :)
    • 是的,我注意到刚删除时,dispatchDraw 会出错
    【解决方案2】:

    如果你想真正切换视图,你需要实现一个处理动画的AnimationListener。如果您想要更复杂的行为,例如视图之间的“手指跟随”滚动条,您可能必须使用更复杂的东西,但如果您只是说

    if(I flicked upwards)
        move view up
    

    那么 AnimationListener 非常适合您。只需确保在代码中将侦听器设置为 Animation

    希望这会有所帮助!

    【讨论】:

    • 是的,我使用 AnimationListener 来设置 Visibility(View.GONE),但这不是我想要的答案。
    • 你要实际 setContentView()
    猜你喜欢
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 2012-05-11
    • 2019-01-04
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    相关资源
    最近更新 更多