如何给Scrollview里内容截屏并生成bitmap,注意: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[]数组,你可以根据自己需要选择使用·········

 

猜你喜欢

转载自geyubin.iteye.com/blog/1680300