如何在android中的画布中对齐中心位图?

KIR*_*K J 16 android android-layout

我在canvas.i上有一个位图需要将位图对齐到画布的中心(图像的中心应该在画布的中心.在我的要求中,不应该给定位点对齐位图).而纵向视图如何缩放画布和适合分辨率的图像?

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

    }
Run Code Online (Sandbox Code Playgroud)

mbw*_*asi 45

虽然我不知道在Android中获取画布宽度和位图宽度的具体方法,但在中心放置位图通常会是这样的.

centreX = (canvasWidth  - bitmapWidth) /2

centreY = (canvasHeight - bitmapHeight) /2
Run Code Online (Sandbox Code Playgroud)

然后将您的图像放在centreX,centreY