简体   繁体   中英

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

    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM