0

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.................

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Imran Khan
  • 158
  • 1
  • 3
  • 13

1 Answers1

3

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.

j__m
  • 9,392
  • 1
  • 32
  • 56
  • thanks j__m please if you can give me the answer in details I am new to android. – Imran Khan Apr 10 '13 at 12:18
  • A newly-created View is not going to have a window token. You have to call getWindowToken() on something that's already visible, like your SurfaceView. – j__m Apr 15 '13 at 14:22
  • I have surface holder and don't have surface view . how to get the window token. – Imran Khan Apr 23 '13 at 07:30
  • ...i just realized you are creating a live wallpaper -_-;; the way your question is worded, it sounded like you were creating an app (like the launcher) that draws on top of the wallpaper. setWallpaperOffsets is the function the launcher calls to invoke your onOffsetsChanged function; you shouldn't call setWallpaperOffsets from inside a live wallpaper. Instead, you need to change the coordinates in your draw() function. – j__m Apr 26 '13 at 03:12
  • So give an example of Moving background image while navigating through five home screen views – Imran Khan May 01 '13 at 09:48
  • just save the xOffset to a variable and then redraw your wallpaper. right before canvas.drawBitmap, add a line like "canvas.translate(-metdisplayMatrics.widthPixels * xOffset, 0);" – j__m May 01 '13 at 14:41
  • Geat its working now **but it is not smooth motion like android native static wallpaper** – Imran Khan May 03 '13 at 10:43
  • If you want it smooth, you're going to have to redraw your wallpaper multiple times, and each time _translate_ the canvas by a different value. – j__m May 09 '13 at 13:38