我正在尝试使用画布显示允许缩放和平移的图像,但会阻止用户将图像平移到屏幕外。我已经能够停止平移但是当图片被缩放时它会在图像边缘之前停止。我需要能够平移到图像边缘甚至放大。任何帮助都会受到赞赏。
我的代码显示图片,停止平移屏幕并缩放:
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
if (mPosX < displayWidth - mIcon.getIntrinsicWidth()){
mPosX = displayWidth - mIcon.getIntrinsicWidth();}
if (mPosX > 0){
mPosX = 0; }
if (mPosY < displayHeight - mIcon.getIntrinsicHeight()){
mPosY = displayHeight - mIcon.getIntrinsicHeight();}
if (mPosY > 0){
mPosY = 0; }
canvas.translate(mPosX, mPosY);
canvas.scale(mScaleFactor, mScaleFactor);
mIcon.draw(canvas);
canvas.restore();
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.42f, Math.min(mScaleFactor, 5.0f));
invalidate();
return true;
}
}