首先我很抱歉我的英语。
我正在android上做一个项目,点击ImageView中包含的图像,并根据点击收到的cordenadas x, y在图像中绘制一个圆圈。
我的问题是如何将其推广到所有屏幕尺寸?
进入ImageView的图像将是用CameraView拍摄的照片,
一Mat()
行和列取决于屏幕的大小。
我可以在 genymotion 模拟器(720x1290 Samsung Galaxy S3,320dpi)中使用 320x240 的图像执行此操作。但在 Galaxy Y(屏幕尺寸 320x240)上测试时却不起作用。
遵循代码:
... onCreate
im = (ImageView) findViewById(R.id.imageView1);
final DisplayMetrics dm = getResources().getDisplayMetrics();
params = (LayoutParams) im.getLayoutParams();
params.width = 640;
params.height = 480;
im.setLayoutParams(params);
Mat m = new Mat();
im.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
real_x = (float) (event.getX() * (160f / dm.densityDpi));
real_y = (float) (event.getY() * (160f / dm.densityDpi));
updateMat(real_x,real_y);
}
public void loadMat(){
//ma = new Mat();
InputStream inpT = getResources().openRawResource(R.drawable.image);
try {
m = MainActivity.readInputStreamIntoMat(inpT);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(m, bm);
Log.i("BTM", String.valueOf(bm.getHeight() + " " + bm.getWidth()));
im.setImageBitmap(bm);
}
public void updateMat(float real_x2, float real_y2){
Point b = new Point(real_x2,real_y2);
//if is firstClick -> Load the ImageView image on a Mat
if (first_click) {
loadMat();
first_click= false;
}
Core.circle(m, b, 10, new Scalar(0, 255, 0), 2);
Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(m, bm);
Log.i("BTM", String.valueOf(bm.getHeight() + " " + bm.getWidth()));
im.setImageBitmap(bm);
}
最佳答案
我曾经处理过这个问题。我是这样解决的,每当用户单击一个位置时,我都会找到相对于 imageView 的位置的比率。假设 ImageView 的宽度和高度为 200*200 ,当用户点击 100*200 时,其速率为 0.5,1 。之后对图像使用相同的速率。假设图像为 600*600 ,用户实际点击 600x0.5* 600*1 = 300*600。 编辑:尝试下面的代码。
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_UP){
int[] viewCoords = new int[2];
imageView.getLocationOnScreen(viewCoords);
int touchX = (int) event.getX();
int touchY = (int) event.getY();
int imageViewX = touchX - viewCoords[0]; // X touch coordinate on imageView
int imageViewY = touchY - viewCoords[1]; // Y touch coordinate on imageView
float rateOfXPosition = imageViewX/imageView.getWidth();
float rateOfYPosition = imageViewY/imageView.getHeight();
int xOnImage = (int)rateOfXPosition*image.getWidth();
int yOnImage = (int)rateOfYPosition*image.getHeight();
}
}
关于java - 通过单击 ImageView 获取图像上的 x 和 y 点对应位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32030628/