2013-07-25 3 views
0

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

} 

이미지가 ... 그것은 같이 갈해야 screen..But의 측면을 것입니다

+0

바운드 변수가 어떤 값을 가질 수 있습니까? – WarrenFaith

+0

나는 그것을 볼 수 있지만 mHorizontalBound 및 mVerticalBound 변수를 요구하고있다 ... – WarrenFaith

+0

이것은 화면 경계이다 .. – bGorle

답변

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

작동하지만 작동하지 않아요 ... 하단 이미지에 맞지 않습니다 ... 오른쪽 이미지가 가운데로 이동합니다 .... – bGorle

+0

이 작업은 ... 왼쪽 작업 중입니다. 및 상위 포지 젼에서만 ... Beacuse in canvas.drawBitmap (betty, left, top, null); – bGorle

+0

아래/오른쪽에 대한 확인에 크기를 간단히 추가하십시오. 'if (mPosX + width> boundRight)'와'if (mPosY + height> boundBottom)'를 사용합니다. – free3dom

관련 문제