将位图旋转到移动方向

时间:2015-02-05 11:54:17

标签: canvas bitmap autorotate

我试图根据我的运动事件的运动方向旋转位图图像,让我说我从顶视图看到一辆汽车的图像,我希望汽车面向我的运动方向,如果我向左移动它应该转180度,然后它应该转到90等等,请原谅我的坏英语,iv只设法按照0为中心旋转图像

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

1 个答案:

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