Domanda

I would like to display an .jpg image into the screen and add Listener to this image which will can detect the location of tap on that image. I would like to bind XY axis to the image.

Example:

enter image description here

To display an image I should use:

private ImageView imgView;
imgView = (ImageView) findViewById(R.id.imageViewId);
imgView.setImageBitmap(BitmapFactory.decodeFile("pathToImageFile"));

To add listener to the image

iv.setOnClickListener(clickListener);

But how to define that listener which will detect the click coordinates (x,y) but the coordinates should be binded to the image not to the screen.

È stato utile?

Soluzione

    ImgeView.setOnTouchListener(new OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                //Finger placed on screen
                event.getX();  
                event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                //Finger is moving
                event.getX();
                event.getY();
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                //Second finger is touching screen
                break;
            }
            return false;
        }

    });
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top