繁体   English   中英

图片在Android屏幕上消失了

[英]Image is going out of the screen in android

我通过使用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);
    }

}

图像从屏幕的一侧移出。.但是它不应像这样...

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

您可以尝试以下方法:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM