在我的自定义视图的
如果
OnDraw
方法中,我使用Bitmap
绘制到Canvas
的中心位置。protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect r = canvas.getClipBounds();
displayWidth = r.right;
displayHeight = r.bottom;
camera.applyToCanvas(canvas);
float zW = (float)bitmapWidth / (float)displayWidth;
float zH = (float)bitmapHeight / (float)displayHeight;
float z = 0.0f;
if (zW>1 || zH>1) {
z = Math.max(zW, zH);
}
canvas.drawColor(Color.DKGRAY);
canvas.drawBitmap(bitmap, (displayWidth/2.0f - (bitmapWidth)/2.0f), (displayHeight/2.0f - bitmapHeight/2.0f), paint);
if (z>0) {
camera.translate(z, -z, z);
}
}
如果
Bitmap
的高度或宽度大于Canvas
大小(displayWidth, displayHeight
),我该如何使用Camera
类自动缩放以适应Bitmap
并将其居中到Canvas
中。有什么想法吗?