开发者

Android live wallpaper onOffsetsChanged xPixelOffset doesn't seem to return the real pixel offset

开发者 https://www.devze.com 2023-01-06 04:04 出处:网络
I am writing a live wallpaper and I use the function: public void onOffsetsChanged(float xOffset, float yOffset,

I am writing a live wallpaper and I use the function:

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

To see when the uses swipes across to another screen, and I used xPixelOffset to see how far the screen has moved in pixels, however it does not return the number of pixels I would expect.

For each of the 5 screens of the desktop it shows the pixel offset to be 0,-80,-160,-240,-320 however I would expect it to be offset by the wi开发者_JAVA百科dth of the screen each time (240px rather than 80px)

Am I missing something?


You'll notice that when you 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. The obvious result is a parallax-scrolling effect.

To put it another way, end-to-end your 5 screens total something like 1200px (5 x screen width), but the maximum width of a wallpaper on the same display is something like 480px (2 x screen width), because the back doesn't scroll as much as the front.

The offsets, then, should not be used as a measure of how far the display's foreground has moved, but how far the background has moved.

So you're still getting the information that you need from the offsets parameter. It's just that your interpretation of that information has to change a bit.


Just have your canvas/bitmap that you draw on be the size of the entire virtual wallpaper (which should have dimensions screenWidth*2, screenHeight), and then draw it to the surfaceHolder canvas using the xPixelOffset as the x coordinate. That is the easy way.

Of course there is no law that your virtual wallpaper needs those dimensions. It could scroll along at the speed of the icons if you wish. That's when you'll want to use the xOffset value to figure out your own pixel offset.


          if(xPixelOffset != 0){
              myXPixelOffset = xPixelOffset*((backgroundWidth - screenwidth)/screenwidth);
          }

backgroundWidth should bee screenHeight*2

0

精彩评论

暂无评论...
验证码 换一张
取 消