Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making android live wallpaper with parallax scrolling

Tags:

android

I'm creating a live wallpaper with parallax scrolling. I've read the article: Parallax effect scrolling of live wallpaper background. But when I change desktops the background moves wrong way (If I change desktop from left to right, picture moves from right to left). How to change direction? Code snippet:

public void Init(Bitmap bitmap){
  bg = new BitmapFactory().decodeResource(context.getResources(), R.drawable.thunder);
  bg = Bitmap.createScaledBitmap(bg, (int)(width*1.4), height, true);
}

float dx = 0.0f; 
@Override
    public void onOffsetsChanged(float xOffset, float yOffset,
            float xStep, float yStep, int xPixels, int yPixels) {
        dx = (width - bg.getWidth()) * (1 - xOffset);
    } 

private void doDraw(Canvas canvas) {
    canvas.save();
    canvas.translate(dx, 0);
    canvas.drawBitmap(bg, 0, 0, null);

    canvas.restore();
}   
like image 656
Nolesh Avatar asked Oct 20 '12 08:10

Nolesh


People also ask

How does Parallax wallpaper work?

Parallax wallpapers work by zooming in on an image a bit, so there is a little bit of the image outside viewable area. That way, when you change perspective, it can “move” the wallpaper in a way that makes it appear 3D.


1 Answers

I know this is an old issue, but since this is a high result on Google, all you need to do to is flip the sign on your translate.

canvas.translate(-dx, 0);

This will effectively reverse the direction the background scrolls when the user swipes. Hope this helps others.

like image 62
Jawnnypoo Avatar answered Nov 15 '22 05:11

Jawnnypoo