0

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);
}
  • 2
    possible duplicate of [Android: How do I get the x y coordinates within an image / ImageView?](http://stackoverflow.com/questions/8909835/android-how-do-i-get-the-x-y-coordinates-within-an-image-imageview) –  Aug 16 '15 at 00:23

2 Answers2

1

I used to handle this problem.I did the solution in this way , whenever user clicks a position , i find the rate of position relative to imageView. Lets say ImageView's width and height is 200*200 , when user clicks on 100*200 , its rate is 0.5,1 . After that use same rate on image. Lets say Image is 600*600 , user actually clicks 600x0.5* 600*1 = 300*600. Edit: Try this code below.

 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();
        }

    }
Abdullah Tellioglu
  • 1,434
  • 1
  • 10
  • 26
  • Thanks for listening. So, considering that my image completely fills the ImageView (this already by default) and the ImageView size is: `imageView.width = (int) (screen_width * 0.8);` and`imageView.height = (int) (screen_heigh * 0.8);` How I can get the click rate and converts it to the corresponding pixel in the image (Point (x, y))? I'm sorry, I'm unable to see it in code. – João Luiz Aug 16 '15 at 04:10
  • http://stackoverflow.com/questions/11312128/get-the-touch-position-inside-the-imageview-in-android – Abdullah Tellioglu Aug 16 '15 at 11:00
0

getX() and getY() do not always return "absolute" values (or should I say relative to the clicked view). Sometimes they return a delta from the previous touch event. I suggest using getRawX() and getRawY()

See this question/answer

The difference is that getX/Y returns coordinates relative to the view (when not incremental), while the getRawX/Y pair returns the screen coordinates. If you need delta from view coordinates you can use View.getLocationOnScreen and use the values to offset getRawX/Y.

Community
  • 1
  • 1
N.T.
  • 2,601
  • 1
  • 14
  • 20