2011-08-30 88 views
0

我使用触摸事件为画廊移动和缩放图片(画布)创建了自己的类。 我的问题是,图片总是移动,甚至是在屏幕外。正确的行为是图片的左边界必须在左边屏幕的边界或左边...并且与其余边界相同...Android:调整画布以查看边框

我尝试将max和min设置为mPosY和mPosX但它很难和复杂,因为当图片比屏幕小时,它必须居中......

任何想法?这里是一些代码:

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(); 
    } 
    ... 
} 

谢谢:)

回答

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

是一个有点难以理解的问题,但在简单的算术来说,这应该做的工作。