first I'm sorry my English.
I am doing a project on android that gets a click on an image contained in a ImageView, and draws a circle in the image based on the cordenadas x, y received by the click.
My problem is how to generalize this to all screen size?
The image goes to the ImageView will be a picture taken with the CameraView,
one Mat()
with rows and columns depending of the screen's size.
I could do this in genymotion emulator (720x1290 Samsung Galaxy S3, 320dpi) with a image 320x240. But when tested on a Galaxy Y (screen size 320x240) has not worked.
Follows the code:
... 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);
}