Get half the image bitmap image coordinates from ImageView

I have an ImageView containing a Bitmap image. The image is twice as large as its container. I announced that onScroll() can move the Bitmap image. How can I get the coordinates of ImageView to 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); 
+4
source share
1 answer

I have not found a real way to do this. Here is the method I used:

After creating the ImageView, highlight the known location.

 int ivX = 0; int ivY = 0; _iv.invalidate(); _iv.scrollTo(ivX, ivY); 

So I have the exact (x, y) coordinates where I am. Then I applied the onScroll () method and used the generated distances to recalculate the coordinates (x, y):

 @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 better understand how scrollTo() works, and the relationship between the coordinates of the image and its container, follow this link in another post of mine.

0
source

All Articles