【问题标题】:TranslateAnimation not work at the first timeTranslateAnimation 第一次不起作用
【发布时间】:2018-10-25 06:07:23
【问题描述】:

我想通过点击它的行来显示/从右到左/从左到右动画的按钮, 这是我将其设置为可见的代码:

button.setVisibility(View.VISIBLE);
TranslateAnimation animate = new TranslateAnimation(button.getWidth(),0,0,0);
animate.setDuration(500);
animate.setFillAfter(true);
button.startAnimation(animate);

一去不复返:

TranslateAnimation animate = new TranslateAnimation(0,button.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
button.startAnimation(animate);

默认情况下,按钮已消失。第一次,当我单击该行时,按钮变得可见但没有动画。之后它可以正常工作。为什么第一次动画不起作用??

【问题讨论】:

    标签: android animation visibility


    【解决方案1】:

    第一次你可以使用延迟来工作

           TranslateAnimation animate1 = new TranslateAnimation(
                        0,                 // fromXDelta
                        0,                 // toXDelta
                        0,  // fromYDelta
                        binding.saveCardview.getMeasuredHeight());                // toYDelta
                    animate1.setDuration(500);
                    animate1.setFillAfter(true);
                    binding.saveCardview.startAnimation(animate1);
    
                    final Handler handler = new Handler(Looper.getMainLooper());
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //Do something after 100ms
                            binding.saveCardview.setVisibility(View.GONE);
                        }
                    }, 500);
    

    【讨论】:

      【解决方案2】:

      在开始动画之前,调用button.setY(button.getWidth())设置它的位置。

      【讨论】:

      • 比以前更糟糕:(
      • 我需要您的包含按钮的布局代码,以查看您如何放置它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-31
      • 1970-01-01
      相关资源
      最近更新 更多