2012-09-08 80 views
1

我想设置一个位图作为androids壁纸,我已经找到了那部分。然而,图像总是太大,偏离中心并被裁剪。我试图调整位图的显示大小,但我仍然得到相同的结果,这里是我的一些代码。制作位图适合屏幕壁纸

Display display = getWindowManager().getDefaultDisplay(); 

final int maxWidth = display.getWidth(); 
final int maxHeight = display.getHeight(); 

Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(img_url).getContent());  


    int imageHeight = bitmap.getHeight(); 
if (imageHeight > maxHeight) 
imageHeight = maxHeight; 
int imageWidth = (imageHeight*bitmap.getWidth())/bitmap.getHeight(); 
if (imageWidth > maxWidth) { 
imageWidth = maxWidth; 
imageHeight = (imageWidth*bitmap.getHeight())/bitmap.getWidth(); 
} 


Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, imageWidth, imageHeight, true); 

    myWallpaperManager.setBitmap(resizedBitmap); 

谁能帮我把壁纸图片的大小和正确集中的壁纸?

谢谢!

回答

0

尝试类似这样的事情。壁纸尺寸通过WallpaperManager.getDesiredMinimumHeightWallpaperManager.getDesiredMinimumWidth获取。如果其中任何一个是<= 0,它都会请求默认显示的尺寸。

/* determine wallpaper dimensions */ 
final int w = myWallpaperManager.getDesiredMinimumWidth(); 
final int h = myWallpaperManager.getDesiredMinimumHeight(); 
final boolean need_w = w <= 0; 
if (need_w || h <= 0) { 
    final Rect rect = new Rect(); 
    getWindowManager().getDefaultDisplay().getRectSize(rect); 
    if (need_w) { 
    w = rect.width(); 
    } else { 
    h = rect.height(); 
    } 
} 

/* create resized bitmap */ 
final Bitmap resized = Bitmap.createScaledBitmap(bitmap, w, h, false); 

这不会保留纵横比,但告诉我它是怎么回事。

+0

更接近。宽度是关闭的,但是,我读过的地方是它延伸图像显示在3个屏幕或什么? – Dan