Android onTouchListener的热点不沿TranslationAnimation移动

Android onTouchListener的热点不沿TranslationAnimation移动,android,android-layout,Android,Android Layout,在运行时,我添加了一个新的ImageView并附加了一个侦听器,如下所示: image = new ImageView(someAppContext()); relativeLayout.addView(image, someLayoutParam); // add onTouch listener to the image view: image.setOnTouchListener(new ImageView.OnTouchListener() { public boolean on

在运行时,我添加了一个新的ImageView并附加了一个侦听器,如下所示:

image = new ImageView(someAppContext());
relativeLayout.addView(image, someLayoutParam);
// add onTouch listener to the image view:
image.setOnTouchListener(new ImageView.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
      makeToast("touched!");
      return false; // tried return true, makes no difference
    }
});

之后,我将TranslateAnimation与
setFillAfter(true)
一起使用,因此ImageView将保持在新位置。但是,如果我触摸ImageView的旧位置,而不是图像可视的新位置,则会触发onTouch()。如何使触控热点与ImageView本身一起移动?我还尝试了使()无效(),但似乎没有帮助。

动画实际上不会移动视图。平移动画仅向其坐标添加偏移

我认为最好的解决方法是清除所有动画,并更新布局参数,以便将实际的ImageView移动到动画的目标

您需要存储翻译的目的地,例如
dest_x,dest_y
,动画完成后,执行以下操作:

    someLayoutParam.leftMargin = dest_x;
    someLayoutParam.topMargin = dest_y;
    yourImageView.clearAnimation(); // resets its position
    yourRelativeLayout.updateViewLayout(yourImageView, someLayoutParam);

这样,图像视图的位置不会改变,但实际视图会移动,因此可触摸区域也会移动。

动画实际上不会移动视图。平移动画仅向其坐标添加偏移

我认为最好的解决方法是清除所有动画,并更新布局参数,以便将实际的ImageView移动到动画的目标

您需要存储翻译的目的地,例如
dest_x,dest_y
,动画完成后,执行以下操作:

    someLayoutParam.leftMargin = dest_x;
    someLayoutParam.topMargin = dest_y;
    yourImageView.clearAnimation(); // resets its position
    yourRelativeLayout.updateViewLayout(yourImageView, someLayoutParam);

这样,图像视图的位置不会改变,但实际视图会移动,因此可触摸区域也会移动。

我只记录log.d(标记“touch”),图像和文本分别获得了触摸事件和单击事件。没错,它们都会被触发。我把重点放在了错误的问题上:它与动画有关。我只记录log.d(标记为“touch”),图像和文本分别获得了touch事件和click事件。你是对的,它们都被触发了。我把重点放在了错误的问题上:它与动画有关。