我试图使用WallpaperManager将位图设置为我手机壁纸的中心位置。
我尝试了很多策略:
答案 0 :(得分:0)
我找到了答案:Scaled Bitmap maintaining aspect ratio
我做的是:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int phoneHeight = metrics.heightPixels;
int phoneWidth = metrics.widthPixels;
Bitmap mBit = returnBitmap(mImage, phoneWidth, phoneHeight);
private Bitmap returnBitmap(Bitmap originalImage, int width, int height){
Bitmap background = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);
float originalWidth = originalImage.getWidth();
float originalHeight = originalImage.getHeight();
Canvas canvas = new Canvas(background);
float scale = width / originalWidth;
float xTranslation = 0.0f;
float yTranslation = (height - originalHeight * scale) / 2.0f;
Matrix transformation = new Matrix();
transformation.postTranslate(xTranslation, yTranslation);
transformation.preScale(scale, scale);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(originalImage, transformation, paint);
return background;
}