如何获得位图内的X和Y触摸位置 [英] How to get the X and Y touch location inside the Bitmap
问题描述
我有一个应用程序中,它有一个主要的画布,我已经加在它上面的位图的另一个画布。在主画布我有橡皮擦,其中当用户触摸位图区域,将检测。我想知道在哪里等的同时从主画布移动橡皮擦触摸因为位图画布是从主画布不同的位图内的x和y。我想x和主画布的y为相同的位图的帆布被移动。谢谢
下面是我的代码片段
公共无效TOUCH_MOVE(浮法X,浮法Y){
浮DX = Math.abs(X - MX);
浮DY = Math.abs(Y - 我的);
如果(DX> = || TOUCH_TOLERANCE DY> = TOUCH_TOLERANCE){
mPath.quadTo(MX,MY,(X + MX)/ 2,(Y + MY)/ 2);
MX = X;
我= Y;
}
如果(istouchingBitmap(X,Y)== TRUE){
浮动xRatio =(浮点)bitmap.getWidth()/ parent.getWidth();
浮动yRatio =(浮点)bitmap.getHeight()/ parent.getHeight();
浮XPOS = lastX * xRatio;
浮yPos = lastY * yRatio;
eraseBitmap(位图,XPOS,yPos,5);
}
}
功能,探测时,位图触摸
/ **
*
* @参数X中的光标在主视图的x位置。
* @参数y中的光标在主视图的Y位置。
返回:这仅用于当光标正在触摸的位图图像来检测。
* /
公共布尔istouchingBitmap(浮法X,浮法Y){
matrix.invert(反向);
浮动[] PTS = {X,Y};
inverse.mapPoints(PTS);
如果(!bounds.contains(PTS [0],PTS [1])){
返回false;
}
//复制到的位置
lastX = X;
lastY = Y;
返回Color.alpha(bitmap.getPixel((int)的PTS [0],(int)的PTS [1]))!= 0;
}
您只需在触摸事件得到X和Y值。
然后sustract顶部,留有余量/分别偏移值。
的结果是在X,Y坐标相对于所述位图的开始
PD:做这件事时,我在处理状态栏的高度问题,因为它测量Android版本和设备型号之间改变的方式。
希望这有助于。
I have an app in which it has a main canvas and i have added another canvas of bitmap on top of it. In the main canvas I have eraser in which it will detect when the user touches the bitmap area. I want to know the x and y inside the bitmap where the eraser touches and so on while moving from main canvas since the bitmap canvas is different from the main canvas. I want the x and y of the main canvas be the same with the bitmap canvas to be move. Thanks
Here's my snippet
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);
}
}
function to detect the bitmap when touch
/**
*
* @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;
}
You only need to get the X and Y values from the touch event.
Then sustract the top,left margin/offset values respectively.
The result is the X,Y coordinate relative to the start of the bitmap.
PD: When doing this, i was having problems handling the status bar height, since the way of measuring it changes between Android version and device model.
Hope this helps.
这篇关于如何获得位图内的X和Y触摸位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!