2013-04-19 105 views
-1

是否有可能得到的观点背后的意见绘图缓存?例如,我有一个半透明的视图,我可以看到它背后的意见。那么,我可以在后面的视图中看到这个视图的图形缓存吗?View.getDrawingCache()与背景半透明,

代码在那里我加入了以WM:

final View screenshotView = new View(this) { 
        @Override 
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
         super.onLayout(changed, left, top, right, bottom); 

         if (!changed) { 
          buildDrawingCache(); 
          final Bitmap bitmap = Bitmap.createBitmap(getDrawingCache()); 
          final File file = new File("/sdcard/test.png"); 
          try { 
           file.createNewFile(); 
           FileOutputStream ostream = new FileOutputStream(file); 
           bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream); 
           ostream.close(); 
          } catch (Exception e) { 
           Log.e(TAG, e.toString()); 
          } 
         } 
        } 
       }; 

WindowManager.LayoutParams screenShotViewLp = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.RGBA_8888); 
       screenShotViewLp.width = WindowManager.LayoutParams.MATCH_PARENT; 
       screenShotViewLp.height = WindowManager.LayoutParams.MATCH_PARENT; 
       screenShotViewLp.gravity = Gravity.LEFT | Gravity.TOP; 

       wm.addView(screenshotView, screenShotViewLp); 

回答

1

[编辑]根据我觉得这个答案不适合了这个问题的最后编辑。直接添加视图到窗口管理INFACT讨论引向一个众所周知的问题:在Android的屏幕截图的编程是不允许的,然后下面露出的想法似乎完全不切实际。 所以请不要低估。 [/编辑]

AFAIK这不能通过单个视图的drawingCache实现。为了得到背景中的其他视图,您应该采用视图树中最顶端节点的drawingCache,即包含Activity的内容。获得这样的位图是非常简单的:

View root = currActivity.getWindow().getDecorView().findViewById(android.R.id.content); 
root.setDrawingCacheEnabled(true); 
Bitmap bmp = root.getDrawingCache(); 

然后,为了只拿到位你感兴趣的部分,你不得不根据View.getLocatonInWindow(int[])值裁剪生成位图。

还没有证明自己,但我相当有信心它应该工作。

+0

我的看法是从服务直接添加到窗口管理器没有活动 – arts777

+0

这个复杂的事情有点:)你可以发布用于连接视图到'WindowManager'的代码? –

+0

我已经添加了代码的问题 – arts777