Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to align center a bitmap inside a canvas in android?

i have a bitmap on canvas.i need to align bitmap to centre of canvas(centre of the image should be in centre of canvas.in my requirement,there should not give fixed points to align bitmap).while portrait view how to scale canvas and image appropriate to resolution?

    public void onDraw(Canvas canvas) {
    Bitmap imgtable = BitmapFactory.decodeResource(getResources(), R.drawable.table_01);
    canvas.drawColor(Color.TRANSPARENT);
    canvas.drawBitmap(imgtable, 10, 10, null);

    }
like image 520
KIRAN K J Avatar asked Jul 22 '11 05:07

KIRAN K J


1 Answers

While i do not know the specific methods of getting the canvas width and bitmap width in Android, to place an bitmap at the center usually goes something like this.

centreX = (canvasWidth  - bitmapWidth) /2

centreY = (canvasHeight - bitmapHeight) /2

Then place your image at centreX,centreY

like image 84
mbwasi Avatar answered Nov 05 '22 06:11

mbwasi