0

I have the following code:

    public void loop1(int times, SpriteObject[] obj){
        for(int i = 0; i < times; i++){
            obj[i] = new SpriteObject(BitmapFactory.decodeResource(gv.getResources(),  R.drawable.bar1), 500,720);
        }
     }

However, when I draw this on the view it does not stay as i expected. If I use obj[i].setX(10) and obj[5].setX(20). Only the second one appears as it is refreshing and only taking the last object. is it possible to reuse the drawables like this?

4

1 回答 1

0

I don't completely understand what you're trying to accomplish here and to me it seems like your resource load method is completely set up wrong. From what I can tell, you're trying to make two similar bitmaps move around.

First, for simple drawing applications or games you would use view or surfaceview so you must implement that. Secondly, the way I create moving bitmaps is to first create Bitmap variables to hold the respective bitmaps and then I create a separate classes for each respective Bitmap. In the classes I have the following.

public float x = 0; public float y = 0;

So with this in mind all you have to do to draw the bitmaps is in the onDraw method of View you use this method. canvas.drawBitmap(bitmapVariable, bitmap.x, bitmap.y, null);

If you wanted to move the bitmaps you would just have to change the X and Y of the respective bitmap. Either in a separate method or in your update loop.

Ultimately, I think you're understanding bitmaps wrong. Once created you don't need to recreate it to redraw it at a different location unless the bitmap was garbage collected.

于 2012-11-17T01:38:16.550 回答