我有一個應用程序中,它有一個主要的畫布,我已經加在它上面的位圖的其他畫布內。在主畫布中,我有橡皮擦,當用戶觸摸位圖區域時,橡皮擦會檢測到它。由於位圖畫布與主畫布不同,我想知道從主畫布移動時橡皮擦接觸的位圖內部的x和y等。我希望主畫布的x和y與要移動的位圖畫布相同。由於如何獲得X和Y觸摸位置的位圖
這裏是我的代碼段
public void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
if(istouchingBitmap(x, y) == true){
float xRatio = (float)bitmap.getWidth()/parent.getWidth();
float yRatio = (float)bitmap.getHeight()/parent.getHeight();
float xPos = lastX * xRatio;
float yPos = lastY * yRatio;
eraseBitmap(bitmap, xPos , yPos , 5);
}
}
功能,探測位圖時觸摸
/**
*
* @param x The X location of the cursor in main View.
* @param y The Y location of the cursor in main View.
* @return This is only used to detect if the cursor is touching the Bitmap Image.
*/
public boolean istouchingBitmap(float x, float y) {
matrix.invert(inverse);
float[] pts = {x, y};
inverse.mapPoints(pts);
if (!bounds.contains(pts[0], pts[1])) {
return false;
}
// copy the location
lastX = x;
lastY = y;
return Color.alpha(bitmap.getPixel((int) pts[0], (int) pts[1])) != 0;
}
看一看這樣的:http://stackoverflow.com/questions/2447564/detect-touch-on-bitmap – Khuong
你好@ khuong291。我已經更新了這個問題。我已經訪問過該鏈接:) – donmj