【问题标题】:android Scrollable wallpaper on screen's phone programmatically以编程方式在屏幕手机上的android可滚动壁纸
【发布时间】:2016-04-06 05:21:53
【问题描述】:

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

【问题讨论】:

    标签: android android-bitmap android-wallpaper


    【解决方案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();
        }
    }
    

    【讨论】:

    • 哦,非常感谢!看来,它有效!但是,您忘记了wallpaperManager.suggestDesiredDimensions(width, height); 行的权限 android.permission.SET_WALLPAPER_HINTS
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多