我有一个应用程序,它将壁纸应用到主屏幕上,图像与屏幕尺寸匹配,但是在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发布于 2012-06-20 23:00:32
编辑:添加Y偏移量修复-谢谢@Jason Goff!
好了,原来s3上主屏幕的最小宽度不是720,而是1280!您可以通过调用以下命令找到所需的墙纸最小宽度和高度
wallpaperManager.getDesiredMinimumWidth();//returned 1280 on s3
wallpaperManager.getDesiredMinimumHeight();//also returned 1280 on s3因此,为了将墙纸应用到屏幕的中心,我必须动态创建一个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();
}
});
}发布于 2014-07-24 14:15:36
感谢你的文章。我发现我的壁纸卡在我的设备屏幕的顶部,所以我稍微改变了overlayIntoCentre方法,用第二个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;}
发布于 2012-07-24 00:22:51
你可能想再看一遍这个:http://developer.android.com/reference/android/app/WallpaperManager.html#suggestDesiredDimensions(int, int)
来自开发人员参考:
public void suggestDesiredDimensions (int minimumWidth,int minimumHeight)
由于: API Level 5仅供当前家庭应用程序使用,以指定它要使用的墙纸大小。这允许这样的应用程序具有比物理屏幕更大的虚拟墙纸,与其工作空间的大小相匹配。
请注意,似乎没有阅读本文的开发人员。这是让主屏幕告诉他们想要多大尺寸的墙纸。其他人都不应该这么说!当然不会有其他改变墙纸的非主屏应用程序。这些应用程序应该检索建议的大小,以便它们可以构建与之匹配的墙纸。
https://stackoverflow.com/questions/11120012
复制相似问题