onTouch 事件坐标和 imageview 绘制坐标不匹配
onTouch event coordinates and imageview draw coordinates mismatch
我有:
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//Choose which motion action has been performed
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Get X, Y coordinates from the ImageView
int X = (int) event.getX();
int Y = (int) event.getY();
和
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lithuania,myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(40, 160, 15, paint);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
知道如何在这些坐标之间建立关系吗?因为现在我的 onTouch 坐标和绘图坐标必须不同才能正确地向用户表示。
在这里搜索过,但似乎没有人有正确答案。任何帮助将不胜感激:)
编辑
float xRatio = (float)bitmap.getWidth() / imageView.getWidth();
float xPos = event.getX() * xRatio;
float yPos = event.getY() * xRatio;
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(xPos, yPos, 15, paint);
像魅力一样工作!
我已经测试了您的代码,问题是您的位图与 imageView 的尺寸不同。您需要确定尺寸差异并进行相应调整。例如,我使用的图像是 1800x1800 而我的 imageView 是 900x900 所以我必须将 X 和 Y 坐标乘以 2 才能获得正确的位置
int xRatio = bitmap.getWidth() / imageView.getWidth();
int xPos = event.getX() * xRatio;
这是一个更强大的版本,它考虑了比目标视图小的位图:
int xRatio, yRatio;
int xPos, yPos;
if (mutableBitmap.getWidth() >= ib.getWidth()) {
xRatio = mutableBitmap.getWidth() / ib.getWidth();
xPos = (int) Math.floor(xRatio * event.getX());
} else {
xRatio = ib.getWidth() / mutableBitmap.getWidth();
xPos = (int) Math.floor(xRatio*event.getX()-((ib.getWidth() - mutableBitmap.getWidth())/2));
}
if (mutableBitmap.getHeight() >= ib.getHeight()) {
yRatio = mutableBitmap.getHeight() / ib.getHeight();
yPos = (int)Math.floor(event.getY()*yRatio);
} else {
yRatio = ib.getHeight() / mutableBitmap.getHeight();
yPos = (int) Math.floor(yRatio*event.getY()-((ib.getHeight() - mutableBitmap.getHeight())/2));
}
我有:
imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//Choose which motion action has been performed
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Get X, Y coordinates from the ImageView
int X = (int) event.getX();
int Y = (int) event.getY();
和
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.lithuania,myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(40, 160, 15, paint);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(mutableBitmap);
知道如何在这些坐标之间建立关系吗?因为现在我的 onTouch 坐标和绘图坐标必须不同才能正确地向用户表示。
在这里搜索过,但似乎没有人有正确答案。任何帮助将不胜感激:)
编辑
float xRatio = (float)bitmap.getWidth() / imageView.getWidth();
float xPos = event.getX() * xRatio;
float yPos = event.getY() * xRatio;
Canvas canvas = new Canvas(mutableBitmap);
canvas.drawCircle(xPos, yPos, 15, paint);
像魅力一样工作!
我已经测试了您的代码,问题是您的位图与 imageView 的尺寸不同。您需要确定尺寸差异并进行相应调整。例如,我使用的图像是 1800x1800 而我的 imageView 是 900x900 所以我必须将 X 和 Y 坐标乘以 2 才能获得正确的位置
int xRatio = bitmap.getWidth() / imageView.getWidth();
int xPos = event.getX() * xRatio;
这是一个更强大的版本,它考虑了比目标视图小的位图:
int xRatio, yRatio;
int xPos, yPos;
if (mutableBitmap.getWidth() >= ib.getWidth()) {
xRatio = mutableBitmap.getWidth() / ib.getWidth();
xPos = (int) Math.floor(xRatio * event.getX());
} else {
xRatio = ib.getWidth() / mutableBitmap.getWidth();
xPos = (int) Math.floor(xRatio*event.getX()-((ib.getWidth() - mutableBitmap.getWidth())/2));
}
if (mutableBitmap.getHeight() >= ib.getHeight()) {
yRatio = mutableBitmap.getHeight() / ib.getHeight();
yPos = (int)Math.floor(event.getY()*yRatio);
} else {
yRatio = ib.getHeight() / mutableBitmap.getHeight();
yPos = (int) Math.floor(yRatio*event.getY()-((ib.getHeight() - mutableBitmap.getHeight())/2));
}