Lonergan6275
Lonergan6275

Reputation: 2038

Android get Position of Bitmap in ImageView

I am using CropView which extends an Android ImageView. It allows the image with in the the view to be scaled and moved around. there does not appear to be any public reference to the image position or scale. Is there a way to get the position of an image relative to a regular ImageView that I can try.

I have also tried setting a touch listener on the CropView that simply prints the x & y positions of the touch but i don see how i can use these to move get the updated position of the image.

mCropView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d(TAG, "///" + event.getX() +  " __ " + event.getY());
        return false;
    }
});

enter image description here

Upvotes: 5

Views: 4374

Answers (3)

user1135300
user1135300

Reputation:

https://github.com/lyft/scissors/blob/master/scissors/src/main/java/com/lyft/android/scissors/CropView.java

The CropView itself is the viewport. Cropview has two methods getViewportWidth and getViewportHeight. You'll need them.

The image that is scaled, moved, cropped etc is a Drawable. Drawable has a getBounds method that will return a Rect. You can get the top, left width and height from this Rect but you need to get the Drawable first.

CropView calls this Drawable "bitmap". You can get it calling the CropView's getImageBitmap() method.

Once you have it, call getBounds which gives you the image's Rect.

To get the x and y you want you'll have to do a bit of math with the Rect's top, left, width and height along with the viewport's height and width which you'll get from CropView's getViewportHeight and getViewportWidth methods.

Simply:

vpwidth=cropview.getViewPortWidth();
vpheight=cropview.getViewPortHeight();
imagetop=cropview.getImageBitmap().getBounds().top;
imageleft=cropview.getImageBitmap().getBounds().left;
imageheight=cropview.getImageBitmap().getBounds().height;
imagewidth=cropview.getImageBitmap().getBounds().width;

>math here

Upvotes: 1

MonkeyDroid
MonkeyDroid

Reputation: 637

I suggest to rely on pure xml and add to the Imagevie:

android:scaleType="centerCrop"

this will lead to the desired output as drawn.

Upvotes: 0

Zumry Mohamed
Zumry Mohamed

Reputation: 9558

You could try out the method below,

    final float[] getPointOfTouchedCordinate(ImageView view, MotionEvent e) {

        final int index = e.getActionIndex();
        final float[] coords = new float[] { e.getX(index), e.getY(index) };
        Matrix m = new Matrix();
        view.getImageMatrix().invert(m);
        m.postTranslate(view.getScrollX(), view.getScrollY());
        m.mapPoints(coords);
        return coords;

}

you can get the value

mCropView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float[] points = getPointOfTouchedCordinate(v, event);
        Log.d(TAG, "///" + points[0] +  " __ " + points(1));
        return false;
    }
});

Upvotes: 0

Related Questions