图像在android中出现在屏幕外

时间:2013-07-25 09:09:39

标签: android bitmap

我通过使用BitmapFactory绘制图片......内容是图片将监听加速度计...并根据那个...移动...我编写代码并且它工作得很好...但是图像已经用完了屏幕......图片应该在屏幕上并且始终对用户可见....

请告诉我如何解决这个问题......

mWidth = metrics.widthPixels;
            mHeight = metrics.heightPixels;
            float mRange = s.getMaximumRange();

            //
            // Convert MetersToPixels
            // metersToPixels = mWidth / .0254f
            //

            float startX = mWidth / (0.0254f * mRange);
            float startY = mHeight / (0.0254f * mRange);

            Canvas canvas = ourHolder.lockCanvas();
            canvas.drawColor(Color.WHITE);
            float mPosY = sensorY * startY;
            float mPosX = sensorX * startX;


            if (mPosX > mHorizontalBound) {
                mPosX = mHorizontalBound;
            }
            if (mPosY > mVerticalBound) {
                mPosY = mVerticalBound;
            }

            canvas.drawBitmap(betty, mPosY, mPosX, null);
            ourHolder.unlockCanvasAndPost(canvas);
        }

@Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
        mXOrigin = (w - betty.getWidth()) * 0.5f;
        mYOrigin = (h - betty.getHeight()) * 0.5f;
        mHorizontalBound = ((w / mMetersToPixelsX) * 0.5f);
        mVerticalBound = ((h / mMetersToPixelsY) * 0.5f);
    }

}

图像出现在屏幕的一侧..但它不应该像这样......

1 个答案:

答案 0 :(得分:0)

您需要检查画布的所有四个边。

你可以试试这个:

int boundRight = canvas.getClipBounds().right; 
int boundLeft = canvas.getClipBounds().left;
int boundBottom = canvas.getClipBounds().bottom;
int boundTop= canvas.getClipBounds().top;

if ( mPosX > boundRight )  {
   mPosX = boundRight;
}  else if ( mPosX < boundLeft )  {
   mPosX = boundLeft;
}

if ( mPosY > boundBottom )  {
   mPosY = boundBottom
}  else if ( mPosY < boundTop )  {
   mPosY = boundTop;
}