Android : click / touch event not working after canvas translate

馋奶兔 提交于 2019-12-11 09:50:48

问题


I have a FrameLayout that contains several ImageView. On the main activity, I record the touch events in order to move my FrameLayout and the images inside with the finger (drag).

For doing so, I am calling canvas.translate(x,y) inside the onDraw of the framelayout which is called by a invalidate() in the activity touch event handler.

Everything works like a charm except that after the translate, I am not able to click on my ImageView. In fact, the click listener of each image is still at the original place before the translate.

I have read that I should manually update the layout of each image after the translate but how to do that ? If I change the margin with the translate value, the images are going two times further ...

I would really appreciate any help on that one.

Cheers.

Here is the frameLayout where I translate the canvas in the onDraw() method (the ImageView are added to that FrameLayout in my main Activity).

public class TopView extends FrameLayout {

public float mPosX = 0;
public float mPosY = 0;

public TopView(Context context)
{
    super(context);
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(1920, 3200, Gravity.CENTER);
    this.setLayoutParams(lp);
    setWillNotDraw(false);
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.translate(this.mPosX, this.mPosY);

}

}

回答1:


You can use setPadding(this.mPosX,this.mPosY,0,0) in the the constructor. It should work.



来源:https://stackoverflow.com/questions/6851322/android-click-touch-event-not-working-after-canvas-translate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!