6

I want to save up memory for my game and I wanted to ask you because I couldn't find anything and last time that I asked something here I got a good answer. Can i flip the bitmap inside eclipse so i could save on memory for sprites? All the tutorials that i found were about rotating and not flipping. The tutorials for flipping a bitmap were only for open Gl or something like that. Please help me. I've been looking up for tutorials in google but i gave up at page 5. Can anyone help me? Does anyone have a good tutorial? By the way I am using a canvas. Thanks!

I get a force close everytime I try to run it... can u figure it out? here is my code:

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

I want the arrow to be at the bottom right corner.

Baruch
  • 1,618
  • 5
  • 23
  • 42
  • lets say i have an animation of a sprite going to the left, i want to make it to go to the right with the same sprite, just flip it to the other way. hope i cleared it .. – Baruch Oct 15 '11 at 00:09
  • Post the stack trace from the crash. – Christopher Perry Oct 15 '11 at 06:49
  • 2
    Wow. If you're running Eclipse, and your Android device is hooked up to your computer, take a look at the output in the Logcat when your app force closes. There will be some red text in there towards the bottom, it will give you the line number and the reason for the crash http://developer.android.com/guide/developing/debugging/ddms.html, You really should learn to debug if you're going to be working on apps. – Christopher Perry Oct 15 '11 at 07:02
  • maybe its that? E 51 garlloc [unregister] handle 0x474500 still locked (state =40000001) – Baruch Oct 15 '11 at 07:14
  • Please read over the link on ddms. – Christopher Perry Oct 15 '11 at 07:33
  • I think i figured it out. i get an error now which is different than the other one that i got. it says: I got the same message twice but with different pid, one of them is "301" the other is "312", the tag:"AndroidRuntime" message: "ERROR: thread attach failed". on my emulator: "Application MyApp(process com.my.app) is waiting for the debugger to attach." I hope that it is the right information, if not can you give me an easier tutorial, i had a hard time understanding the other one. Thanks – Baruch Oct 15 '11 at 16:59
  • nvm i fixed, you are a genius! just one question, why does it make the x to be on the right instead of on the left, its just like mirror. – Baruch Oct 15 '11 at 17:08

2 Answers2

18

Since you're using Canvas, why not try the drawBitmap (Bitmap bitmap, Matrix matrix, Paint paint) method. Use a Matrix that flips the x coordinates.

You can do something like this:

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

canvas.drawBitmap(myBitmap, flipHorizontalMatrix, myPaint);
Jesse Jashinsky
  • 10,313
  • 6
  • 38
  • 63
Christopher Perry
  • 38,891
  • 43
  • 145
  • 187
  • You need to use flipHorizontalMatrix.postTranslate(..) instead of setTranslate, because you actually need to concat the current scale matrix with a translate matrix, but not to create a translate matrix only. – asenovm Oct 15 '11 at 01:39
  • I got a little confused, should I use only posttranslate instead of settranslate or both? – Baruch Oct 15 '11 at 02:57
  • I edited my answer with the correct call. I wasn't aware that the set call didn't concatenate. – Christopher Perry Oct 15 '11 at 04:12
  • 1
    Its not working, i added my code to my question. everything is happening in the constructor. is it suppose to be somewhere else? – Baruch Oct 15 '11 at 06:15
0

Thanks,check out this code it may be useful for you to rotate an Bitmap image.. Here i have a taken a aquarium fish example,it should be moved from left to right and flipped and continue moving from right to left and viceversa.. here is the code for you..

           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); 

public class OurView extends SurfaceView implements 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);



        }
    }