设置壁纸

时间:2013-01-25 14:27:23

标签: android wallpaper

我试图将我的位图图像设置为壁纸!

我目前正在使用此代码来完成它。

            WallpaperManager wpm = WallpaperManager.getInstance(this);

            float minH =  wpm.getDesiredMinimumHeight();
            float minW =  wpm.getDesiredMinimumWidth();
            Log.d("seb", minH + " = Min Height");
            Log.d("seb", minW + " = Min Width");
            targetBitmap = Bitmap.createScaledBitmap(targetBitmap,(int)minW, (int)minH, false);         
            wpm.setBitmap(targetBitmap);

有效!手机会自动调整位图的大小以适应屏幕,但不管我的位图有多小,它总是水平裁剪并按比例放大。

有谁知道如何解决这个问题?

(一个修复方法是将黑色边框放在一边,让它们裁剪而不是实际图片,不过我猜是有更好的选择)

修改

This is original picture

这是代码中的原始图片。

以下图片是我设置为壁纸时裁剪的意思:

Cropped picture

即使我调整图像大小,这也是一样的,因为系统自动放大图片以适应整个屏幕

1 个答案:

答案 0 :(得分:0)

好的,所以在我们讨论“评论”之后,这里有一个可行的解决方案。

试试:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

    int width = bm.getWidth();

    int height = bm.getHeight();

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

// create a matrix for the manipulation

    Matrix matrix = new Matrix();

    // resize the bit map

    matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

    return resizedBitmap;

}

尝试一下,如果您遇到相同(或其他)问题,请告诉我们,我们会尽力解决。