【问题标题】:Making Bitmap fit screen as Wallpaper使位图适合屏幕作为墙纸
【发布时间】:2012-09-08 20:03:14
【问题描述】:

我想设置一个位图作为 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);

谁能帮我把壁纸图片的大小和居中正确设置为壁纸?

谢谢!

【问题讨论】:

    标签: android bitmap wallpaper


    【解决方案1】:

    试试这样的。壁纸尺寸通过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);
    

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

    【讨论】:

    • 更接近。但是,宽度已关闭,我在某处读到它会将图像拉伸到 3 个屏幕或其他东西上显示?
    猜你喜欢
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    相关资源
    最近更新 更多