可滚动壁纸android

时间:2014-12-07 05:54:59

标签: android wallpaper

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

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

这是我的代码

WallpaperManager wm = WallpaperManager.getInstance(mActivity.getContext());
    try {
        wm.setBitmap(mCropImageView.getCroppedImage());
    } catch (IOException e) {
        e.printStackTrace();
    }

2 个答案:

答案 0 :(得分:2)

试试这个,它适用于api> 11

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

我想在我的应用中 中有一个可滚动的墙纸作为背景。 (主屏幕替换应用)

从我的应用设置墙纸(可以滚动)。

但是当搜索scrollable wallpaper android stackoverflow时,这个问题就出现在Google上的第一篇SO帖子中。

所以我决定回答这个问题。

我使用Kotlin制作了一个位于here的演示应用程序

它使用this方法滚动壁纸。

使用WallpaperManager.setWallpaperOffsets

addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
    override fun onPageScrollStateChanged(state: Int) {}

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        val xOffset = (position + positionOffset) / (pageModels.size - 1)
        val wallpaperManager = WallpaperManager.getInstance(applicationContext)
        wallpaperManager.setWallpaperOffsets(viewPager.windowToken, xOffset, 0.0f)
    }
}

还有我制作的video

应用的演示