0

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

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

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

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

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

(一个解决方法是在周围放置一个黑色边框并裁剪它们而不是实际图片,尽管我猜有更好的选择)

编辑

这是原图

这是代码中的原始图片。

下图是我设置为墙纸时裁剪的意思:

裁剪图片

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

4

1 回答 1

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;

}

试试看,如果您有相同(或其他)问题,请告诉我,我们会尽力解决。

于 2013-01-28T08:44:51.337 回答