2011-10-14 39 views
6

我想爲我的遊戲節省內存,我想問你,因爲我找不到任何東西,上次我在這裏問了一些問題,我得到了一個很好的答案。我可以翻轉日食內的位圖,這樣我可以節省內存的精靈?我發現的所有教程都是關於旋轉而不是翻轉的。翻轉一個位圖的教程只用於打開G1或類似的東西。請幫幫我。 我一直在尋找谷歌的教程,但我放棄了在第5頁。任何人都可以幫助我嗎? 有沒有人有一個很好的教程? 順便說一句,我使用的畫布。 謝謝!在android幫助中翻轉位圖?

我每次嘗試運行時都會收到一個關閉力......你能想出來嗎?這裏是我的代碼:

 Matrix flipHorizontalMatrix = new Matrix(); 
     flipHorizontalMatrix.setScale(-1,1); 
     flipHorizontalMatrix.postTranslate(0, canvas.getHeight()-arrowL.getHeight()); 
     canvas.drawBitmap(arrowL, flipHorizontalMatrix, null); 

我希望箭頭位於右下角。

+0

你是什麼意思的「翻轉」? –

+0

可以說我有一個精靈向左走的動畫,我想用相同的精靈向右走,只是將它翻轉到另一個方向。希望我清除它.. – Baruch

+0

從崩潰發佈堆棧跟蹤。 –

回答

19

由於您使用的是Canvas,爲什麼不嘗試drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint)方法。使用翻轉x座標的Matrix

你可以做這樣的事情:

Matrix flipHorizontalMatrix = new Matrix(); 
flipHorizontalMatrix.setScale(-1,1); 
flipHorizontalMatrix.postTranslate(myBitmap.getWidth(),0); 

canvas.drawBitmap(myBitmap, flipHorizontalMatrix, myPaint); 
+0

您需要使用flipHorizo​​ntalMatrix.postTranslate(..)而不是setTranslate,因爲您實際上需要使用翻譯矩陣連接當前比例矩陣,但不是僅創建翻譯矩陣。 – asenovm

+0

即時消息我會嘗試它謝謝 – Baruch

+0

我有點困惑,我應該只使用posttranslate而不是settranslate或兩者? – Baruch

0

謝謝,看看這個代碼,它可能對您有用旋轉的位圖圖像.. 這裏我有一個採取了觀賞魚的例子,它應該移動從左至右翻轉,並繼續從從右向左移動,反之亦然.. 這裏是

  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    fish = BitmapFactory.decodeResource(getResources(), R.drawable.fish); 
      v = new OurView(this); 

公共類OurView擴展SurfaceView實現Runnable {

爲你的代碼..
Thread t = null; 
    SurfaceHolder holder; 
    boolean isitOK = false; 
    String Flag = "right"; 
    Bitmap rotatedBitmap=null; 
    Matrix rotateRight = new Matrix(); 
    Matrix rotateLeft = new Matrix(); 
    Bitmap rSprite=null; 
    Bitmap lSprite=null; 
    public OurView(Context context) { 
     super(context); 
     holder = getHolder(); 
     rotateLeft.setScale(-1, 1); 


     rSprite = Bitmap.createBitmap(fish, 0, 0, 
       fish.getWidth(), fish.getHeight(), rotateRight, true); 
     lSprite = Bitmap.createBitmap(fish, 0, 0, 
       fish.getWidth(), fish.getHeight(), rotateLeft, true); 
    } 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 

     while (isitOK == true) { 
      if (!holder.getSurface().isValid()) { 
       continue; 
      } 
      Canvas canvas = holder.lockCanvas(); 

      canvas.drawBitmap(bg, 0, 0, null); 
      if(Flag == "right") 
      canvas.drawBitmap(lSprite, x, y, null); 

      if(Flag == "left") 
      canvas.drawBitmap(fish, x, y, null);  

       if (Flag == "right" && x <= 60) { 

        x++; 
        if (x == 60) { 

         Flag = "left"; 
        // canvas.drawBitmap(rSprite, 0, fish.getWidth(), null); 
         canvas.drawBitmap(fish, x, y, null);  
        } 
       }    
       if (Flag == "left" && x >= 0) { 
        x--; 
        if (x == 0) { 
        Flag = "right"; 

        canvas.drawBitmap(fish, x, y, null); 
        } 
       } 




      holder.unlockCanvasAndPost(canvas); 



     } 
    }