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

所属分类: 软件编程 / Android 阅读数: 144
收藏 0 赞 0 分享

本文实例讲述了Android编程之截屏实现方法。分享给大家供大家参考,具体如下:

public class ScreenShot {
 // 获取指定Activity的截屏,保存到png文件
 public static Bitmap takeScreenShot(Activity activity) {
  // View是你需要截图的View
  View view = activity.getWindow().getDecorView();
  view.setDrawingCacheEnabled(true);
  view.buildDrawingCache();
  Bitmap b1 = view.getDrawingCache();
  // 获取状态栏高度
  Rect frame = new Rect();
  activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
  int statusBarHeight = frame.top;
  System.out.println(statusBarHeight);
  // 获取屏幕长和高
  int width = activity.getWindowManager().getDefaultDisplay().getWidth();
  int height = activity.getWindowManager().getDefaultDisplay()
    .getHeight();
  // 去掉标题栏
  // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
  Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
    - statusBarHeight);
  view.destroyDrawingCache();
  savePic(b, "/sdcard/screen_test.png");
  return b;
 }
 // 保存到sdcard
 public static void savePic(Bitmap b, String strFileName) {
  FileOutputStream fos = null;
  try {
   fos = new FileOutputStream(strFileName);
   if (null != fos) {
    b.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.flush();
    fos.close();
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 /**
  * 把View对象转换成bitmap
  * */
 public static Bitmap convertViewToBitmap(View view) {
  view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
  view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
  view.buildDrawingCache();
  Bitmap bitmap = view.getDrawingCache();
  if (bitmap != null) {
   System.out.println("这不是nullde1");
   Log.d("nullde1", "nullde1");
  } else {
   System.out.println("这nullnulllnulnlul");
  }
  return bitmap;
 }
 // 程序入口1
 public static void shoot(Activity a) {
  ScreenShot.savePic(ScreenShot.takeScreenShot(a), "/sdcard/screen_test.png");
 }
 // 程序入口2
 public static void shootView(View view) {
  ScreenShot.savePic(ScreenShot.convertViewToBitmap(view),
    "sdcard/xx.png");
 }
 public static Bitmap getViewBitmap(View v) {
  v.clearFocus();
  v.setPressed(false);
  boolean willNotCache = v.willNotCacheDrawing();
  v.setWillNotCacheDrawing(false);
  // Reset the drawing cache background color to fully transparent
  // for the duration of this operation
  int color = v.getDrawingCacheBackgroundColor();
  v.setDrawingCacheBackgroundColor(0);
  if (color != 0) {
   v.destroyDrawingCache();
  }
  v.buildDrawingCache();
  Bitmap cacheBitmap = v.getDrawingCache();
  if (cacheBitmap == null) {
   Log.e("TTTTTTTTActivity", "failed getViewBitmap(" + v + ")",
     new RuntimeException());
   return null;
  }
  Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
  // Restore the view
  v.destroyDrawingCache();
  v.setWillNotCacheDrawing(willNotCache);
  v.setDrawingCacheBackgroundColor(color);
  return bitmap;
 }
 /**
  * 截取scrollview的屏幕
  * **/
 public static Bitmap getBitmapByView(ScrollView scrollView) {
  int h = 0;
  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 {
   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;
 }
}

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android实现信号强度监听的方法

这篇文章主要介绍了Android实现信号强度监听的方法,是Android手机中很常见的一个实用功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android实现Activity界面切换添加动画特效的方法

这篇文章主要介绍了Android实现Activity界面切换添加动画特效的方法,非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中Dialog去黑边的方法

这篇文章主要介绍了Android中Dialog去黑边的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt for Android开发实例教程

这篇文章主要介绍了Qt for Android开发的方法,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期操作实例

这篇文章主要介绍了Android开发之时间日期操作,是Android程序开发中常见的一个功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期组件用法实例

这篇文章主要介绍了Android开发之时间日期组件用法,主要介绍了TimePicker和DatePicker组件,对于Android程序开发有不错的借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之获取网络链接状态

这篇文章主要介绍了Android获取网络链接状态的方法,主要是通过ConnectivityManager类来完成的,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之广播机制浅析

这篇文章主要介绍了Android开发之广播机制浅析,主要包括了发布、接收及配置广播的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之登录验证实例教程

这篇文章主要介绍了Android开发之登录验证实现方法,包括发送数据、服务器端验证、配置文件等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之注册登录方法示例

这篇文章主要介绍了Android开发的注册登录方法,是针对Android程序设计中版本兼容性的进一步完善,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多