2

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?

XTL
  • 1,452
  • 2
  • 17
  • 40
  • Did you see [this](http://stackoverflow.com/questions/4933612/how-to-convert-coordinates-of-the-image-view-to-the-coordinates-of-the-bitmap)? – Poger Oct 28 '15 at 12:41
  • @Poger yes,i'm trying now to implement. Do you have anothers ideas? :) – XTL Oct 28 '15 at 12:43
  • see getImageMatrix and read Matrix docs – pskink Oct 28 '15 at 12:49
  • @Poger for some reason(dont know why),i got same values on intrinsicHeight/Width and ScaledHeight/Width – XTL Oct 28 '15 at 13:05
  • @pskink Okay,ill do. thanks – XTL Oct 28 '15 at 13:05
  • @VetaLio dont use accepted answer from that link, use the answer with `Matrix` (the second one, by `akonsu`) – pskink Oct 28 '15 at 19:36
  • @pskink i tried also,same coords as simple GetX() :( – XTL Oct 28 '15 at 20:29
  • 1
    see [this](http://pastebin.com/MvBTTRpu) – pskink Oct 28 '15 at 21:10
  • @pskink seems that your are right(coords that i see in logs(w/ inverse of matrix) and coords that i get w/o invert matrix are different). But strange why my perspective doesnt work. – XTL Oct 28 '15 at 22:24

1 Answers1

7

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);
pskink
  • 23,874
  • 6
  • 66
  • 77