XTL
XTL

Reputation: 1452

How to take real coordinates(X,Y) of Bitmap from imageView Android

i have little problem.
I'm getting an image,converting to Bitmap and fill imageview with this Bitmap.

So the problem is,how to take real coordinates(X,Y) of this Bitmap?
For better illustration, i attached an image:
What i have
As shown on picture, for e.g. i got an image with Custom Resolution,where user can make an perspective , via yellow points.
So i need to know,how to take real coords of bitmap from ImageView.

What i've made at current moment(but this approach isn't correct):
i take width/height from imageView and recreating new bitmap with this resolution.

Bitmap bt = Bitmap.CreateScaledBitmap(BitmapFactory.DecodeResource(Activity.Resources, Resource.Drawable.test),800,1420,false); //800 width 1420 height
                imgView.SetImageBitmap(bt);

Via implementation of TouchListener(methods GetX()/GetY()),i get coords of points(yellow circle's that overlayed on Bitmap), and the problem is that these coordinates not correct.

Also its interesting for me case when i'm stretching bitmap via ScaleType.FitXY on imageView(its possible to take real coords of bitmap) ?

So how can i achieve my goal?

Upvotes: 2

Views: 6527

Answers (1)

pskink
pskink

Reputation: 24740

use ImageView#getImageMatrix, something like this:

    final ImageView iv = new ImageView(this);
    setContentView(iv);
    // setup your image here by 
    // calling for example iv.setImageBitmap()
    // or iv.setImageDrawable()
    // or iv.setImageResource()
    View.OnTouchListener otl = new View.OnTouchListener() {
        Matrix inverse = new Matrix();
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            iv.getImageMatrix().invert(inverse);
            float[] pts = {
                    event.getX(), event.getY()
            };
            inverse.mapPoints(pts);
            Log.d(TAG, "onTouch x: " + Math.floor(pts[0]) + ", y: " + Math.floor(pts[1]));
            return false;
        }
    };
    iv.setOnTouchListener(otl);

Upvotes: 7

Related Questions