基于Android4.4智能TV平台,最近有客户说设置壁纸后,壁纸总是截取一部分显示,导致整张图片显示不全。
原因是在设置壁纸的时候,由于分辨率的问题,导致有图片是截图显示或是拉伸显示。例如,平台的分辨率是1280*720,如果要显示的壁纸分辨率正好是1280*720,则正好能够满屏显示。如果要显示的壁纸分辨率是1650*1050,则会被截图显示,最后看到额效果就是显示不全。如果要显示的壁纸是720*480,则会被拉伸,显示的时候由于比例问题也会被截取而显示不全。
解决办法就是在设置壁纸之前得到平台分辨率,然后根据平台分辨率缩放需要显示的壁纸,缩放后再设置壁纸。这样壁纸的分辨率与平台的分辨率一致,就是正好满屏不截取显示。
缩放函数:
public static Bitmap scaleBitmap(Bitmap srcBitmap, int targetWidth, int targetHeight) {
int srcWidth = srcBitmap.getWidth();
int srcHeight = srcBitmap.getHeight();
float scaleWidth = ((float) targetWidth) / srcWidth;
float scaleHeight = ((float) targetHeight) / srcHeight;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight); //缩放
return Bitmap.createBitmap(srcBitmap, 0, 0, srcWidth, srcHeight, matrix, true);
}
设置壁纸:
final String fullPath = mPhotoFileList.get(mCurrentPosition).getPath();
final WallpaperManager wpm = (WallpaperManager) getSystemService(Context.WALLPAPER_SERVICE);
final Bitmap bitmap = decodeBitmap(fullPath);
if (bitmap == null) {
Log.e(TAG, "Couldn't get bitmap for path!!");
} else {
try {
Bitmap scaledBitmap = scaleBitmap(bitmap, mWindowWidth,
mWindowHeight);
int bitmapWidth = scaledBitmap.getWidth();
int bitmapHeight = scaledBitmap.getHeight();
wpm.suggestDesiredDimensions(bitmapWidth, bitmapHeight);
wpm.setBitmap(scaledBitmap);
mHandler.sendEmptyMessage(SHOW_TOAST);
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "Failed to set wallpaper.");
}
}
至于decodeBitmap,后续补上。