2

Androidの壁紙としてビットマップを設定したいのですが、その部分を理解しました。ただし、画像は常に大きすぎ、中心からずれており、トリミングされています。ビットマップのサイズを表示サイズに変更しようとしましたが、それでも同じ結果が得られますが、コードの一部です。

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);

壁紙の画像のサイズと中央を壁紙として正しく配置するのを手伝ってくれる人はいますか?

ありがとう!

4

1 に答える 1

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);

これはアスペクト比を維持しませんが、どうなるか教えてください。

于 2012-09-08T20:12:52.893 に答える