开发者

Android: adjust canvas to view borders

I made my own class 开发者_如何学编程with touch events to move and zoom a picture ( canvas ) for a gallery. The problem I have is that the picture moves always, even is out the screen. The right behavior is the left border of the pic must be at left screen's border or left-hand to it... and the same with rest of borders...

I try to set max and min to mPosY and mPosX but it's hard and complicated, because when the pic is smaller than the screen, it must be centered...

Any idea? here is some of the code:

public class TouchView extends View {
    ...
    @Override
        public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(mPosX, mPosY);
        mScaleFactor = Math.max( mScaleFactor, minScaleFactor);
        canvas.scale(mScaleFactor, mScaleFactor, pivotX, pivotY);
        mIcon.draw(canvas);
        canvas.restore();
    }
    ...
}

Thanks :)


if(imageWidth < screenWidth && imageHeight < screenHeight) {
    imageX = screenWidth / 2 - imageWidth / 2;
    imageY = screenHeight / 2 - imageHeight / 2;
} else {
    if(imageX > 0) imageX = 0;
    if(imageY > 0) imageY = 0;
    if(imageX + imageWidth < screenWidth)
        imageX = screenWidth - imageWidth;
    if(imageY + imageHeight < screenHeight)
        imageY = screenHeight - imageHeight;
}

Was a little bit difficult to understand the problem, but in terms of simple arithmetic this should do the job.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜