1

我的应用程序是一个启动器,现在我需要一个当前视图的位图。启动器的视图可以通过 getDrawingCache() 检索,如下所示

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

但位图只是启动器的视图,不包含壁纸。

如何获得带壁纸的位图?

4

1 回答 1

0

好吧,我不得不说没有单一的API提供这个功能,用户必须复合两个位图,一个是壁纸部分,另一个是启动器的桌面视图。通常,启动器会将壁纸扩展到多个屏幕,因此我们必须获取壁纸的可见部分。

以下是我的做法:

    //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
于 2014-08-05T03:08:54.173 回答