Search code examples
androidwindowandroid-wallpaper

how to getDrawingCache() with wallpaper


My application is a launcher, now I need a bitmap of current view. Launcher's view can be retrived by getDrawingCache(), like following

   Bitmap bitmap = Bitmap.createBitmap(screen_w, screen_h, Config.ARGB_8888 );       
   View decorview = getWindow().getDecorView(); 
   decorview.setDrawingCacheEnabled(true);    
   bitmap = decorview.getDrawingCache();  

But the bitmap is only of launcher'view, NOT containing wallpaper.

How can I get the bitmap with wallpaper ?


Solution

  • OK, I had to say there is NO single API provide this function, user has to compound two bitmap, one is wallpaper part, the other is launcher's desktop view. Generally, launcher extends wallpaper to several screens, so we have to get wallpaper's visible part.

    Following is how I do:

        //get wallpaper's visible part
        //@param screenCount  how many screens launcher has
        //@param screenCurrent  current screen index
        Bitmap wallpaper = 
           ((BitmapDrawable) WallpaperManager.getInstance(context).getDrawable()).getBitmap();
        float step = 0;
        step = (wallpaper.getWidth() - w) / (screenCount - 1);
        Bitmap wallpaperCurrent = 
        Bitmap.createBitmap(wallpaper, (int)(screenCurrent * step), 0, w, h);
    
        //get launcher's visible view bitmap
        View decorview = context.getWindow().getDecorView();     
        decorview.buildDrawingCache();
        Bitmap desktopBitmap = decorview.getDrawingCache(); 
    
        //compound bitmaps
        Bitmap compoundBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    
        Canvas canvas = new Canvas(compoundBitmap);  
        canvas.drawBitmap(wallpaperCurrent, 0, 0, null);
        canvas.drawBitmap(desktopBitmap, 0, 0, null);
    
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
    
        // return compoundBitmap