[android] Android : 중심점에서 비트 맵을 회전하는 방법

이 문제에 대한 해결책을 찾기 위해 하루 이상을 찾고 있었지만 여기에 대한 답변조차도 도움이되지 않습니다. 문서는 아무것도 설명하지 않습니다.

나는 단순히 다른 물체의 방향으로 회전을 시도하고 있습니다. 문제는 비트 맵이 고정 된 지점을 중심으로 회전하는 것이 아니라 비트 맵 (0,0)을 중심으로 회전된다는 것입니다.

다음은 문제가있는 코드입니다.

  Matrix mtx = new Matrix();
  mtx.reset();
  mtx.preTranslate(-centerX, -centerY);
  mtx.setRotate((float)direction, -centerX, -centerY);
  mtx.postTranslate(pivotX, pivotY);
  Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, spriteWidth, spriteHeight, mtx, true);
  this.bitmap = rotatedBMP;

이상한 부분은 pre/ 내의 값을 변경하는 방법 postTranslate()setRotation(). 누군가 제발 도와주고 올바른 방향으로 밀어 줄 수 있습니까? 🙂



답변

다음 코드 시퀀스가 ​​도움이되기를 바랍니다.

Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, config);
Canvas canvas = new Canvas(targetBitmap);
Matrix matrix = new Matrix();
matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
canvas.drawBitmap(source, matrix, new Paint());

에서 다음 방법을 확인하면 ~frameworks\base\graphics\java\android\graphics\Bitmap.java

public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height,
        Matrix m, boolean filter)

이것은 그것이 회전과 이동으로 무엇을하는지 설명 할 것입니다.


답변

편집 : 최적화 된 코드.

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
      Matrix matrix = new Matrix();
      matrix.postRotate(angle);
      return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

리소스에서 비트 맵을 가져 오려면 :

Bitmap source = BitmapFactory.decodeResource(this.getResources(), R.drawable.your_img);


답변

나는 우리가 게임을 마무리하고있는 지금이 문제로 돌아 왔고 나는 단지 나를 위해 일한 것을 게시하려고 생각했다.

다음은 매트릭스를 회전하는 방법입니다.

this.matrix.reset();
this.matrix.setTranslate(this.floatXpos, this.floatYpos);
this.matrix.postRotate((float)this.direction, this.getCenterX(), this.getCenterY());

( this.getCenterX()기본적으로 비트 맵 X 위치 + 비트 맵 너비 / 2)

그리고 비트 맵을 그리는 방법 ( RenderManager클래스 를 통해 호출 됨 ) :

canvas.drawBitmap(this.bitmap, this.matrix, null);

그래서 prettey 정직하지만 난 그게으로 나는 일에 그것을 얻을 수 없다는 이상한 ABIT 찾아 setRotate다음 postTranslate. 왜 이것이 작동하지 않는지 아는 사람들이 있습니까? 이제 모든 비트 맵이 제대로 회전하지만 비트 맵 품질이 약간 저하되지는 않습니다.

어쨌든 도와 주셔서 감사합니다!


답변

다음을 ImageView사용하여 회전 할 수도 있습니다 RotateAnimation.

RotateAnimation rotateAnimation = new RotateAnimation(from, to,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(ANIMATION_DURATION);
rotateAnimation.setFillAfter(true);

imageView.startAnimation(rotateAnimation);


답변

다음과 같이 사용할 수 있습니다.


Matrix matrix = new Matrix();
matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2);
RectF rectF = new RectF(0, 0, source.getWidth(), source.getHeight());
matrix.mapRect(rectF);
Bitmap targetBitmap = Bitmap.createBitmap(rectF.width(), rectF.height(), config);
Canvas canvas = new Canvas(targetBitmap);
canvas.drawBitmap(source, matrix, new Paint());


답변

Lunar Lander라는 Google의 샘플을보세요. 우주선 이미지가 동적으로 회전합니다.

Lunar Lander 코드 샘플


답변

이 구성을 사용했지만 여전히 픽셀 화 문제가 있습니다.

Bitmap bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.map_pin);
        Bitmap targetBitmap = Bitmap.createBitmap((bmpOriginal.getWidth()),
                (bmpOriginal.getHeight()),
                Bitmap.Config.ARGB_8888);
        Paint p = new Paint();
        p.setAntiAlias(true);

        Matrix matrix = new Matrix();
        matrix.setRotate((float) lock.getDirection(),(float) (bmpOriginal.getWidth()/2),
                (float)(bmpOriginal.getHeight()/2));

        RectF rectF = new RectF(0, 0, bmpOriginal.getWidth(), bmpOriginal.getHeight());
        matrix.mapRect(rectF);

        targetBitmap = Bitmap.createBitmap((int)rectF.width(), (int)rectF.height(), Bitmap.Config.ARGB_8888);


        Canvas tempCanvas = new Canvas(targetBitmap);
        tempCanvas.drawBitmap(bmpOriginal, matrix, p);