Android 即使尺寸与屏幕匹配,WallperManager也会缩放图像
我有一个应用程序,将墙纸应用到主屏幕,图像与屏幕大小匹配,但是在galaxy s3上,当我应用墙纸时,墙纸会放大,即使使用的图像与屏幕尺寸完全匹配!它是一个静态图像,在主屏幕上切换页面时甚至不会滚动。奇怪的是,如果我使用内置图库应用图像,墙纸在没有缩放的情况下应用得很好(它会出现“裁剪图像”屏幕,但裁剪区域本身与图像的边缘匹配) 我使用的代码在一系列手机(galaxy note、ace 2、s2等)上运行良好,但在s3上运行不好;我想知道我能做些什么来强制壁纸正确地填充屏幕?我当前用于应用墙纸的代码是:Android 即使尺寸与屏幕匹配,WallperManager也会缩放图像,android,wallpaper,homescreen,Android,Wallpaper,Homescreen,我有一个应用程序,将墙纸应用到主屏幕,图像与屏幕大小匹配,但是在galaxy s3上,当我应用墙纸时,墙纸会放大,即使使用的图像与屏幕尺寸完全匹配!它是一个静态图像,在主屏幕上切换页面时甚至不会滚动。奇怪的是,如果我使用内置图库应用图像,墙纸在没有缩放的情况下应用得很好(它会出现“裁剪图像”屏幕,但裁剪区域本身与图像的边缘匹配) 我使用的代码在一系列手机(galaxy note、ace 2、s2等)上运行良好,但在s3上运行不好;我想知道我能做些什么来强制壁纸正确地填充屏幕?我当前用于应用墙纸的
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
wallpaperManager.setWallpaperOffsets(wallpaperViewer.getApplicationWindowToken(), 0, 0);
wallpaperManager.setBitmap(BitmapFactory.decodeFile(file.getPath()));//file is jpg on sd card
编辑:添加了Y偏移修正-谢谢@Jason Goff 好的,s3上主屏幕的最小宽度不是720,而是1280!您可以通过拨打电话找到所需的墙纸最小宽度和高度
wallpaperManager.getDesiredMinimumWidth();//returned 1280 on s3
wallpaperManager.getDesiredMinimumHeight();//also returned 1280 on s3
<> P>所以为了将壁纸应用到屏幕的中心,我必须在空白处创建一个空白的位图1280x1280,然后将我的墙纸覆盖到空白位图的中心,我用一种调整位图的方法创建了一个静态的BITMAPHELPER类,这里是创建位图和覆盖壁纸图像的方法:
public class BitmapHelper {
public static Bitmap overlayIntoCentre(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);//draw background bitmap
//overlay the second in the centre of the first
//(may experience issues if the first bitmap is smaller than the second, but then why would you want to overlay a bigger one over a smaller one?!)
//EDIT: added Y offest fix - thanks @Jason Goff!
canvas.drawBitmap(bmp2, (bmp1.getWidth()/2)-(bmp2.getWidth()/2), (bmp1.getHeight()/2)-(bmp2.getHeight/2), null);
return bmOverlay;
}
public static Bitmap createNewBitmap(int width, int height)
{
//create a blanks bitmap of the desired width/height
return Bitmap.createBitmap(width, height, Config.ARGB_8888);
}
}
下面是我使用BitmapHelper的其余代码:
private void applyWallpaperFromFile(final File file)
{
Bitmap wallpaperImage = BitmapFactory.decodeFile(file.getPath());
try {
if((wallpaperManager.getDesiredMinimumWidth()>0)&&(wallpaperManager.getDesiredMinimumHeight()>0))
{
Bitmap blank = BitmapHelper.createNewBitmap(wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight());
Bitmap overlay = BitmapHelper.overlayIntoCentre(blank, wallpaperImage);
wallpaperManager.setBitmap(overlay);
}
else
{
wallpaperManager.setBitmap(wallpaperImage);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(WallpaperActivity.this,"Wallpaper set to:"+file.getName(), Toast.LENGTH_SHORT).show();
}
});
}
您可以尝试使用suggestDesiredDimensions()方法您可能希望再次了解这一点: 从开发者参考: 建议的公共空间所需尺寸(int minimumWidth、int minimumHeight) 自:API 5级 仅供当前主应用程序使用,请执行以下操作: 指定要使用的墙纸的大小。这就允许这样做 应用程序的虚拟墙纸要大于 物理屏幕,与工作区大小匹配 请注意开发人员,他们似乎没有读到这篇文章。这是回家的 屏幕显示他们想要什么尺寸的壁纸。没有其他人 应该叫这个!当然不是其他非主屏幕应用程序 换壁纸。这些应用程序应该能够检索 建议的尺寸,以便他们可以构建与之匹配的墙纸
谢谢你的文章。我发现我的壁纸卡在了设备屏幕的顶部,所以我稍微改变了OverlayToCenter方法,用第二次
drawBitmap
调用中的0替换下面的高度计算
public static Bitmap overlayIntoCentre(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);//draw background bitmap
//overlay the second in the centre of the first
//(may experience issues if the first bitmap is smaller than the second, but then why would you want to overlay a bigger one over a smaller one?!)
canvas.drawBitmap(bmp2, (bmp1.getWidth()/2)-(bmp2.getWidth()/2), (bmp1.getHeight()/2)-(bmp2.getHeight/2), null);
return bmOverlay;
}Per,该方法仅适用于启动器应用程序。很好!我的代码假设叠加位图的高度对于设备是正确的,因此不需要居中vertically@Jason戈夫-是的-做了完全相同的事情,找出了问题所在-然后检查了你的评论。在Y偏移修复后效果很好。感谢a tone@AndroidNoob-为精确设置墙纸苦苦挣扎了一段时间-最终通过对代码的细微调整获得了它。基本上必须设置Y偏移。你是我的救命恩人!这件事我已经忙了半天了。非常感谢:)