android之截屏(包括截取scrollview与listview的)
- 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之截屏(包括截取scrollview与listview的)的更多相关文章
- Android滚动截屏,ScrollView截屏
在做分享功能的时候,需要截取全屏内容,一屏展示不完的内容,一般我们会用到 ListView 或 ScrollView 一: 普通截屏的实现 获取当前Window 的 DrawingCache 的方式, ...
- Android长截屏-- ScrollView,ListView及RecyclerView截屏
http://blog.csdn.net/wbwjx/article/details/46674157 Android长截屏-- ScrollView,ListView及RecyclerV ...
- Android系统截屏的实现(附代码)
1.背景 写博客快两年了,写了100+的文章,最火的文章也是大家最关注的就是如何实现android系统截屏.其实我们google android_screen_ ...
- android后台截屏实现(2)--screencap源码修改
首先找到screencap类在Android源码中的位置,/442/frameworks/base/cmds/screencap/screencap.cpp 源码如下: /* * Copyright ...
- Android 长截屏原理
https://android-notes.github.io/2016/12/03/android%E9%95%BF%E6%88%AA%E5%B1%8F%E5%8E%9F%E7%90%86/ a ...
- Android手机截屏
刚开始打算做一个简单的截屏程序时,以为很轻松就能搞定. 在Activity上放一个按钮,点击完成截屏操作,并将数据以图片形式保存在手机中. 动手之前,自然是看书和网上各种查资料.结果发现了解的知识越多 ...
- Android代码截屏
本文来源:http://myhpu2008.iteye.com/blog/999779 这种方法应该只能对当前Activity本身进行截屏,因而你只能在你应用程序中参照该代码对其应用程序本身截屏. i ...
- android后台截屏实现(3)--编译screencap
修改好之后就要编译了,screencap的编译是要在源码环境中进行的. 将修改后的screencap.cpp文件替换源码中的原始文件,然后修改screencap的Android.mk文件,修改后的文件 ...
- android手机截屏、录屏
1. 手动截屏,通过其他第三方软件发送截图,或者从手机取出截图 2. 使用命令截图,将截图保存到手机,再拉取到电脑 #!/bin/sh #运行 sh screenshot name picName=$ ...
随机推荐
- mysql千万级表关联优化(2)
概述: 交代一下背景,这算是一次项目经验吧,属于公司一个已上线平台的功能,这算是离职人员挖下的坑,随着数据越来越多,原本的SQL查询变得越来越慢,用户体验特别差,因此SQL优化任务交到了我手上. 这个 ...
- awk调用shell命令的两种方法:system与print
from:http://www.oklinux.cn/html/developer/shell/20070626/31550.htmlawk中使用的shell命令,有2种方法: 一.使用所以syste ...
- 【51nod】1531 树上的博弈
题解 我们发现每次决策的时候,我们可以判断某个点的决策,至少小于等于几个点或者至少大于等于几个点 我们求最大值 dp[u][1 / 0] dp[u][1]表示u这个点先手,至少大于等于几个点 dp[u ...
- 为mongodb数据库增加用户名密码权限
加固mongodb建议:修改数据库默认端口,添加数据库访问权限: 启动数据库(裸奔):C:\mongodb\bin>mongod --dbpath C:\MongoDB\data(同时用--db ...
- [leetcode tree]98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- HTTP/FTP压力测试工具siege
HTTP/FTP压力测试工具siege 压力测试可以检测服务器的承载能力.针对HTTP和FTP服务,Kali Linux提供专项工具siege.该工具可以模拟多个用户同时访问同一个网站的多个网页, ...
- 关于socket知识整理
一个完整的计算机系统是由硬件.操作系统.应用软件三者组成,具备了这三个条件,一台计算机系统就可以玩单机游戏.如果你想上网(访问个黄色网站,发个黄色微博啥的),就需要遵守网络协议,即计算机之间交流的标准 ...
- python中的super( test, self).__init__()
python中的super( test, self).__init__() 对继承自父类的属性进行初始化 首先找到test的父类(比如是类A),然后把类test的对象self转换为类A的对象,然后“被 ...
- BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序
https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...
- ngx_lua应用最佳实践
引子: 以下文字,是UPYUN系统开发工程师timebug在SegmentFault D-Day南京站技术沙龙上所做分享的内容要义提炼,主题为UPYUN系统开发团队在进行业务逻辑由C模块到ngx_lu ...