10

我需要我的列表视图来隐藏和显示使用替代触摸。因此,为了隐藏屏幕左侧的列表视图,我正在使用动画

 Animation animation = new TranslateAnimation(-100, 0,0, 0);
                            animation.setDuration(100);
                            animation.setFillAfter(true);
                            lv.startAnimation(animation);
                            lv.setVisibility(0);

并用于显示我正在使用

lv.setVisibility(View.VISIBLE);

我的问题是列表视图没有隐藏。它会向左走,然后又回来。我不知道如何在触摸时将列表视图完全隐藏到左边缘。请帮助实现这一目标

4

4 回答 4

22
// To animate view slide out from left to right
public void slideToRight(View view){
TranslateAnimation animate = new TranslateAnimation(0,view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}
// To animate view slide out from right to left
public void slideToLeft(View view){
TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}

// To animate view slide out from top to bottom
public void slideToBottom(View view){
TranslateAnimation animate = new TranslateAnimation(0,0,0,view.getHeight());
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}

// To animate view slide out from bottom to top
public void slideToTop(View view){
TranslateAnimation animate = new TranslateAnimation(0,0,0,-view.getHeight());
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);
view.setVisibility(View.GONE);
}
于 2013-09-16T10:08:47.243 回答
4

最后我找到了答案,这是对坐标值的非常简单的修改。代码是

Animation animation = new TranslateAnimation(0,-200,0, 0);
                    animation.setDuration(2000);
                    animation.setFillAfter(true);
                    listView1.startAnimation(animation);
                    listView1.setVisibility(0);

这里我在第二个坐标处设置负值,因为它正在向负侧移动,这意味着视图正在向内左侧移动。

于 2013-09-16T11:33:11.810 回答
3

如果要隐藏视图,请使用

View.INVISIBLE // constant value 4

或者

View.GONE // constant value 8

您当前使用的值 0是 的常量值View.VISIBLE

我想你想在动画后隐藏 ListView 吗?

但是您在启动动画后直接显示 ListView 。看看AnimationListener并隐藏ListView

onAnimationEnd(...)

例如:

// assuming the listview is currently visible
Animation animation = new TranslateAnimation(-100, 0,0, 0);
                            animation.setDuration(100);
                            animation.setFillAfter(true);

animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                     lv.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });

lv.startAnimation(animation);
于 2013-09-16T10:06:34.870 回答
0

对于你不知道的一般理解,我发现你的另一篇文章解释得很好!视图及其动画的工作方式与预期的有所不同!

https://stackoverflow.com/a/5888969/2074990

于 2013-09-16T10:07:44.853 回答