Java Android活动MotionEvent坐标到ImageView画布
我想通过“onTouchEvent”捕捉触摸事件的坐标。现在,在我的活动中,我有以下代码来实现这一点:Java Android活动MotionEvent坐标到ImageView画布,java,android,drawing,android-canvas,Java,Android,Drawing,Android Canvas,我想通过“onTouchEvent”捕捉触摸事件的坐标。现在,在我的活动中,我有以下代码来实现这一点: public boolean onTouchEvent(MotionEvent event) { try { if (event.getAction() == MotionEvent.ACTION_DOWN) { Point p = new Point(); p.set((int) event.getRaw
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" />
以下是创建路径坐标的方法:
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) {
}
}
}
现在我的问题是:获取实际坐标的方法(我还尝试使用event.getX()等)是否有问题,或者XML是否有问题?我还尝试从ImageView扩展一个类,并通过重写“onDraw”进行绘制,但这也为点创建了错误的位置。谢谢。如果要在
视图
和图像坐标之间映射,请使用矩阵
getImageMatrix()
方法。您的意思是调用ImageView的getImageMatrix()?我尝试在生成的矩阵上使用mapPoints,但这没有改变任何东西。你说“这没有改变任何东西”是什么意思?路径的位置仍然不正确。它可能会改变origBitmap和currentBitmap都旋转90度的情况。这会改变什么吗?好的,换句话说,你为什么要这样做?如果您只想在ImageView上绘制,请扩展它并覆盖其onDraw方法。非常感谢!我终于设法让它运行起来了。我使用一个新类扩展了ImageView,并在它的onDraw方法中调用canvas.setMatrix(getImageMatrix())。看来这就成功了!
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) {
}
}
}
((ImageView) findViewById(R.id.handView)).setImageBitmap(currentBitmap);