private void limitPan() { final float aspectQuotient = mAspectQuotient.get(); final float zoomX = mState.getZoomX(aspectQuotient); final float zoomY = mState.getZoomY(aspectQuotient); final float panMinX = .5f - getMaxPanDelta(zoomX); final float panMaxX = .5f + getMaxPanDelta(zoomX); final float panMinY = .5f - getMaxPanDelta(zoomY); final float panMaxY = .5f + getMaxPanDelta(zoomY); if (mState.getPanX() > panMaxX) { mState.setPanX(panMaxX); } if (mState.getPanX() < panMinX) { mState.setPanX(panMinX); } if (mState.getPanY() > panMaxY) { mState.setPanY(panMaxY); } if (mState.getPanY() < panMinY) { mState.setPanY(panMinY); } }
private void limitZoom() { if (mState.getZoom() > MAX_ZOOM) { mState.setZoom(MAX_ZOOM); } if (mState.getZoom() < MIN_ZOOM) { mState.setZoom(MIN_ZOOM); } }
/** * Set object holding the zoom state that should be used * * @param state The zoom state */ public void setZoomState(ZoomState state) { if (mState != null) { mState.deleteObserver(this); } mState = state; mState.addObserver(this); invalidate(); }
@Override protected void onDraw(Canvas canvas) { if (mBitmap != null && mState != null) { final int viewWidth = getWidth(); final int viewHeight = getHeight(); final int bitmapWidth = mBitmap.getWidth(); final int bitmapHeight = mBitmap.getHeight(); final float panX = mState.getPanX(); final float panY = mState.getPanY(); final float zoomX = mState.getZoomX(mAspectQuotient) * viewWidth / bitmapWidth; final float zoomY = mState.getZoomY(mAspectQuotient) * viewHeight / bitmapHeight; // Setup source and destination rectangles mRectSrc.left = (int) (panX * bitmapWidth - viewWidth / (zoomX * 2)); mRectSrc.top = (int) (panY * bitmapHeight - viewHeight / (zoomY * 2)); mRectSrc.right = (int) (mRectSrc.left + viewWidth / zoomX); mRectSrc.bottom = (int) (mRectSrc.top + viewHeight / zoomY); mRectDst.left = getLeft(); mRectDst.top = getTop(); mRectDst.right = getRight(); mRectDst.bottom = getBottom(); // Adjust source rectangle so that it fits within the source image. if (mRectSrc.left < 0) { mRectDst.left += -mRectSrc.left * zoomX; mRectSrc.left = 0; } if (mRectSrc.right > bitmapWidth) { mRectDst.right -= (mRectSrc.right - bitmapWidth) * zoomX; mRectSrc.right = bitmapWidth; } if (mRectSrc.top < 0) { mRectDst.top += -mRectSrc.top * zoomY; mRectSrc.top = 0; } if (mRectSrc.bottom > bitmapHeight) { mRectDst.bottom -= (mRectSrc.bottom - bitmapHeight) * zoomY; mRectSrc.bottom = bitmapHeight; } canvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPaint); } }
public void pan(float dx, float dy) { final float aspectQuotient = mAspectQuotient.get(); mState.setPanX(mState.getPanX() + dx / mState.getZoomX(aspectQuotient)); mState.setPanY(mState.getPanY() + dy / mState.getZoomY(aspectQuotient)); limitPan(); mState.notifyObservers(); }
public void zoom(float f, float x, float y) { final float aspectQuotient = mAspectQuotient.get(); final float prevZoomX = mState.getZoomX(aspectQuotient); final float prevZoomY = mState.getZoomY(aspectQuotient); mState.setZoom(mState.getZoom() * f); limitZoom(); final float newZoomX = mState.getZoomX(aspectQuotient); final float newZoomY = mState.getZoomY(aspectQuotient); mState.setPanX(mState.getPanX() + (x - .5f) * (1f / prevZoomX - 1f / newZoomX)); mState.setPanY(mState.getPanY() + (y - .5f) * (1f / prevZoomY - 1f / newZoomY)); limitPan(); mState.notifyObservers(); }
public boolean isZoomed() { return mState.isZoomed(); }
private void resetZoomState() { mZoomState.setPanX(0.5f); mZoomState.setPanY(0.5f); mZoomState.setZoom(1f); mZoomState.notifyObservers(); }