3

I use the lib https://github.com/chrisbanes/PhotoView

At start I have Bitmap and x,y point on the Bitmap.

How to scale and zoom image to x,y coordinates of the 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 programly move center to x,y coordinates enter image description here

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
NickUnuchek
  • 11,794
  • 12
  • 98
  • 138
  • what is that x,y point for? – pskink Feb 24 '18 at 10:52
  • @pskink coordinates of point on bitmap – NickUnuchek Feb 24 '18 at 11:07
  • ok for example its 100 and 200, what do you want to do with that point? you want it to be centered on your view? you want it to be on some corner of your view? maybe post some image describing what you want to achieve? – pskink Feb 24 '18 at 11:15
  • @pskink right, I want to scale and move to be centered on view at this x,y point – NickUnuchek Feb 24 '18 at 11:21
  • @pskink updated question, on first `scale = 1f`, at second `scale = 4f` and I want programly move center to x,y coordinates – NickUnuchek Feb 24 '18 at 11:26
  • Hi, i see your post here https://github.com/chrisbanes/PhotoView/issues/585 the answer @aslansari is correct but did you have found anything on how to move the center of the zoomed area ? – Zenocode Jul 22 '19 at 14:34

3 Answers3

1

I was facing the same issue, and finally made it.

So to achieve that I tried many things. First of all you need to convert your coordinates to the 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. For the part of the programmatically movement of your object center to be on the screen center when zoomed in I made 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 doesn't need the animation on the scale you doesn't need more.

If you want to use the animation

I figured the issue, but can't fix it yet. Here is my thoughts.

That doesn't work because it call the animation in another thread.

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

And in the same time I tried to simulate the movement to re-center the point int 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 get executed the scale isn't finished, so the movement can't happened cause the image is full sized and we can't drag a full size image.

So to resolve that I remove the animation.

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

An other solution could be use a callback or coroutine for the scaling and in the same time do the move, cause we will be in a sequential code part. But the AnimatedZoomRunnable is in another thread so we will go till the :

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

And go back to the next line of the coroutine.

So actually I don't have more option to do that. Maybe place a button to be able to center the point in the middle of the screen like in GoogleMap and execute the "simulating drag" when user click ?

Or does there is a way to get notify when AnimatedZoomRunnable is finished ?

Let me know if you found something interesting.

Zenocode
  • 656
  • 7
  • 19
0

I found a more simple solution. It is based on dispatchTouchEvent() method.

public class YourActivity extends AppCompatActivity {

    private float mTapX;
    private float mTapY;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        mTapX = ev.getX();
        mTapY = ev.getY();
        return super.dispatchTouchEvent(ev);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PhotoView photoView = findViewById(R.id.photo_view);
        mPhotoView.setImageResource(R.drawable.pic1);
        mPhotoView.setOnPhotoTapListener((view, x, y) -> {
            mPhotoView.setScale(photoView.getMaximumScale(), mTapX, mTapY, true);
        });
    }
}
Dyno Cris
  • 1,823
  • 1
  • 11
  • 20
0
    var photoView=findViewById(R.id.photoView)
    var zoomBtn=findViewById(R.id.zoomBtn)
    zoomBtn.setOnClickListener {
        if (photoView.scale!=photoView.minimumScale)
             photoView.scale = photoView.minimumScale
        else photoView.scale = photoView.maximumScale
    }
Rizwan Ahmed
  • 105
  • 2
  • 11
  • Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others. – DCCoder Oct 04 '20 at 16:15
  • Here the main code is just 1 line `photoView.scale = photoView.maximumScale`. Others are Listener, Condition, etc. That's why I haven't explained. – Rizwan Ahmed Oct 04 '20 at 18:55