无法在画布中平移和缩放图像

时间:2011-10-04 20:25:17

标签: android image-zoom panning

我正在尝试使用画布显示允许缩放和平移的图像,但会阻止用户将图像平移到屏幕外。我已经能够停止平移但是当图片被缩放时它会在图像边缘之前停止。我需要能够平移到图像边缘甚至放大。任何帮助都会受到赞赏。

我的代码显示图片,停止平移屏幕并缩放:

    @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;
        }
    }

1 个答案:

答案 0 :(得分:0)