1

我正在 Android 中开发一个壁纸应用程序,我正在寻找一种为我的应用程序设置可滚动壁纸的正确方法。现在,我的代码可以从位图设置壁纸,但它被裁剪以适合一页并且只停留在一页上(我在主屏幕上有 5 页)。这意味着每个页面中的内容可以滚动浏览墙纸,但墙纸不能滚动。

我想设置一个可滚动的壁纸。我尝试了一些来自互联网的代码,但它们没有帮助。你们有什么想法吗?

setImage_Wallpaper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = imageLoader.getDiscCache().get(urldisplay);
                Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                WallpaperManager myWallpaperManager
                        = WallpaperManager.getInstance(mContext);
                try {
                    myWallpaperManager.setBitmap(bitmap);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

我使用此代码但不起作用:

//get screen height
Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    screenHeight = size.y;

 wallPaperBitmap= ... //your bitmap resource

//adjust the aspect ratio of the Image
//this is the main part

int width = wallPaperBitmap.getWidth();
        width = (width * screenHeight) / wallPaperBitmap.getHeight();

//set the wallpaper
//this may not be the most efficent way but it worked for me

wallpaperManager.setBitmap(Bitmap.createScaledBitmap(wallPaperBitmap, width, height, true));
4

1 回答 1

1

帖子很旧,但无论如何......你可以尝试类似的东西

public static void setWallpaper(Context context) {
    int wallpaperRId = getWallpaperImageRid(context);

    if (wallpaperRId == 0) {
        return;
    }

    Bitmap tempBmp = BitmapFactory.decodeResource(context.getResources(), wallpaperRId);

    // get size
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;
    int area = width * height / 1000;

    width *= 2;
    float scale = width / (float) tempBmp.getWidth();
    height = (int) (scale * tempBmp.getHeight());

    Bitmap bitmap = Bitmap.createScaledBitmap(tempBmp,width,height, true);

    WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
    wallpaperManager.setWallpaperOffsetSteps(1, 1);
    wallpaperManager.suggestDesiredDimensions(width, height);

    try {
        wallpaperManager.setBitmap(bitmap);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
于 2016-12-27T14:32:46.413 回答