放大Android的 - 保持图像鉴于 [英] Zooming in android - keep image in view

查看:170
本文介绍了放大Android的 - 保持图像鉴于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有上实际获得双指缩放实现吨线程 - 但我得到这个已远

我使用SimpleScaleGestureListener处理捏。我已经成功地确保您不能缩小更远比保持图像高度一样的屏幕尺寸(所以你像高度将永远是可用的屏幕尺寸至少是高度)。

不过,我正在寻找一种方式,如果图像被摇这些边界之外的自动检测。就像显示图像的标准机器人方式。所以,如果我放大,平移图像后,再放大了,我希望它当图像底部到达屏幕的底部检测,然后变焦的'支点'相应的调整,从而决不让空白的上方或下方的图像本身。谁能帮我?

有关参考,这是我onScale和方法的onDraw:

 公共布尔onScale(ScaleGestureDetector检测器){
    浮动minScale =(浮点)((浮点)SCREEN_SIZE.y /(浮点)DRAWABLE_SIZE.y);
    mScaleFactor * = detector.getScaleFactor();
    mScaleFactor = Math.max(minScale,Math.min(mScaleFactor,5.0F));
    无效();
    返回true;
}保护无效的onDraw(帆布油画){
    canvas.save();
    canvas.drawColor(Color.DKGRAY);
    canvas.translate(mPosX,mPosY);
    canvas.scale(mScaleFactor,mScaleFactor,getDrawable()。getIntrinsicWidth()/ 2,
    。getDrawable()getIntrinsicHeight()/ 2);    。this.getDrawable()画(油画);
    canvas.restore();
}


解决方案

您可以使用android.graphics.Matrix改造绘制位图。
矩阵同时拥有规模和翻译它。

您会简单地套用矩阵画布上:
canvas.setMatrix

您可以很容易地计算适合对屏幕所需的矩形:

  Matrix.setRectToRect

其中source是您的图片(位图的)真实分辨率,0,0在右下左/顶部宽/ heght矩形。 DST是目标视图的矩形,其中将位图绘制。

然后,你也可以解决你的任务 - 要知道你的图像边界将被绘制。
Matrix.ma preCT将计算在您的位图的矩形(如讨论previously)将屏幕上的屏幕坐标出现。然后,你可以简单地在那里图片的左侧相对于屏幕的左侧绘制,同样顶级/右/底部。

我不打算给你code,只是暗示该走哪条路。
我最近美元,捏缩放,适合到屏幕,过场动画等C $ CD图像浏览器,以及所有使用矩阵和该操作完成。

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();
}

解决方案

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:

Matrix.setRectToRect

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.

这篇关于放大Android的 - 保持图像鉴于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆