3
以我的自定義視圖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
。有任何想法嗎?
有沒有幫助?讓我知道你是否需要更具體的代碼 –