【问题标题】:Couldnt use buttons after using TranslateAnimation?使用 TranslateAnimation 后无法使用按钮?
【发布时间】:2012-01-11 10:41:18
【问题描述】:

我一直在写下面的代码

    Animation animation1= new TranslateAnimation(0,0,0,-250);
    animation1.setDuration(1000);
  animation1.setFillAfter(true);
     rom.startAnimation(animation1);

翻译工作正常,但翻译后我无法使用 clicklistener。 是否可以使用它。 请有人帮帮我?

【问题讨论】:

  • 当你使用动画时,按钮位置在他的第一个位置(不是动画结束后的位置),尝试点击你的按钮的第一个位置,你会看到按钮上的单击事件,所以要解决这个问题,我建议添加一个AnimationListener ,并在onAnimationEnd() 中,尝试更改按钮的位置,使其准确地放置在动画后的最后一个位置,我希望你懂我,对不起我的英语:)
  • 您能告诉我如何更改按钮的位置吗?

标签: android translate-animation


【解决方案1】:

您可以在动画本身上调用 setFillAfter(true),然后再运行它。 这将在动画完成后将新坐标应用于视图。

anim.setFillAfter(true);
viewToAnimate.startAnimation(anim);

如果这不起作用,您可以尝试在动画完成后在视图上设置边距。创建一个 AnimationListener 并将其设置为 Animation。然后使用 onComplete 方法中的边距更改视图位置。

final View viewToAnimate = ...;
final int finalX = ...;
final int finalY = ...;

anim.setAnimationListener(new AnimationListener()
            {

            public void onAnimationStart(Animation animation)
            { 
            }

            public void onAnimationRepeat(Animation animation)
            {
            }

            public void onAnimationEnd(Animation animation)
            {
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(getLayoutParams());
                lp.setMargins(finalX, finalY, 0, 0);
                viewToAnimate.setLayoutParams(lp);
                viewToAnimate.requestLayout();
            }
        });
viewToAnimate.startAnimation(anim);

以上代码假定您的视图包含在 FrameLayout 中(因此是 FrameLayout.LayoutParams 代码)

您可以尝试的另一件事是在动画完成后使用新坐标再次布局视图。

viewToAnimate.layout(finalX, finalY, finalX + viewWidth, finalY + viewHeight);
viewToAnimate.requestLayout();

尝试这 3 种方法中的每一种,看看哪种方法适合您。 希望 setFillAfter(true) 方法能够工作,因为它是最简单的。

编辑:糟糕——我没有注意到你已经在调用 setFillAfter。看起来这不起作用,所以尝试其他两种方法之一

【讨论】:

  • 抱歉,这是不正确的。动画实际上并没有移动按钮,它仍然停留在其原始坐标,请参阅 SDK。
  • 嗯,是的,这就是我要说的。这就是为什么你必须在动画完成后使用边距来移动视图,或者在动画完成后再次调用视图布局方法。只有这样,视图才会移动。看我上面的答案...
猜你喜欢
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-29
  • 1970-01-01
相关资源
最近更新 更多