如何对Scrollview进行截屏,这种需求一般是作为分享截屏时需要的·,查了很多方法,开始是和我前面写的对webview截屏的方法做参考,后来发现完全不合要求,毕竟Scrollview是布局不是View控件。
废话不多说,见核心部分:主要是计算当前scrollview的总高度,超过手机屏幕的高度了。
使用for循环递归累加其内部的子控件的高度:
- private ScrollView scrollView;
- scrollView = (ScrollView) findViewById(R.id.scrollview);
- int h = 0;
- for (int i = 0; i < scrollView.getChildCount(); i++) {
- h += scrollView.getChildAt(i).getHeight();}
- Bitmap bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
- Bitmap.Config.ARGB_8888);
- // Bitmap bitmap = scrollView.getDrawingCache(true);
- final Canvas c = new Canvas(bitmap);
- scrollView.draw(c);
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
- final byte[] picture = stream.toByteArray();
- if (bitmap != null && !bitmap.isRecycled()) {
- bitmap = null;// 把原来的 bitmap.recycle().改成这个
- }
代码如上即可给Scrollview进行截屏并转换为bitmap,和byte[]数组,你可以根据自己需要选择使用·········