android 開發之ScrollView 截屏代碼

來源:互聯網
上載者:User

項目要求把統計圖截屏分享,但是統計圖有5個,上層為scrollview,在網上查詢了並找到瞭解決方法:

[java]  
  1. /**
  2. * 截取scrollview的螢幕
  3. * **/
  4. public static Bitmap getBitmapByView(ScrollView scrollView) {
  5. int h = 0;
  6. Bitmap bitmap = null;
  7. // 擷取listView實際高度
  8. for (int i = 0; i < scrollView.getChildCount(); i++) {
  9. h += scrollView.getChildAt(i).getHeight();
  10. scrollView.getChildAt(i).setBackgroundResource(R.drawable.bg3);
  11. }
  12. Log.d(TAG, 實際高度: + h);
  13. Log.d(TAG, 高度: + scrollView.getHeight());
  14. // 建立對應大小的bitmap
  15. bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
  16. Bitmap.Config.ARGB_8888);
  17. final Canvas canvas = new Canvas(bitmap);
  18. scrollView.draw(canvas);
  19. // 測試輸出
  20. FileOutputStream out = null;
  21. try {
  22. out = new FileOutputStream(/sdcard/screen_test.png);
  23. } catch (FileNotFoundException e) {
  24. e.printStackTrace();
  25. }
  26. try {
  27. if (null != out) {
  28. bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
  29. out.flush();
  30. out.close();
  31. }
  32. } catch (IOException e) {
  33. // TODO: handle exception
  34. }
  35. return bitmap;
  36. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.