2013-05-08 138 views
0

我有一个LinearLayout,其中包含很多sub LinearLayouts,其中包含TextViews。我想从父母LinearLayout获得一张截图,以全面了解我的“收据”。于是,我就这样做:从LinearLayout获取屏幕截图

View v = findViewById(R.id.llReceipt); 
v.setDrawingCacheEnabled(true); 
Bitmap b = v.getDrawingCache(); 

问题是位图b得到null值。

有没有解决方案来解决它?

+0

你可以发布你的布局 – Raghunandan 2013-05-08 09:10:12

回答

0

我不知道为什么你的位图是空,但我用这个位之前得到一个自定义视图的屏幕截图和它的工作:

public Bitmap getBitmapFromView(View view, int width, int height) { 
    Bitmap returnedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(returnedBitmap); 
    Drawable bgDrawable = view.getBackground(); 
    if (view==mainPage.boardView) { 
     canvas.drawColor(BoardView.BOARD_BG_COLOR); 
    } else if (bgDrawable!=null) { 
     bgDrawable.draw(canvas); 
    } else { 
     canvas.drawColor(Color.WHITE); 
    } 
    view.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); 
    view.layout(0, 0, width, height); 
    view.draw(canvas); 
    return returnedBitmap; 
} 

here上当受骗!

0

我发现我的问题: 我叫getScreenShot()函数从onCreate()函数不起作用。 它应该在此步骤后调用它。