安卓开发定时截屏

此处有两种方式:(都是定时截屏,不需要定时功能可以剔除service)

   1.app内截屏  https://download.csdn.net/download/hdhhd/89517797   

    2.截取当前任意手机显示屏幕   https://download.csdn.net/download/hdhhd/89517800

第一种方式:

            通过view.getDrawingCache()获取当前界面截屏并转成Bitmap,直接看代码:

   实现片段:     

public class Screenshot_View {

    /**
     * v.getDrawingCache()根据传入的View截取整个页面
     *
     * @param v
     * @return
     */
    public static Bitmap takeScreenShotOfView(View v) {
        v.setDrawingCacheEnabled(true);  //启用缓存
        v.buildDrawingCache(true);
        Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
        v.destroyDrawingCache();         // 销毁缓存
        v.setDrawingCacheEnabled(false); // 清除绘图缓存
        return bitmap;
    }
}

弊端:只能在APP内截取。

第二种方式:

        使用MediaProjectionManager抓取屏幕(截图):

实例化MediaProjectionManager,然后拿mediaProjectionManager.createScreenCaptureIntent()    创建屏幕捕获意图

    private void getMediaProjectionManager() {
        if (mediaProjectionManager == null) {
            mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
            Intent intent = new Intent(mediaProjectionManager.createScreenCaptureIntent());// 创建屏幕捕获意图
            startActivityForResult(intent, REQUEST_CODE);
        }
    }

执行回调:


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {   
        super.onActivityResult(requestCode, resultCode, data);
        if (REQUEST_CODE == requestCode && resultCode == RESULT_OK) {

            if (service == null) {
                service = new MyService();
                Intent intent = new Intent(this, service.getClass());
                intent.putExtra("windowManager_whd", windowManager_whd);
                intent.putExtra("resultCode", resultCode);
                intent.putExtra("data", data);
                startService(intent);
                mediaProjectionManager = null;
            }
        } else {
            showToastWarning("获取屏幕截图失败");
        }
    }
将回调中的状态 resultCode,data以及屏幕宽、高、密度传递给service进行持久化定时截取

需要注意的是Intent intent = new Intent(mediaProjectionManager.createScreenCaptureIntent());只需要执行一次拿到状态即可,否则每次执行都会让APP后台变前台(自动打开影藏在后台的APP)


    /**
     * 执行抓屏、并保存本地
     */
    @SuppressLint("WrongConstant")
    private synchronized void captureScreen() {
        try {
            if (resultCode == 0 || data == null || windowManager_whd == null) {
                Log.e(TAG, "缺少屏幕捕获所需的参数.");
                return;
            }

            if (mediaProjectionManager == null || mImageReader == null) {
                Log.e(TAG, "1233123123123.");
                mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
                projection = mediaProjectionManager.getMediaProjection(resultCode, data);
                //创建用于接收投影的容器
                mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat.RGBA_8888, 2);
                //通过MediaProjection创建创建虚拟显示器对象,创建后物理屏幕画面会不断地投影到虚拟显示器VirtualDisplay上,输出到虚拟现实器创建时设定的输出Surface上。
                VirtualDisplay virtualDisplay = projection.createVirtualDisplay("projection", mWidth, mHeight, mDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mImageReader.getSurface(), null, null);
            }

            Image image = null;
            while (image == null) {
                image = mImageReader.acquireLatestImage();
            }

            final Image.Plane[] planes = image.getPlanes();
            if (planes.length > 0) {
                final ByteBuffer buffer = planes[0].getBuffer();
                int pixelStride = planes[0].getPixelStride();
                int rowStride = planes[0].getRowStride();
                int rowPadding = rowStride - pixelStride * mWidth;
                Bitmap bitmap = Bitmap.createBitmap(mWidth + rowPadding / pixelStride, mHeight, Bitmap.Config.ARGB_8888);
                bitmap.copyPixelsFromBuffer(buffer);
                image_Base64(bitmap);
                saveBitmapToFile(bitmap);
                image.close();
            } else {
                projection = null;
            }

        } catch (Exception e) {
            Log.i(TAG, e.getMessage());
        } finally {
//            mImageReader.close();
        }
    }

优点:可以截取当前打开的任意屏幕

效果图:


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值