android - ImageButton 在 TranslateAnimation 之后移动后的 OnClickListener 问题

标签 android onclicklistener

我应该怎么做才能将 imageButton 移动到新位置并保留 OnClick 事件?

我有两个问题:

  1. 如果我使用 fillAfter(true),onclick 将不会带到新的地方。
  2. 如果使用 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/

相关文章:

android - 在哪里调用 notifyDataSetChanged

android - view listeners 和 network callbacks 会不会导致内存泄露问题?

Android - Recycler View onClick 监听器不工作

javascript - input-group-addon 使用 onclick 更改颜色

java - 将 Onclick() 添加到 ArrayList 中的项目

java - 按钮点击无 react

java - 有没有办法以编程方式确定上次打开应用程序的时间?

android - 是否可以将 iOS db 文件用于 Android 项目?

java.io.IOException : HTTP request failed, HTTP 状态:500 在 android 中使用 WCF for android

android - ListView 在 onCreate 中两次添加项目