当视图超过一个屏幕的时候,可能是listview,也可能是Scrollview,这时候,其实截图就是对listview或者Scrollview进行截图
/**
* 截取scrollview的屏幕* **/
public static Bitmap getScrollViewBitmap(ScrollView scrollView,String picpath) {
int h = 0;
Bitmap bitmap;
// 获取listView实际高度
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
}
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(picpath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (null != out) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
}
return bitmap;
}
private static String TAG = "Listview and ScrollView item 截图:";
/**
* 截图listview
* **/
public static Bitmap getListViewBitmap(ListView listView,String picpath) {
int h = 0;
Bitmap bitmap;
// 获取listView实际高度
for (int i = 0; i < listView.getChildCount(); i++) {
h += listView.getChildAt(i).getHeight();
}
Log.d(TAG, "实际高度:" + h);
Log.d(TAG, "list 高度:" + listView.getHeight());
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(listView.getWidth(), h,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
listView.draw(canvas);
// 测试输出
FileOutputStream out = null;
try {
out = new FileOutputStream(picpath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (null != out) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
}
return bitmap;
}
// 程序入口 截取ScrollView
public static void shootScrollView(ScrollView scrollView,String picpath) {
ScreenShot.savePic(getScrollViewBitmap(scrollView, picpath), picpath);
}
// 程序入口 截取ListView
public static void shootListView(ListView listView,String picpath) {
ScreenShot.savePic(getListViewBitmap(listView,picpath), picpath);
}
ListView截屏:ListView是会回收与重用Item,并且只会绘制在屏幕上显示的ItemView,下面的方法采用一个List来存储Item的视图,这种方案依然不够好,当Item足够多的时候,可能会发生oom。
- /**
- * ListView截图
- *
- * @param listView 要截图的ListView
- * @return Bitmap
- */
- public static Bitmap shotListView(ListView listView) {
- ListAdapter adapter = listView.getAdapter();
- int itemsCount = adapter.getCount();
- int allItemsHeight = 0;
- ArrayList<Bitmap> bmps = new ArrayList<>();
- for (int i = 0; i < itemsCount; i++) {
- View childView = adapter.getView(i, null, listView);
- childView.measure(View.MeasureSpec.makeMeasureSpec(listView.getWidth(),View.MeasureSpec.EXACTLY),
- View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED));
- childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
- childView.setDrawingCacheEnabled(true);
- childView.buildDrawingCache();
- bmps.add(childView.getDrawingCache());
- allItemsHeight += childView.getMeasuredHeight();
- }
- Bitmap bigBitmap = Bitmap.createBitmap(listView.getMeasuredWidth(), allItemsHeight, Bitmap.Config.ARGB_8888);
- Canvas bigCanvas = new Canvas(bigBitmap);
- Paint paint = new Paint();
- int iHeight = 0;
- for (int i = 0; i < bmps.size(); i++) {
- Bitmap bmp = bmps.get(i);
- bigCanvas.drawBitmap(bmp, 0, iHeight, paint);
- iHeight += bmp.getHeight();
- bmp.recycle();
- bmp = null;
- }
- return bigBitmap;
- }