Android:画布翻译后单击/触摸事件不起作用
我有一个包含多个 ImageView 的 FrameLayout。在主要活动中,我记录触摸事件,以便用手指(拖动)移动我的 FrameLayout 和内部图像。
为此,我在框架布局的 onDraw 内部调用 canvas.translate(x,y),该框架布局由活动触摸事件处理程序中的 invalidate() 调用。
一切都像一个魅力,除了翻译后,我无法点击我的 ImageView。事实上,每个图像的点击监听器仍然在翻译之前的原始位置。
我读过,我应该在翻译后手动更新每个图像的布局,但如何做到这一点?如果我用翻译值更改边距,图像会进一步移动两倍......
我真的很感激对此的任何帮助。
干杯。
这是我在 onDraw() 方法中平移画布的frameLayout(ImageView 添加到我的主 Activity 中的 FrameLayout)。
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在构造函数中使用 setPadding(this.mPosX,this.mPosY,0,0) 。它应该有效。
You can use setPadding(this.mPosX,this.mPosY,0,0) in the the constructor. It should work.