我有一个应用程序,将壁纸应用到主屏幕上,其图像与屏幕尺寸相匹配,但是在我应用它时,在壁纸s3上壁纸会被放大,即使使用的图像与屏幕尺寸完全匹配!它是静态图像,在主屏幕上切换页面时甚至不滚动.奇怪的是,如果我使用内置图库应用图像,壁纸应用得很好而没有缩放(它出现’裁剪图像’屏幕,但裁剪区域本身匹配图像的边缘)
我使用的代码在整个手机阵列上工作正常(星系注释,ace 2,s2等)但不在s3上;我想知道是否有什么可以强迫壁纸正确填满屏幕?我用来应用壁纸的当前代码是:
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); wallpaperManager.setWallpaperOffsets(wallpaperViewer.getapplicationwindowToken(),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
因此,为了将壁纸应用到屏幕中心,我必须动态创建一个空白位图1280×1280,然后将我的壁纸覆盖到空白位图的中心,我创建了一个静态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();
}
});
}