2011-10-14 28 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); 



     } 
    }