night mare
night mare

Reputation: 129

Drawing multiple images on Canvas in Android

I am trying to draw multiple images on a canvas and display it but I just don't know how to.

Here's my code:

public class CustomDrawableView extends View {
private Drawable mDrawable;
private Drawable mD2;

public CustomDrawableView(Context context) {
    super(context);

    int x = 40;
    int y = 100;
    int width = 20;
    int height = 10;

    Resources res = context.getResources();
    mDrawable = res.getDrawable(R.drawable.main);
    mDrawable.setBounds(x, y, x + width, y + height);

    mD2 = res.getDrawable(R.drawable.virus);
    mD2.setBounds(x+50,y-70,width+10,height+5);
}

protected void onDraw(Canvas canvas) {
    mDrawable.draw(canvas);
    Canvas canvas2 = new Canvas();
    mD2.draw(canvas2);
}
}

Upvotes: 1

Views: 10031

Answers (2)

user710502
user710502

Reputation: 11471

You dont need to create a new instance of a canvas, just do md2.draw(canvas). And also you may try:

what happens when you draw only mDrawable, and then only md2, do they show?

Upvotes: 2

zrgiu
zrgiu

Reputation: 6322

I haven't worked with Drawables on a Canvas, but I can tell your for sure that with bitmaps it works just fine like this:

canvas.drawBitmap(bmp1,0,0,myPaint); // draws in top left corner
canvas.drawBitmap(bmp2,100,100, myPaint); // draws at an offset of 100 px on both the X and the Y axis

Upvotes: 4

Related Questions