开发者

Android Development: SurfaceView Load bitmap from Array

开发者 https://www.devze.com 2023-03-19 13:26 出处:网络
I am developing a simple 2d game. But im stuck at this point where i need to \"spawn\" 开发者_运维百科unlimited with the same enemy when i click the screen.

I am developing a simple 2d game. But im stuck at this point where i need to "spawn" 开发者_运维百科unlimited with the same enemy when i click the screen.

So i think the best choose for something with unlimited is an array but i have no idea how to get a bitmapArray and then for each item in BitmapArray do canvas.draw

Someone please help me out!

//Simon


First of all, you will end up with a sort of limited array. Secondly, I'm recommending the Memory Pool pattern when using stuff like this, so you don't create new instances during runtime.

Back to your question, a first implementation would look something like this:

public class BitmapObject {
    private Bitmap mBitmap;
    private int mPositionX;
    private int mPositionY;
    private int mBitmapWidth;
    private int mBitmapHeight;
    private boolean mIsAlive;

    public BitmapObject(Bitmap bitmap) {
        mBitmap = bitmap;
        mBitmapWidth = bitmap.getWidth();
        mBitmapHeight = bitmap.getHeight();
        mIsAlive = false;
    }    

    public void draw(Canvas canvas) {
        if (mIsAlive) {
            canvas.drawBitmap(mBitmap, mPositionX, mPositionY, null);
        }
    }

    public void setNewPosition(int touchX, int touchY) {
        mPositionX = touchX - mBitmapWidth / 2;
        mPositionY = touchY - mBitmapHeight / 2;
    }

    public void setIsAlive(boolean isAlive) { mIsAlive = isAlive; }

    public boolean getIsAlive() { return mIsAlive; }

}

And use it like this in your SurfaceView class:

public class CanvasRenderer extends SurfaceView implements SurfaceHolder.Callback {     
    private static final int MAX_OBJECTS = 16;
    private BitmapObject[] mBitmapObjectsArray;

    public CanvasRenderer(Context context) {
        super(context);
         // Necessary SurfaceView initialization stuff.

         Bitmap sprite = BitmapFactory.decodeResource(context.getResources(), 
             R.drawable.sprite);
         mBitmapObjectsArray = new BitmapObject[MAX_OBJECTS];

         for (int x = 0; x < MAX_OBJECTS; x++) {
             mBitmapObjectsArray[x] = new BitmapObject(sprite);
         }
    }

    @Override 
    public void onDraw(Canvas canvas) {     
        canvas.drawColor(Color.BLACK);  
        for (int x = 0; x < MAX_OBJECTS; x++) {
            mBitmapObjectsArray[x].draw(canvas);
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // Stuff.
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {      
        // Stuff.
    }

    @Override
    public void onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            for (int x = 0; x < MAX_OBJECTS; x++) {
                boolean isAlive = mBitmapObjectsArray[x].getIsAlive();
                if (!isAlive) {
                    mBitmapObjectsArray[x].setNewPosition((int) event.getX(), 
                        (int) event.getY());
                    mBitmapObjectsArray[x].setIsAlive(true);
                    break;
                }
            }
        }
    }

}


Ok, this is only the concept of drawing 2 bitmaps on a canvas. Actual implementation is much more serious.

Bitmap renderbmp1 = Bitmap.createBitmap( bitmapWidth,bitmapHeight,Bitmap.Config.RGB_565 );
Bitmap renderbmp2 = Bitmap.createBitmap( bitmapWidth,bitmapHeight,Bitmap.Config.RGB_565 );

Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
Canvas c = getHolder().lockCanvas(null);
c.drawBitmap(renderbmp1, left1, top1, paint);
c.drawBitmap(renderbmp2, left2, top2, paint);
getHolder().unlockCanvasAndPost(c);

renderbmp1.recycle();
renderbmp2.recycle();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号