Search code examples
androidbitmaploadlivewallpaper

Android LiveWallpaper: load and draw various bitmaps efficiently (example: video)


I am developing a live wallpaper that reproduces an animation (like a short video) on background.

Does anyone know how to efficiently load various full screen size bitmaps and draw them on canvas? I have tried 2 approaches and both are not very good.


Approach #1: Load all bitmaps on wallpaper startup.

Problem: Memory excess limit (about 35MB) cant load more than 10 bitmaps. So animations lack of different images.


Approach #2: Load only 2 bitmaps. On run time, paint bitmap, delete old bitmap, load new bitmap, repeat.

Problem: Consumes a lot of the system, (not memory, but slows down os in general), however it works because it doesn't exceed memory limit. But still, slows down the entire system.

example:

  Drawer.drawAll(res,c,p);
  res.removeOldBitmaps();
  res.loadNewBitmaps(wpservice,display);

A different approach i thought about is load resource on a separete thread, what do you guys think of that? do you have any other solutions?

Cheers!


Solution

  • Solved, the solution I found was the following:

    Load images on runtime, 1 by 1.

    And use this options:

    /**Effective image decoder by chelin**/
    public static Bitmap decodeResource(WallpaperService wpservice,int id){
    
        int scale=1;
        Resources res = wpservice.getResources();
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
        o2.inSampleSize=scale;
        o2.inPurgeable = true;
        o2.inInputShareable = true;
        return BitmapFactory.decodeResource(res, id, o2);
    }
    

    Hope it works as it worked for me. Cheers!