Android实现截屏与截长图功能的方法

  介绍

这篇文章给大家分享的是有关Android实现截屏与截长图功能的方法的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

Android实现截屏与截长图功能展示的具体代码,具体内容如下

演示在GitHub的地址:ScreenShoot

演示在CSDN上的下载地址:Android实现截屏与截长图功能

在Android开发中,有时候会遇到需要截屏分享到朋友圈或者QQ,截屏有截取当前屏幕,也有需要截取不仅一个屏幕,可能会很长。

截取当前屏幕并保存到内存卡的方法:

//,获取指定活动的截屏,保存到png文件   ,public  static  Bitmap  takeScreenShot (Activity 活动),{//才能,视图是你需要截图的视图   View 才能;View =, activity.getWindow () .getDecorView ();   view.setDrawingCacheEnabled才能(真正的);   view.buildDrawingCache才能();   Bitmap 才能;b1 =, view.getDrawingCache ();//,才能获取状态栏高度   Rect 才能;frame =, new 矩形();   .getDecorView activity.getWindow才能()().getWindowVisibleDisplayFrame(框架);   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 (0, b1,还以为,statusBarHeight,宽度,高度   ,,,的背后,statusBarHeight);   view.destroyDrawingCache才能();   return 才能;b;   ,}      ,//保存到sdcard   ,public  static  void  savePic (Bitmap  b, String  strFileName), {   FileOutputStream 才能;fos =,空;   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 ();   ,,}   ,}//,程序入口,截取当前屏幕   ,public  static  void  shootLoacleView (Activity  String  picpath), {   ScreenShot.savePic才能(ScreenShot.takeScreenShot (a),, picpath);   以前,}

当视图超过一个屏幕的时候,可能是视图,也可能是滚动视图,这时候,其实截图就是对列表视图或者Scrollview进行截图:

,/* *   *,才能截取滚动视图的屏幕   ,*,* */,public  static  Bitmap  getScrollViewBitmap (ScrollView 滚动视图,String  picpath), {   int 才能;h =, 0;   Bitmap 才能,位图;//,才能获取listView实际高度   for 才能;(int 小姐:=,0;,小姐:& lt;, scrollView.getChildCount();,我+ +),{   ,,h  +=, scrollView.getChildAt (i) .getHeight ();   ,,}   Log.d才能(标签,“实际高度:“,+,h);   Log.d才能(标签,,,,高度:“,+,scrollView.getHeight ());//,才能创建对应大小的位图   时间=bitmap 才能;Bitmap.createBitmap (scrollView.getWidth (),,,   ,,,Bitmap.Config.ARGB_8888);   final 才能Canvas  Canvas =, new 画布(位图);   scrollView.draw才能(画布);//,才能测试输出   FileOutputStream 才能;out =,空;   try {才能   ,,out =, new  FileOutputStream (picpath);   ,,},catch  (FileNotFoundException  e), {   ,,e.printStackTrace ();   ,,}   try {才能   ,,if  (null  !=,), {   ,,,bitmap.compress (Bitmap.CompressFormat.PNG,, 100,,);   ,,,out.flush ();   ,,,out.close ();   ,,}   ,,},catch  (IOException  e), {   ,,}   return 才能,位图;   ,}      ,private  static  String  TAG =,“Listview 以及ScrollView  item 截图:“;/* *   *,才能截图列表视图   ,*,* */,public  static  Bitmap  getListViewBitmap (ListView  listView, String  picpath), {   int 才能;h =, 0;   Bitmap 才能,位图;//,才能获取listView实际高度   for 才能;(int 小姐:=,0;,小姐:& lt;, listView.getChildCount();,我+ +),{   ,,h  +=, listView.getChildAt (i) .getHeight ();   ,,}   Log.d才能(标签,“实际高度:“,+,h);   Log.d才能(标签,“list 高度:“,+,listView.getHeight ());//,才能创建对应大小的位图   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android实现截屏与截长图功能的方法