安卓:帆布后,点击翻译/触摸事件不工作 [英] Android : click / touch event not working after canvas translate

查看:133
本文介绍了安卓:帆布后,点击翻译/触摸事件不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个ImageView的一个的FrameLayout。在主要活动,我记录了触摸事件,为了将我的FrameLayout和内侧用手指(拖)的图像。

有关这样做,我打电话canvas.translate(X,Y)的的FrameLayout的OnDraw中被称为由活动触摸事件处理程序无效()内。

一切的工作方式类似,只是经过翻译的魅力,我不能够点击我的ImageView的。事实上,每个图像的点击听者仍然处于以前的翻译原来的地方。

我看过,我应该手动更新后,每幅图像的布局的转换,但如何做到这一点?如果我改变与转换价值的保证金,图像会两次再...

我真的AP preciate在一个任何帮助。

干杯。

下面是的FrameLayout我的翻译在画布上的OnDraw()方法(ImageView的被添加到的FrameLayout在我的主要活动)。

 公共类TOPVIEW扩展的FrameLayout {

公众持股量mPosX = 0;
公众持股量mPosY = 0;

公共冠捷(上下文的背景下)
{
    超(上下文);
    FrameLayout.LayoutParams LP =新FrameLayout.LayoutParams(1920,3200,Gravity.CENTER);
    this.setLayoutParams(LP);
    setWillNotDraw(假);
}

@覆盖
公共无效的OnDraw(帆布油画){
    super.onDraw(画布);
    canvas.translate(this.mPosX,this.mPosY);

}

}
 

解决方案

您可以在构造函数中使用setPadding(this.mPosX,this.mPosY,0,0)。它应该工作。

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);

}

}

解决方案

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

这篇关于安卓:帆布后,点击翻译/触摸事件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆