0
我想通过'onTouchEvent'捕捉触摸事件的坐标。现在,在我的活动我有以下的代码来做到这一点:Android Activity MotionEvent坐标到ImageView Canvas
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个坐标,并使用'路径'绘制它们。不幸的是,积分总是处于不正确的位置。这里面是什么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是一个路径实例。
updateDraw的最后一部分()被绘制由画布上的ImageView创建的内容:
((ImageView) findViewById(R.id.handView)).setImageBitmap(currentBitmap);
现在我的问题是:是否有什么问题得到实际的坐标的方式(我也尝试过使用event.getX()等)或者可能与XML的东西?我也尝试从ImageView自己扩展一个类,并通过覆盖'onDraw'进行绘制,但也为点创建了不正确的位置。谢谢。
你的意思是调用ImageView的getImageMatrix()吗?我尝试在结果矩阵上使用mapPoints,但这并没有改变任何东西。 – 2013-05-13 19:35:48
你是说什么“没有改变什么”? – pskink 2013-05-13 19:56:26
路径的位置仍然不正确。它可能会改变origBitmap和currentBitmap都旋转90度的东西。这会改变什么吗? – 2013-05-13 20:05:01