2012-02-23 105 views
1

我有一個尺寸大於屏幕(即1000x1000)的位圖。感謝畫布,我可以拖動此位圖。如果我在點(500,500)getX或getY不返回500其他值在0和320 [屏幕尺寸模擬器]之間的某個值,所以我需要知道相對於點0的絕對值。我希望我的視圖不要轉換位圖比0左派和查看絕對座標位圖android

的0前點多,我把一些代碼來澄清問題

public MapaView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); 
    mSurfaceHeight = 640; 
    mSurfaceWidth = 480; 
    mapaActual = new Mapa(); 
    mContext = context; 
    mBufferedCanvas = new Canvas(); 

} 


    draw(Canvas c){ 

    bitmapAux.eraseColor(Color.WHITE); 

    /*draw the rectangles....*/ 

    c.translate(mPosX, mPosY); 
    c.scale(mScaleFactor, mScaleFactor); 
    c.drawBitmap(bitmapAux, 0, 0, null);} 

我得到的位置下一個方法

@Override 
    public boolean onTouchEvent(MotionEvent ev) { 
     // Let the ScaleGestureDetector inspect all events. 
     mScaleDetector.onTouchEvent(ev); 


     final int action = ev.getAction(); 
     switch (action & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_DOWN: { 
      startTime = ev.getEventTime(); 
      final float x = ev.getX(); 
      final float y = ev.getY(); 
      final float xR= ev.getRawX(); 
      final float yR= ev.getRawY(); 
      movido=false; 
      mLastTouchX = x; 
      mLastTouchY = y; 
      mLastTouchXRaw = xR; 
      mLastTouchYRaw = yR; 

      mActivePointerId = ev.getPointerId(0); 

      break; 
     } 

     case MotionEvent.ACTION_MOVE: { 
      final int pointerIndex = ev.findPointerIndex(mActivePointerId); 
      final float x = ev.getX(pointerIndex); 
      final float y = ev.getY(pointerIndex); 

      // Only move if the ScaleGestureDetector isn't processing a gesture. 
      if (!mScaleDetector.isInProgress()) { 
       final float dx = x - mLastTouchX; 
       final float dy = y - mLastTouchY; 

       mPosX += dx; 
       mPosY += dy; 

       movido=true; 
       invalidate(); 
      } 

      mLastTouchX = x; 
      mLastTouchY = y; 
      movido=true; 


      break; 
     } 

     case MotionEvent.ACTION_UP: { 
      mActivePointerId = INVALID_POINTER_ID; 
      endTime = ev.getEventTime(); 

      if ((endTime - startTime > 500)&&!movido) { 

       // we have a 1000ms duration touch 
       // propagate your own event 
       Log.d("LC", "time touched greater than 1000ms " + endTime + " " 
         + startTime); 

       startTime = 0; 
       endTime = 0; 
       movido=false; 
       Box pepeBox=new Box(); 
       pepeBox=buscarElemento(); 
       //Intent intent=new Intent(mContext,OpcionesConceptoList.class); 
       //mContext.startActivity(intent); 
       /* 
       * return true; notify that you handled this event (do not 
       * propagate) 
       */ 
      } 
      break; 
     } 

     case MotionEvent.ACTION_CANCEL: { 
      mActivePointerId = INVALID_POINTER_ID; 
      break; 
     } 

     case MotionEvent.ACTION_POINTER_UP: { 
      final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; 
      final int pointerId = ev.getPointerId(pointerIndex); 
      if (pointerId == mActivePointerId) { 
       // This was our active pointer going up. Choose a new 
       // active pointer and adjust accordingly. 
       final int newPointerIndex = pointerIndex == 0 ? 1 : 0; 
       mLastTouchX = ev.getX(newPointerIndex); 
       mLastTouchY = ev.getY(newPointerIndex); 
       mActivePointerId = ev.getPointerId(newPointerIndex); 
      } 
      break; 
     } 

     } 



     return true; 
    } 

任何意見?

在此先感謝和原諒我的英語。

回答

1

getRawX()和getRawY():座標(X,Y)相返回到電話

的getX()和的getY()的屏幕:(X,Y)相對返回coordiantes到視圖。

其實絕對座標是什麼?

+0

感謝您快速的回答。 我想要位圖的絕對座標,因爲如果我在位置(900,900)上畫一個矩形,我必須移動以查看它,對於這個我使用canvas.translate(mPosX,mPosY) 謝謝 – user727763 2012-02-23 11:06:01

0

如果我明白你的問題,

// 
Bitmap bitmap; 
// 
// 
double bitmapRatio = ((double)bitmap.getWidth())/bitmap.getHeight() 

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
double screenHeight = metrics.heightPixels; 
double screenWidth = metrics.widthPixels; 

double screenRatio= screenWidth /screenHeight; 

if(bitmapRatio > screenRatio){ 

}else{ 

}