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();
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With