android Scrollable wallpaper on screen's phone programmatically

拟墨画扇 提交于 2019-12-11 23:35:31

问题


I am developing a wallpaper application in Android and i am finding a right way to set scrollable wallpaper for my app. Now, my code can set wallpaper from bitmap but it was cropped to fit with one page and just stayed only on one page (i have 5 pages in home screen). That means the content in each page can scroll through the wallpaper but the wallpaper was not scroll.

I want to set a scrollable wallpaper. I tried some codes from internet but they did not help. Do you guys have any idea?

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

And I use from this code but don't work :

//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:


Post is old but anyway... You can try something similar to this

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


来源:https://stackoverflow.com/questions/36442175/android-scrollable-wallpaper-on-screens-phone-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!