以编程方式在屏幕的手机上安装Android Scrollable壁纸

时间:2016-04-06 05:21:53

标签: android android-bitmap android-wallpaper

我正在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));

1 个答案:

答案 0 :(得分:0)

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

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