简体   繁体   中英

Android: Can I have layers in a live wallpaper?

I want to create a live wallpaper, and I want to have the bottom background slide together with swiping homescreen pages, while another layer stays always on top of the background and under the app icons.

Is this possible and how can this be done?

You'll have to override public void onOffsetsChanged (float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) Using the value of xOffset , you can define a source rectangle that extracts part of your bitmap and draws that part on the screen.

This image should give you an understanding of how xOffset works: Assuming that there are 5 homescreen pages, android xOffset示例 If your picture is of size 960 x 800 (width x height) and if you want to draw a portion of size 480 x 800 every time, then you can define a source rectangle whose co-ordinates would be:

x1 = xOffset * (960 - 480); y1 = 0; x2 = x1 + 480, y2 = 800;

Then your destination rectangle would be the portion of the screen you want to draw onto. You can then use public void drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint) method to draw the bitmap onto the screen.

I had used this technique a long time ago. I didn't check this in code before posting, and there may be alternatives (like using canvas.translate()). But hopefully this should help you get started. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM