【问题标题】:Translation animation for hiding View隐藏视图的翻译动画
【发布时间】:2013-09-16 19:33:11
【问题描述】:

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

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

用于显示 am 使用

lv.setVisibility(View.VISIBLE);

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

【问题讨论】:

    标签: android android-listview android-animation android-view


    【解决方案1】:
    // 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);
    }
    

    【讨论】:

    • 我想要的是我需要我的视图从左侧隐藏到左侧内部,因此我声明了负值。这是从一端移动到另一端。
    【解决方案2】:

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

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

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

    【讨论】:

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

      【讨论】:

      • 视图没有完全隐藏,而是回来了。我需要它完全隐藏在内部左侧。
      • 那你需要增加动画的距离。可能将其设置为显示器的宽度。
      【解决方案4】:

      为了大致了解您不知道的内容,我为您找到了另一篇解释得很好的帖子!视图及其动画的工作方式与预期的有所不同!

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

      【讨论】:

      • 关于填充。我需要隐藏触摸视图
      猜你喜欢
      • 2015-08-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      相关资源
      最近更新 更多