繁体   English   中英

Android Activity MotionEvent坐标到ImageView画布

[英]Android Activity MotionEvent coordinates to ImageView Canvas

我想通过'onTouchEvent'捕获触摸事件的坐标。 现在,在我的活动中,我具有以下代码可以做到这一点:

public boolean onTouchEvent(MotionEvent event) {
    try {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
                Point p = new Point();
                p.set((int) event.getRawX(), (int) event.getRawY());

                                    // The point is added to a list here

                    return true;
        }

        updateDraw();
    } 
    catch (Exception e) {
        Log.wtf("Error", e.getMessage());
    }

    return false;
}

活动的XML布局如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context=".MarkLinesActivity" >

<ImageView
    android:id="@+id/handView"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:scaleType="fitXY" />

现在,在updateDraw()函数中出现了实际的问题。 因为我尝试通过“ onTouchEvent”捕获3个坐标,然后使用“ Path”绘制它们。 不幸的是,这些点始终位于错误的位置。 这是updateDraw()内部的内容:

Canvas tempCanvas = new Canvas(currentBitmap);

tempCanvas.drawBitmap(origBitmap, 0, 0, null);

tempCanvas.drawPath(p, pa);

这是我创建路径坐标的方式:

p.moveTo(points.get(currentLineState).get(0).x,    points.get(currentLineState).get(i).y);

for (int i = 1; i < 3; i++) {
try {
    p.lineTo( (points.get(currentLineState).get(i).x),
                      (points.get(currentLineState).get(i).y) );
        } catch (Exception e) {
        }
}
}

注意:点被定义为HashMap<String, ArrayList<Point>>并且p是Path实例。

updateDraw()的最后一部分是在ImageView上绘制画布创建的内容:

((ImageView) findViewById(R.id.handView)).setImageBitmap(currentBitmap);

现在我的问题是:获取实际坐标的方式是否有问题(我也尝试使用event.getX()等等),或者与XML有关? 我也尝试自己从ImageView扩展一个类,并通过覆盖'onDraw'进行绘制,但这也为这些点创建了不正确的位置。 谢谢。

如果要在View和图像坐标之间进行映射,请使用Matrix getImageMatrix()方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM