博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android代码截屏
阅读量:4043 次
发布时间:2019-05-24

本文共 1602 字,大约阅读时间需要 5 分钟。

这种方法应该只能对当前Activity本身进行截屏,因而你只能在你应用程序中参照该代码对其应用程序本身截屏。
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
public class ScreenShot {
    // 获取指定Activity的截屏,保存到png文件
    
private 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;
        Log.i("TAG", "" + 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();
        return b;
    
}
    // 保存到sdcard
  
  private 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();
        }
  
  }
    // 程序入口
   
 public static void shoot(Activity a) {
        ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");
    
}
}
注意:
shoot方法只能在view已经被加载后方可调用。

转载地址:http://rqrdi.baihongyu.com/

你可能感兴趣的文章
简单理解Socket及TCP/IP、Http、Socket的区别
查看>>
利用HTTP Cache来优化网站
查看>>
利用负载均衡优化和加速HTTP应用
查看>>
消息队列设计精要
查看>>
高可用性系统在大众点评的实践与经验
查看>>
美团酒店Node全栈开发实践
查看>>
分布式缓存负载均衡负载均衡的缓存处理:虚拟节点对一致性hash的改进
查看>>
分布式存储系统设计(1)—— 系统架构
查看>>
分布式存储系统设计(2)—— 数据分片
查看>>
架构师之路--视频业务介绍,离线服务架构和各种集群原理
查看>>
mysql、zookeeper、redis和elasticsearch主从同步机制
查看>>
MySQL数据库的高可用方案总结
查看>>
git 配置多个SSH-Key
查看>>
nodejs真的是单线程吗?
查看>>
JavaScript 模板引擎实现原理解析
查看>>
如何理解和熟练运用js中的call及apply?
查看>>
koa-源码分析
查看>>
co模块用法及分析
查看>>
深入理解Node.js垃圾回收与内存管理
查看>>
深入剖析Nodejs的异步IO
查看>>