2010-12-29 36 views
4

と思われますが、Drawableクラスのメソッドを使用する方法はありません。それから、Canvasを何らかの方法で反転させなければならないかもしれないと思っていましたが、まだ適切な方法を見つけることができませんでした。X軸またはY軸に描画可能なものを反転する

y軸上にDrawableを「フリップ」するだけで、中心yを指定することができます。これどうやってするの?

答えて

8

10kフィートのレベルから、新しいビットマップを作成し、ビットマップを反転する変換マトリックスを指定します。

これはちょっとしたことかもしれませんが、これを行う方法を示す小さなサンプルアプリケーションがあります。書かれているように、(-1.0f、1.0f)の変換行列のプリスケールはx方向に画像を反転させ、(1.0f、-1.0f)のプリスケールはy方向に反転する。

public class flip extends Activity{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //Set view to our created view 
     setContentView(new drawView(this)); 
    } 

    private class drawView extends View{ 
     public drawView(Context context){ 
      super(context); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      super.onDraw(canvas); 

      //Load the jellyfish drawable 
      Bitmap sprite = BitmapFactory.decodeResource(this.getResources(), R.drawable.jellyfish); 

      //Create a matrix to be used to transform the bitmap 
      Matrix mirrorMatrix = new Matrix(); 

      //Set the matrix to mirror the image in the x direction 
      mirrorMatrix.preScale(-1.0f, 1.0f); 

      //Create a flipped sprite using the transform matrix and the original sprite 
      Bitmap fSprite = Bitmap.createBitmap(sprite, 0, 0, sprite.getWidth(), sprite.getHeight(), mirrorMatrix, false); 

      //Draw the first sprite 
      canvas.drawBitmap(sprite, 0, 0, null); 

      //Draw the second sprite 5 pixels to the right of the 1st sprite 
      canvas.drawBitmap(fSprite, sprite.getWidth() + 5, 0, null); 
     } 
    } 
} 
+0

ありがとうございます!私はまだMatrixを使用することについてはあまり知らない。多分、これが私の理解に役立つでしょう:p – Snailer

+0

なぜあなたは 'Activity'を拡張しますか?私はあなたがコンテンツビュー全体が反転されたBitmapであることを望むケースを想像することはできません –

+0

"これはちょっとしたことかもしれませんが、ここでは小さなサンプルアプリケーションです"。これは完全に自己完結型の例ですが、それはあなたの使い方に合った方法で実装するための練習問題として残されています。 –

関連する問題