ImageView scaling while maintaining center point in the same place

I set the predefined bitmap as the source of ImageView. Then I read the ImageView Matrix and changed the ImageView bitmap using matrix.postTranslate(shiftX, shiftY) .

Now I want to enlarge / reduce the image and save it while keeping the ImageView center at the same point in the bitmap that was before scaling.

If I try to enlarge the image using matrix.postScale(zoom, zoom) , specify what I want to save (blue dot), shift to another place (purple dot).

I tried several different ways to shift Bitmap back, but I cannot get it to work correctly. I know the initial size of the bitmap image, the size of the ImageView, the distances indicated by the outlined line. I tried to calculate the necessary shift and use matrix.postTranslate(-zoomshiftX, -zoomshiftY) afterwards, but it does not shift correctly.

It even turned out that the basic number of Bitmap pixels does not change after the matrix.postScale () function and checks matrix.postTranslate (-zoomshiftX / zoom, -zoomshiftY / zoom), but still no luck.

How to achieve such an increase?

scalling issue

+2
source share
2 answers

Take a look at my question here regarding creating a scalable ViewGroup. I have described code fragments from my final solution, and some of them may be useful.

Extending RelativeLayout and overriding the Draw () dispatcher to create a scalable ViewGroup

+1
source

Perhaps this may be useful:

If you have two fingers on the screen, you can get the two-finger event and get the midpoint:

 PointF mid; MotionEvent event; float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); mid.set(x / 2, y / 2); 

Then you can set the scaling having a midpoint in the center of the screen:

 matrix.postScale(scale, scale, mid.x, mid.y); 
0
source

All Articles