放大 Android - 保持图像可见
我知道有大量关于实际实现捏缩放的线程 - 但我已经做到了这一点。
我使用 SimpleScaleGestureListener 来处理捏合。我设法确保您不能缩小得比保持图像高度与屏幕尺寸相同(因此您的图像高度将始终至少是可用屏幕尺寸的高度)。
但是,我正在寻找一种方法来自动检测图像是否平移到这些边界之外。就像标准的 android 显示图像的方式一样。因此,如果我放大,向上平移图像,然后缩小,我希望它能够检测到图像底部何时到达屏幕底部,然后缩放的“枢轴”相应调整,因此永远不允许“图像本身上方或下方的空白”。谁能帮助我吗?
作为参考,这是我的 onScale 和 onDraw 方法:
public boolean onScale(ScaleGestureDetector detector){
float minScale = (float) ((float) SCREEN_SIZE.y / (float) DRAWABLE_SIZE.y);
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(minScale, Math.min(mScaleFactor, 5.0f));
invalidate();
return true;
}
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.drawColor(Color.DKGRAY);
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor, getDrawable().getIntrinsicWidth() / 2,
getDrawable().getIntrinsicHeight() / 2);
this.getDrawable().draw(canvas);
canvas.restore();
}
I know there are tons of threads on actually getting pinch zoom implemented - but I got this far already.
I'm using SimpleScaleGestureListener to handle the pinching. I've managed to make sure you can't zoom out farther than to keep the image height the same as the screen size (so you're image height will always be at least the height of the available screen size).
However, I'm looking for a way to automatically detect if the image is being panned outside of these boundaries. Just like the standard android way of displaying the image. So if I zoom in, and pan the image up, and then zoom out, I want it to detect when the bottom of the image reaches the bottom of the screen, and then 'pivot' of the zooming adjusts accordingly, thus never allowing "whitespace" above or below the image itself. Can anyone help me?
For reference, here's my onScale and onDraw methods:
public boolean onScale(ScaleGestureDetector detector){
float minScale = (float) ((float) SCREEN_SIZE.y / (float) DRAWABLE_SIZE.y);
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(minScale, Math.min(mScaleFactor, 5.0f));
invalidate();
return true;
}
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.drawColor(Color.DKGRAY);
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor, getDrawable().getIntrinsicWidth() / 2,
getDrawable().getIntrinsicHeight() / 2);
this.getDrawable().draw(canvas);
canvas.restore();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 android.graphics.Matrix 来转换绘制的 Bitmap。
矩阵同时包含比例和平移。
您只需将矩阵应用到画布上即可:
canvas.setMatrix
您可以轻松计算所需矩形的适合屏幕大小:
其中源是图像(位图)的实际分辨率矩形,左/上为 0,0,右下为宽/高。 dst 是目标视图的矩形,将在其中绘制位图。
然后你还可以解决你的任务 - 知道图像的边界将在哪里绘制。
Matrix.mapRect 将计算位图的矩形(如前所述)将在屏幕坐标中出现在屏幕上的位置。然后,您可以简单地相对于屏幕左侧绘制图像的左侧,对于顶部/右侧/底部也是如此。
我不会给你代码,只是提示你该走哪条路。
我最近编写了具有捏缩放、适合屏幕、动画过渡等功能的图像查看器,所有这些都是使用 Matrix 及其操作完成的。
You can use android.graphics.Matrix to transform drawn Bitmap.
Matrix holds both scale and translation in it.
You would simply apply the matrix to canvas:
canvas.setMatrix
You can easily compute fit-to-screen for desired rectangle:
where source is your image's (Bitmap's) real resolution rectangle with 0,0 in left/top and width/heght in right bottom. dst is target view's rectangle where Bitmap will be drawn.
Then you may also solve your task - to know where borders of your image would be drawn.
Matrix.mapRect will compute where your Bitmap's rectangle (as discussed previously) will appear on screen in screen's coordinates. Then you may simply where image's left side is drawn relative to screen's left side, same for top/right/bottom.
I'm not going to give you code, just hints which way to go.
I've recently coded image viewer with pinch-zoom, fit-to-screen, animated transitions, etc, and all was done using Matrix and manipulations of that.
一个简单的解决方案是使用此小部件
http://blog。 sephiroth.it/2011/04/04/imageview-zoom-and-scroll/
A simple solution is to use this widget
http://blog.sephiroth.it/2011/04/04/imageview-zoom-and-scroll/