开发者

Unable to pan and zoom an image in canvas

开发者 https://www.devze.com 2023-04-10 10:24 出处:网络
I am trying to display an image with canvas that allows zoom and panning but will stop the user from panning the image off screen. I have been able to stop the panning but when the picture is zoomed i

I am trying to display an image with canvas that allows zoom and panning but will stop the user from panning the image off screen. I have been able to stop the panning but when the picture is zoomed it stops before the edge of the image. I need to be able to pan to the edge of the image even zoomed in. Any help would be appreciated.

My code that displays image, stops the panning offscreen and zoom:

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


Very similar to this: Zooming in android - keep image in view

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号