2012-06-25 9 views
5

回転するビットマップを中心に描画し、ビットマップのサイズを変更せずに回転させようとしています。私はすべてのスプライトをゲームスレッド経由で画面に描画していますので、キャンバスではなく元のビットマップを組み込んだソリューションを探しています。Androidはリサイズなしでビットマップを中心に回転します

ありがとうございます。

これはこれまでのコードですが、中心付近でビットマップを反転させていますが、サイズを変更しています。

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 

を私は挑戦のために、これはいくつかの数学(トリグ)が必要になります誰を理解できますか? :P

+0

これまでに何を試しましたか?あなたのために働いていないもののいくつかの例を投稿できますか? –

+0

ストレートAndroidまたはCocos2d-x? – Fallenreaper

+0

@Fallenreaperというコードで質問を更新しました。私は自分のGEを使用していますので、まっすぐなAndroidです。 –

答えて

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; 

    } 

NB:あなたはアニメーションの回転をしたい場合は、回転パラメータは、すべての新しい値で更新する必要がありますフレーム。 1、2、3 ...

7

Matrixクラスを使用してビットマップを描画する必要があります。以下は、 "Ship"クラスの内部でイメージを回転させたいと仮定した、非常に基本的な考え方です。更新メソッド内の現在位置行列を更新します。 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オブジェクトを生成すると、リソースが浪費されることにも注意してください。

+0

ビットマップを回転したBimapオブジェクトを返すrotateBitmap(ビットマップビットマップ、int回転)という静的メソッドを呼び出すことができますか? –

+0

このメソッドは渡すビットマップを変更します。したがって、ur private変数 "private Bitmap bitmap;"回転しません。ちょうど "返すビットマップ;"メソッドのシグネチャを変更します。 – Aziz

+0

申し訳ありませんが、これは私のために動作しません。 –

関連する問題