項目要求把統計圖截屏分享,但是統計圖有5個,上層為scrollview,在網上查詢了並找到了解決方法:
[java]
- /**
- * 截取scrollview的屏幕
- * **/
- public static Bitmap getBitmapByView(ScrollView scrollView) {
- int h = 0;
- Bitmap bitmap = null;
- // 獲取listView實際高度
- for (int i = 0; i < scrollView.getChildCount(); i++) {
- h += scrollView.getChildAt(i).getHeight();
- scrollView.getChildAt(i).setBackgroundResource(R.drawable.bg3);
- }
- Log.d(TAG, 實際高度: + h);
- Log.d(TAG, 高度: + scrollView.getHeight());
- // 創建對應大小的bitmap
- bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
- Bitmap.Config.ARGB_8888);
- final Canvas canvas = new Canvas(bitmap);
- scrollView.draw(canvas);
- // 測試輸出
- FileOutputStream out = null;
- try {
- out = new FileOutputStream(/sdcard/screen_test.png);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- try {
- if (null != out) {
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
- out.flush();
- out.close();
- }
- } catch (IOException e) {
- // TODO: handle exception
- }
- return bitmap;
- }