android live wallpapers parallax-scrolling effect

大憨熊 提交于 2019-12-20 05:41:41

问题


when we scroll, the foreground of the home screen (icons, widgets, etc.) moves to the left or right by the full screen width, but the background image (or live wallpaper) only moves by a fraction of that width. My question is how get this effect. till now have done this.

    SurfaceHolder holder = getSurfaceHolder();
        Canvas canvas = null;
        try {
            canvas = holder.lockCanvas();
            if (canvas != null) {

                drawCircles(canvas);
            }
        } finally {
            if (canvas != null)
                holder.unlockCanvasAndPost(canvas);
        }

the draw function is

{
    private void draw(Canvas canvas) {
        Paint paint = new Paint();
        DisplayMetrics metdisplayMatrics = new DisplayMetrics();
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        display.getMetrics(metdisplayMatrics);

            canvas.save();
            canvas.drawColor(0xff000000);
            mRecscreenRectangleFrame = new Rect(0, 0,  (int) (metdisplayMatrics.widthPixels*2.0), metdisplayMatrics.heightPixels);
            photo1= BitmapFactory.decodeResource(getResources(), R.drawable.img1);
            canvas.drawBitmap(photo1, null,mRecscreenRectangleFrame, paint);
            photo1.recycle();
            System.gc();
}               

Now how to put live wallpapers parallax-scrolling effect.

    @Override
    public void onOffsetsChanged(float xOffset, float yOffset,
            float xOffsetStep, float yOffsetStep, int xPixelOffset,
            int yPixelOffset) {

        super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep,
                xPixelOffset, yPixelOffset);

        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        View view=new View(getBaseContext());

        myWallpaperManager.setWallpaperOffsets(view.getWindowToken(),xOffset, 0f);

    }

Not working yet.................


回答1:


Call WallpaperManager.setWallpaperOffsets to instruct the wallpaper to scroll.

Documentation

So this should center the wallpaper:

WallpaperManager.setWallpaperOffsets(getWindowToken(), 0.5f, 0f);

This should scroll it to the side:

WallpaperManager.setWallpaperOffsets(getWindowToken(), 0f, 0f);

This should scroll it to the other side:

WallpaperManager.setWallpaperOffsets(getWindowToken(), 1f, 0f);

If you're going to do this, you ought to ensure that you know that the wallpaper can actually be scrolled, or that the user has asked you to enable scrolling. Many devices are configured with wallpaper that is the same size as the screen and does not scroll.



来源:https://stackoverflow.com/questions/15925114/android-live-wallpapers-parallax-scrolling-effect

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