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

 更新时间:2015年11月20日 14:45:25   作者:lee0oo0  
这篇文章主要介绍了Android编程之截屏实现方法,包括截取scrollview与listview屏幕的相关技巧,以及截屏图片的生成与保存技巧,需要的朋友可以参考下

本文实例讲述了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解析JSON数据的方法分析

    Android解析JSON数据的方法分析

    这篇文章主要介绍了Android解析JSON数据的方法,结合实例形式演示了Android解析json格式数据的原理与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • Android ListView自动生成列表条目的实例

    Android ListView自动生成列表条目的实例

    下面小编就为大家分享一篇Android ListView自动生成列表条目的实例,具有很好的 参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • android开发去除标题栏的方法

    android开发去除标题栏的方法

    这篇文章主要介绍了android开发去除标题栏的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • Android使用GPS获取用户地理位置并监听位置变化的方法

    Android使用GPS获取用户地理位置并监听位置变化的方法

    这篇文章主要介绍了Android使用GPS获取用户地理位置并监听位置变化的方法,实例分析了Android编程中GPS定位的实现与使用技巧,需要的朋友可以参考下
    2015-12-12
  • 浅谈Android IPC机制之Binder的工作机制

    浅谈Android IPC机制之Binder的工作机制

    IPC机制即为跨进程通信,是inter-Process Communication的缩写。是指两个进程之间进行通信。在说进程通信之前,我们的弄明白什么是线程,什么是进程。进程和线程是两个截然不同的概念。本文将介绍Android IPC机制之Binder的工作机制。
    2021-06-06
  • Kotlin协程操作之创建启动挂起恢复详解

    Kotlin协程操作之创建启动挂起恢复详解

    本文的定位是协程的创建、启动、挂起、恢复,也会示例一些简单的使用,这里不对suspend讲解,,也不对协程的高级用法做阐述(热数据通道Channel、冷数据流Flow...),本文主要讲协程稍微深入的全面知识
    2022-08-08
  • Android实现帧动画的两种方式

    Android实现帧动画的两种方式

    帧动画(Frame Animation)是一种在一定时间内按顺序播放一系列图像帧(每一帧都是一个单独的图像),从而产生连续运动或变化的动画效果,本文给大家介绍了Android实现帧动画的两种方式,需要的朋友可以参考下
    2024-02-02
  • Android使用属性动画如何自定义倒计时控件详解

    Android使用属性动画如何自定义倒计时控件详解

    自Android 3.0版本开始,系统给我们提供了一种全新的动画模式,属性动画(property animation),它的功能非常强大,下面这篇文章主要给大家介绍了关于Android使用属性动画如何自定义倒计时控件的相关资料,需要的朋友可以参考下
    2018-05-05
  • Android 6.0动态权限申请教程

    Android 6.0动态权限申请教程

    本文主要介绍了Android 6.0动态权限申请的教程,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • android短信拦截的实现代码

    android短信拦截的实现代码

    这篇文章介绍了android短信拦截的实现代码,有需要的朋友可以参考一下
    2013-09-09

最新评论