85

この問題の解決策を 1 日以上探していましたが、ここでの回答でさえ、何も役に立ちません。ドキュメンテーションも何も説明していません。

別のオブジェクトの方向に回転させようとしているだけです。問題は、ビットマップが固定点を中心に回転するのではなく、ビットマップ (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()と 内の float 引数をどのように変更してもかまわないことsetRotation()です。誰かが助けて、私を正しい方向に押してくれませんか? :)

4

8 に答える 8

101

次の一連のコードがお役に立てば幸いです。

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)

これは、回転と移動で何をするかを説明します。

于 2010-11-29T02:52:14.023 に答える
79

編集済み: 最適化されたコード。

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);
于 2013-04-25T15:05:45.153 に答える
25

ゲームを完成させようとしている今、この問題に戻ってきました。

これは、マトリックスを回転させる方法です。

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

ですから、かなり簡単ですがsetRotatepostTranslate. たぶん、これが機能しない理由を知っている人もいますか?これで、すべてのビットマップが適切に回転しますが、ビットマップの品質が多少低下することはありません:/

とにかく、助けてくれてありがとう!

于 2010-12-11T11:10:37.147 に答える
7

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);
于 2010-11-30T03:43:56.960 に答える
5

次のようなものを使用できます。


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

于 2010-12-04T05:06:50.010 に答える
1

この構成を使用しましたが、まだピクセル化の問題があります。

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);
于 2012-10-31T08:22:42.897 に答える
1

Lunar Lander という Google のサンプルを見てください。船の画像が動的に回転しています。

月着陸船のコードサンプル

于 2010-12-05T12:02:08.123 に答える
0
matrix.reset();
matrix.setTranslate( anchor.x, anchor.y );
matrix.postRotate((float) rotation , 0,0);
matrix.postTranslate(positionOfAnchor.x, positionOfAnchor.x);
c.drawBitmap(bitmap, matrix, null); 
于 2013-10-07T09:40:22.253 に答える