Android编程之截屏实现方法(包括scrollview与listview)

2019-12-10 19:52:57王冬梅
  • Bitmap bitmap = null;  // 获取listView实际高度 
  • for (int i = 0; i < scrollView.getChildCount(); i++) {  h += scrollView.getChildAt(i).getHeight(); 
  • scrollView.getChildAt(i).setBackgroundResource(R.drawable.bg3);  } 
  • 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("/sdcard/screen_test.png");  } catch (FileNotFoundException e) { 
  • e.printStackTrace();  } 
  • try {  if (null != out) { 
  • bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);  out.flush(); 
  • out.close();  } 
  • } catch (IOException e) {  // TODO: handle exception 
  • }  return bitmap; 
  • }  private static String TAG = "Listview and ScrollView item 截图:"; 
  • /**  * 截图listview 
  • * **/  public static Bitmap getbBitmap(ListView listView) { 
  • int h = 0;  Bitmap bitmap = null; 
  • // 获取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 {