我正在加载要绘制的位图,但它距屏幕右侧约 25 像素。我不确定为什么会这样。
scaleWallpaperBitmap(R.drawable.board2);
canvas.drawBitmap(board, 0, topOffset, null);
private void scaleWallpaperBitmap(int res) {
// read bitmap in project
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), res, options);
int imgH = options.outHeight;
int imgW = options.outWidth;
// get new bitmap and find how to center it
if (imgH == imgW) {
// easy case of a square wallpaper
if (screenH > screenW) {
board = decodeSampledBitmapFromResource(getResources(), res,
screenW, screenW);
verticalBitmapOffset = true;
horizontalBitmapOffset = false;
topOffset = (screenH - board.getHeight()) / 2;
} else {
board = decodeSampledBitmapFromResource(getResources(), res,
screenH, screenH);
horizontalBitmapOffset = true;
verticalBitmapOffset = false;
leftOffset = (screenW - board.getWidth()) / 2;
}
} else {
// ratio is now involved
}
}
其余方法来自此处的 android 文档...
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
图像几乎完全缩放并居中,垂直居中,但它不是应有的全屏宽度。右边距有大约 25 px 的可用空间。
任何反馈都会很棒!
谢谢