0

I draw a picture by using BitmapFactory...The Content is The picture will listen the accelerometer.. and move according to that...i write the code and it working perfectly...but the image is going out of the screen...the picture should be in the screen and visible to the user at all times....

Please tell me how to solve this...

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

}

The image is going out side of the screen..But it should not go like this...

4

1 回答 1

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;
}
于 2013-07-25T10:20:12.963 回答