如何在PhotoVIew中缩放(缩放和移动)到位图的x,y坐标? [英] How to scale (zoom and move) to x,y coordinates of Bitmap in PhotoVIew?
问题描述
我使用lib https://github.com/chrisbanes/PhotoView
一开始我在位图上有位图和x,y点.
At start I have Bitmap and x,y point on the Bitmap.
如何将图像缩放和缩放到位图的x,y坐标?
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
比例= 1f
scale = 4f,我想以编程方式将中心移至x,y坐标
scale = 4f and I want programly move center to x,y coordinates
推荐答案
我遇到了同样的问题,并最终解决了这个问题.
I was facing the same issue, and finally made it.
为此,我尝试了很多事情.首先,您需要将坐标转换为PhotoView
:
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);
来源:GitHub aslansari
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)
另一种解决方案是使用回调或协程进行缩放,并同时进行移动,因为我们将进入顺序代码部分.但是AnimatedZoomRunnable
在另一个线程中,所以我们将继续进行操作:
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.
所以实际上我没有更多选择.也许像在GoogleMap中那样放置一个按钮,使其能够在屏幕中间居中,并在用户单击时执行模拟拖动"?
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 ?
还是没有一种方法可以在AnimatedZoomRunnable
完成时获得通知?
Or does there is a way to get notify when AnimatedZoomRunnable
is finished ?
如果您发现有趣的东西,请告诉我.
Let me know if you found something interesting.
这篇关于如何在PhotoVIew中缩放(缩放和移动)到位图的x,y坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!