android 开发之ScrollView 截屏代码

来源:互联网 发布:linux tomcat安装 用户 编辑:程序博客网 时间:2024/05/15 23:43

项目要求把统计图截屏分享,但是统计图有5个,上层为scrollview,在网上查询了并找到了解决方法:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  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.    }  
0 0