How to scale (scale and move) the x, y coordinates of a bitmap in PhotoVIEW?

I am using lib https://github.com/chrisbanes/PhotoView

At the beginning I have a bitmap and x, y point to a bitmap.

How do I scale and scale an image to the x, y coordinates of a bitmap?

int bitmapWidth = bitmap.getWidth();//2418
int bitmapHeight = bitmap.getHeight();//1889
float x = 1209f;
float y = 944f;

float targetScale = 4f;    
attacher.setScale(targetScale, x,y, false);//it doesnt work

      

scale = 1f enter image description here

scale = 4f and I want to programmatically move the center in x, y coordinates enter image description here

+2


source to share


1 answer


I faced the same problem and finally did it.

So I tried a lot of things to achieve this. First of all you need to convert your coordinates to PhotoView

:

int bitmapWidth = bitmap.getWidth();//2418
int bitmapHeight = bitmap.getHeight();//1889
float x = 1209f;
float y = 944f;

float focalX = x*photoView.getRight()/bitmap.getWidth();
float focalY = y*photoView.getBottom()/bitmap.getHeight();

float targetScale = 4f;
attacher.setScale(targetScale, focalX, focalY, false);

      

Source: GitHub aslansari

This answer works for the zoom part. To keep some of the programmed motion of your center of the object in the center of the screen when zoomed in, I did this.

var event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, focalX, focalY, 0)
mPhotoView.dispatchTouchEvent(event)

val centerImageX = showResultCoinDetectionPhotoView.displayRect.centerX()
val centerImageY = showResultCoinDetectionPhotoView.displayRect.centerY()

var event = event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, centerImageX, centerImageY, 0)
mPhotoView.dispatchTouchEvent(event)

      

If you don't need scale animation, you don't need any more.

If you want to use animation

I figured out the problem, but can't fix it yet. Here are my thoughts.

This doesn't work because it calls the animation on a different thread.

//PhotoViewAttacher.java
mImageView.post(new AnimatedZoomRunnable(getScale(), scale, focalX, focalY));

      



And at the same time, I tried to simulate motion to re-center the point in the center of the screen.

var event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, focalX, focalY, 0)
mPhotoView.dispatchTouchEvent(event)

val centerImageX = showResultCoinDetectionPhotoView.displayRect.centerX()
val centerImageY = showResultCoinDetectionPhotoView.displayRect.centerY()

var event = event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, centerImageX, centerImageY, 0)
mPhotoView.dispatchTouchEvent(event)

      

But when this part of the code is done, the scaling is not complete, so no movement can occur because the image is full size and we cannot drag the full size image.

So to decide that I am removing the animation.

mPhotoView.setScale(5f, focalX, focalY, false)

      

Another solution might be to use a callback or coroutine to scale and do the move at the same time, because we'll be in a sequential piece of code. But it AnimatedZoomRunnable

's in a different thread, so we'll go up to:

if (animate) {
       mImageView.post(new AnimatedZoomRunnable(getScale(), scale, focalX, focalY));
}

      

And go back to the next line of the coroutine.

So I really don't have any more options to do it. Maybe place a button so that you can center the point in the center of the screen, like in GoogleMap, and do a "fake drag" when the user clicks?

Or is there a way to get a completion notification AnimatedZoomRunnable

?

Let me know if you find anything interesting.

0


source







All Articles