【Android】多种方式实现截图(屏幕截图、控件图片、长图)

2024-06-18 21:12

本文主要是介绍【Android】多种方式实现截图(屏幕截图、控件图片、长图),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 1. View截图
  • 2. WebView截图
  • 3. 屏幕截图
  • 格式转换方法

Android 截图主要为四种:View 截图、WebView 截图、屏幕截图、系统截图和 adb 截图。后两种截图不常用,不详细展开。

1. View截图

可以截取到View不可见的部分,生成长图,状态栏和导航栏无法截到

fun screenshotView(view: ViewGroup):Bitmap?{var h = 0var bitmap:Bitmap?=nullfor(i in 0 until view.childCount){h += view.getChildAt(i).heightview.getChildAt(i).setBackgroundColor(Color.parseColor("#6CC287"))}bitmap = Bitmap.createBitmap(view.width, h, Bitmap.Config.RGB_565)val canvas = Canvas(bitmap)view.draw(canvas)//重新赋色for(i in 0 until view.childCount){view.getChildAt(i).setBackgroundDrawable(null)}return bitmap
}

2. WebView截图

// 1.capturePicture方法废弃
// 2.getScale方法废弃// 3.getDrawingCache方法
private static byte[] screenshotWebView() {Bitmap bitmap = webView.getDrawingCache();byte[] drawByte = getBitmapByte(bmp);return drawByte;
}// 4.draw方法
private static byte[] screenshotWebView() {// webView.setDrawingCacheEnabled(true); 设置缓存Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), webView.getHeight(), Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);webView.draw(canvas);webView.destroyDrawingCache();byte[] drawByte = getBitmapByte(bitmap);return drawByte;
}

可能会截取不到 cavans 元素,原因是开启了硬件加速(关闭硬件加速可能导致页面异常),可在 AndroidManifest.xml 中设置:

android:hardwareAccelerated="false"

截长图的话需要配置:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {WebView.enableSlowWholeDocumentDraw();
}
setContentView(R.layout.webview);

3. 屏幕截图

截取应用当前屏幕的图片:

	/*** 获取当前屏幕截图,包含状态栏** @param activity activity* @return Bitmap*/public static Bitmap captureWithStatusBar(Activity activity) {View view = activity.getWindow().getDecorView();view.setDrawingCacheEnabled(true);view.buildDrawingCache();Bitmap bmp = view.getDrawingCache();int width = getScreenWidth(activity);int height = getScreenHeight(activity);Bitmap ret = Bitmap.createBitmap(bmp, 0, 0, width, height);view.destroyDrawingCache();return ret;}/*** 获取当前屏幕截图,不包含状态栏** @param activity activity* @return Bitmap*/public static Bitmap captureWithoutStatusBar(Activity activity) {View view = activity.getWindow().getDecorView();view.setDrawingCacheEnabled(true);view.buildDrawingCache();Bitmap bmp = view.getDrawingCache();int statusBarHeight = getStatusBarHeight(activity);int width = getScreenWidth(activity);int height = getScreenHeight(activity);Bitmap ret = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);view.destroyDrawingCache();return ret;}/*** 得到屏幕的高** @param context* @return*/public static int getScreenHeight(Context context) {WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);int height = wm.getDefaultDisplay().getHeight();return height;}/*** 得到屏幕的宽** @param context* @return*/public static int getScreenWidth(Context context) {WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);int width = wm.getDefaultDisplay().getWidth();return width;}/*** 获取状态栏高度** @param context 上下文* @return 状态栏高度*/public static int getStatusBarHeight(Context context) {int result = 0;int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");if (resourceId > 0) {result = context.getResources().getDimensionPixelSize(resourceId);}return result;}

格式转换方法

// Bitmap 转 Base64
private static String getBitmapString(Bitmap bitmap) {String result = null;ByteArrayOutputStream out = null;try {if (bitmap != null) {out = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
​out.flush();out.close();byte[] bitmapBytes = out.toByteArray();result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);}} catch (IOException e) {e.printStackTrace();} finally {try {if (out != null) {out.flush();out.close();}} catch (IOException e) {e.printStackTrace();}}return result;
}// Bitmap 转 Byte
private static byte[] getBitmapByte(Bitmap bitmap){ByteArrayOutputStream out = new ByteArrayOutputStream();// 转换类型,压缩质量,字节流资源bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);try {out.flush();out.close();} catch (IOException e) {e.printStackTrace();}return out.toByteArray();
}// Drawable 转 Bitmap
public static Bitmap toBitmap(Drawable drawable) {if (drawable instanceof BitmapDrawable) {return ((BitmapDrawable) drawable).getBitmap();} else if (drawable instanceof ColorDrawable) {//colorBitmap bitmap = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);canvas.drawColor(((ColorDrawable) drawable).getColor());return bitmap;} else if (drawable instanceof NinePatchDrawable) {//.9.pngBitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());drawable.draw(canvas);return bitmap;}return null;
}

这篇关于【Android】多种方式实现截图(屏幕截图、控件图片、长图)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1073069

相关文章

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Jenkins分布式集群配置方式

《Jenkins分布式集群配置方式》:本文主要介绍Jenkins分布式集群配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装jenkins2.配置集群总结Jenkins是一个开源项目,它提供了一个容易使用的持续集成系统,并且提供了大量的plugin满