2013-05-13 47 views
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'進行繪製,但也爲點創建了不正確的位置。謝謝。

回答

0

如果你想在View和圖像座標之間使用MatrixgetImageMatrix()方法。

+0

你的意思是調用ImageView的getImageMatrix()嗎?我嘗試在結果矩陣上使用mapPoints,但這並沒有改變任何東西。 – 2013-05-13 19:35:48

+0

你是說什麼「沒有改變什麼」? – pskink 2013-05-13 19:56:26

+0

路徑的位置仍然不正確。它可能會改變origBitmap和currentBitmap都旋轉90度的東西。這會改變什麼嗎? – 2013-05-13 20:05:01