我应该怎么做才能将 imageButton 移动到新位置并保留 OnClick 事件?
我有两个问题:
- 如果我使用 fillAfter(true),onclick 将不会带到新的地方。
- 如果使用 setAnimationListener(onAnimationEnd),并调用 layout() 将 ImageButton 移动到新位置,ImageButton 将回到新的开始位置,为什么?
代码在这里:
tsla = new TranslateAnimation(0.0f,(float) (imgWidth * 0.45)-wh/2,0.0f,(float) (imgHeight * 0.566666667)-wh/2);
tsla.setDuration(sleepX);
tsla.setAnimationListener(new AnimationListener(){
public void onAnimationStart(Animation arg0) {
}
public void onAnimationEnd(Animation arg0) {
imgBtnChengdu.layout(
(int) (imgWidth * 0.45),
(int) (imgHeight * 0.566666667),
(int) (imgWidth - wh - imgWidth * 0.45),
(int) (imgHeight - wh - imgHeight * 0.566666667)
);
}
public void onAnimationRepeat(Animation arg0) {
}
});
imgBtnChengdu.setAnimation(tsla);
最佳答案
问题是 Android 只会将按钮的图像动画化到新位置。
这意味着 onClick 区域保留在旧位置。您还需要更改动画末尾按钮的位置,以将整个按钮移动到新位置,而不仅仅是它的 View 。
关于android - ImageButton 在 TranslateAnimation 之后移动后的 OnClickListener 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10137657/