【问题标题】:After translating a view, the area where you press to trigger a button press doesn't move. How do you move this area with the view?翻译视图后,您按下以触发按钮按下的区域不会移动。你如何随着视图移动这个区域?
【发布时间】:2016-08-21 13:26:25
【问题描述】:

我创建了一个按钮,并希望它在触发某些事件后移动到屏幕底部。所以我做了一个 TranslateAnimation 对象

    private TranslateAnimation setupAnimation(float yOffset) {
    TranslateAnimation animation = new TranslateAnimation(0, 0, 0, yOffset);
    animation.setDuration(1000);
    animation.setFillAfter(true);
    animation.setInterpolator(new AccelerateDecelerateInterpolator());
    return animation;
}

然后我将 TranslateAnimation 对象传递给我想要移动的视图的 startAnimation() 方法。

嗯,这适用于我想要在视觉上完成的任务,但我注意到我无法点击可见的位置,但我可以按下按钮原来所在的位置,onClick 回调将被执行。

我需要做什么,发布翻译,以允许用户在新位置按下按钮?

【问题讨论】:

标签: android android-animation


【解决方案1】:

TranslateAnimation 只移动屏幕上的像素,它不会改变 Button 的实际位置,它只是看起来像在移动,所以 OnClick/OnTouchListener 不会随之产生动画。

使用 ObjectAnimator 或 ViewPropertyAnimator 来真正改变 Button 的属性。

下面是一个使用ViewPropertyAnimator 的示例来帮助您入门:

yourButton.animate()
    .translationY(yOffset)
    .setInterpolator(new AccelerateDecelerateInterpolator())
    .setDuration(1000);

查看Docs 了解其他可用方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多