6

回転するビットマップを中心に描画し、ビットマップのサイズを変更せずに回転させるのに苦労しています。ゲームスレッドを介してすべてのスプライトを画面に描画しているため、キャンバスではなく元のビットマップを組み込むソリューションを探しています。

前もって感謝します。

これはこれまでの私のコードです。ビットマップを中心に回転させ、サイズを変更します。

i = i + 2;
            transform.postRotate(i, Assets.scoresScreen_LevelStar.getWidth()/2, Assets.scoresScreen_LevelStar.getHeight()/2);
            Bitmap resizedBitmap = Bitmap.createBitmap(Assets.scoresScreen_LevelStar, 0, 0, Assets.scoresScreen_LevelStar.getWidth(), Assets.scoresScreen_LevelStar.getHeight(), transform, true);

            game.getGraphics().getCanvasGameScreen().drawBitmap(resizedBitmap, null, this.levelStar.getHolderPolygons().get(0), null);

アップデート:

これは思ったほど簡単ではないことに気づきました。私の回転コードは問題ではありません。ビットマップは回転しますが、回転角度に応じて dst rect も増減する必要があります。そうしないと、bimap が小さく表示されます。これは、固定された dst rect に描画されるためです。したがって、dst rect を返すメソッドを開発する必要があると思います。 したがって、サイズを変更せずにビットマップを回転させるために必要なメソッドは次のとおりです。

public static Bitmap rotateBitmap(Bitmap bitmap, int rotation) // I've got this method working

public static Rect rotateRect(Rect currentDst, int rotation) // Don't got this

これには数学 (trig) が必要になると思いますが、挑戦できる人はいますか? :P

4

2 に答える 2

8

Matrixクラスを使用してビットマップを描画する必要があります。以下は、「Ship」クラス内で画像を回転させたいと仮定した場合の非常に基本的なアイデアです。update メソッド内で現在位置 Matrix を更新します。onDraw() では、新しく更新された位置マトリックスを使用してビットマップを描画します。これにより、サイズを変更せずに回転したビットマップが描画されます。

public class Ship extends View {

    private float x, y;
    private int rotation;
    private Matrix position;    
    private Bitmap bitmap;

    ...

    @Override
    public void onDraw(Canvas canvas) {
        // Draw the Bitmap using the current position
        canvas.drawBitmap(bitmap, position, null);
    }

    public void update() {
        // Generate a new matrix based off of the current rotation and x and y coordinates.
        Matrix m = new Matrix();
        m.postRotate(rotation, bitmap.getWidth()/2, bitmap.getHeight()/2);
        m.postTranslate(x, y);

        // Set the current position to the updated rotation
        position.set(m);

        rotation += 2;
    }

    ....

}

それが役立つことを願っています!

また、ゲーム ループ内で新しいBitmapオブジェクトを生成すると、リソースが大量に消費されることにも注意してください。

于 2012-07-09T17:14:31.063 に答える
0

これは私のために働いた!

行列を返すメソッドを作成しました。行列は、次の描画方法で使用できます。

public void drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint)

どうぞ!(パラメーター形状は簡単に置き換えることができます。ご希望の場合は、コメントを残してください):

public static Matrix rotateMatrix(Bitmap bitmap, Shape shape, int rotation) {

        float scaleWidth = ((float) shape.getWidth()) / bitmap.getWidth();
        float scaleHeight = ((float) shape.getHeight()) / bitmap.getHeight();

        Matrix rotateMatrix = new Matrix();
        rotateMatrix.postScale(scaleWidth, scaleHeight);
        rotateMatrix.postRotate(rotation, shape.getWidth()/2, shape.getHeight()/2);
        rotateMatrix.postTranslate(shape.getX(), shape.getY());


        return rotateMatrix;

    }

注意: アニメートされた回転が必要な場合は、フレームごとに回転パラメータを新しい値で更新する必要があります。1の次に2の次に3 ...

于 2012-07-30T14:38:19.403 に答える