2015-02-05 5 views
0

비트 맵 이미지를 내 모션 이벤트의 이동 방향에 따라 회전하려고하는 중입니다. 톱 뷰에서 자동차 이미지를 가지고 있습니다. 차가 내 움직임의 방향으로 향하게합니다. 메신저는 180의 degress를 켜고해야 우 이동하는 경우, 그것은 90에 설정해야 등등, PLS 내 나쁜 영어를 excuss, IV는 이미지가 방법에서 중심비트 맵을 이동 방향으로 회전

//this is how i rotate 
double ComputeAngle(float x, float y){ 
    final double RADS_TO_DEGREES = 360/(Math.PI * 2); 
    double result = Math.atan2(y,x) * RADS_TO_DEGREES; 

    if (result < 0){ 
     result = 360 + result; 
    } 
    return result; 
} 

//bitmap to rotate pretend its a car from top view 
Bitmap bitmap; 
//draws bitmap 
private final RectF tmpRF = new RectF(); 
final void drawStrokePoint(Canvas c, float x, float y, float r) { 
    tmpRF.set(x-r,y-r,x+r,y+r); 

    //compute rotation 
    float rotation = (float)ComputeAngle(x, y); 

    Matrix matrix = new Matrix(); 
    matrix.postTranslate(-bitmap.getWidth()/2, -bitmap.getHeight()/2); 
    matrix.postRotate(rotation); 
    matrix.postTranslate(x, y); 

    //draw bitmap 
    c.drawBitmap(mAirbrushBits, matrix, null); 
} 

//get x y cords and draw at x y position 
@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
    float x = event.getX(); 
    float y = event.getY(); 

    switch (event.getAction() & MotionEvent.ACTION_MASK) 
    { 
     case MotionEvent.ACTION_DOWN: 

      strokeX = x; 
      strokeY = y; 

      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 

      break; 
     case MotionEvent.ACTION_MOVE: 

      drawStrokePoint(drawCanvas, x, y, currentWidth); 

      break; 
     case MotionEvent.ACTION_UP: 

      break; 
     case MotionEvent.ACTION_POINTER_UP: 

      break; 
    } 

    return true; 
} 

답변

0

에게있는 0 주문에 따라 회전 관리 ComputeAngle을 사용하면 운동의 올바른 각도를 계산하지 않습니다. 이렇게하려면 x와 y의 이전 값도 필요합니다. 로 보일 것이다 방법은 다음과 같습니다

double ComputeAngle(double x, double y, double oldX, double oldY){ 
    // calculates the direction vector 
    double directionX = x - oldX; 
    double directionY = y - oldY; 

    double vectorLenght = Math.sqrt(directionX * directionX + directionY * directionY); 

    // normalize the direction vector (coordinate/|v|) 
    double normalizedX = directionX/vectorLenght; 
    double normalizedY = directionY/vectorLenght; 

    //Obtains the angle relative to the vector over the axis X (1,0) 
    // The formula is the angle = scalar(v1, v2)/|v1||v2| 
    double angleRadians = (normalizedX)/Math.sqrt(normalizedX*normalizedX + normalizedY*normalizedY); 

    // This formula will give you a angle between 0 and 180, to obtain the other half you must check the direction 
    if (directionY < 0) 
     angleRadians += Math.PI; 

    return angleRadians * RADS_TO_DEGREES; 
} 
+0

감사를 너무 빨리 재생, 나는, PLZ 내 영어 감사를 용서 것을 내가 어떻게해야합니까 oldx, 오래된 Y와 조금 혼란 스러워요 –