Get coordinates of a Bitmap image from an ImageVie

2020-04-07 20:03发布

I have an ImageView containing a Bitmap image. The image is twice as big as its container. I have declared onScroll() to be able to move around the Bitmap image. How can I get the coordinates of the ImageView on the Bitmap image?

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
_iv.setImageBitmap(bm);
_iv.setAdjustViewBounds(true);
_iv.setMaxHeight(bm.getHeight());
_iv.setMaxWidth(bm.getWidth());
_iv.setScaleType(ImageView.ScaleType.CENTER);

Bitmap newBm = Bitmap.createScaledBitmap(bm, bm.getWidth() * 2, bm.getHeight() * 2, true);
_iv.setImageBitmap(newBm);

1条回答
萌系小妹纸
2楼-- · 2020-04-07 20:19

I haven't found an actual way to do this. Here's the method I've used:

Upon creating the ImageView, scroll to a known location.

int ivX = 0;
int ivY = 0;

_iv.invalidate();
_iv.scrollTo(ivX, ivY);

This way I have the exact (x, y) coordinates of where I am. Then, I've implemented the onScroll() method and used the generated distances to recalculate my (x, y) coordinates:

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    //Add the scroll distance to the old X, Y coordinates
    ivX += distanceX;
    ivY += distanceY;

    //Scroll to the new location
    _iv.scrollTo(ivX, ivY);

    return false;
} //End onScroll()

In addition, to get a better understanding of how scrollTo() works and the relationship between the coordinates of the image and its container, follow this link to a different post of mine.

查看更多
登录 后发表回答