문제

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.

도움이 되었습니까?

해결책

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

    });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top