哪些 Android 代码模拟了屏幕截图功能(同时降低音量和电源按钮)? [英] What Android code mimics the screenshot function (volume down and power button simultaneously)?

查看:61
本文介绍了哪些 Android 代码模拟了屏幕截图功能(同时降低音量和电源按钮)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种以编程方式截取 android 设备屏幕截图的方法.我已经查看了 stackoverflow 中的几个解决方案,这些解决方案涉及对正在运行的活动进行屏幕截图.我想通过同时按下音量调低和电源按钮来模仿在 Android 中默认触发的行为.请注意,我不想截取我自己的活动或跑步活动的屏幕截图.例如,我想用一个按钮启动一个对话框活动,当点击按钮时,我完成我的活动并启动一个后台任务,该任务将截取可见部分的屏幕截图,包括状态栏,比如按住音量调低和权力.我如何实现这一目标?

I am looking for a way to take screenshots of android device programmatically. I have looked at several solutions in stackoverflow that involve taking a screenshot of the running activity. I want to mimic the behavior that is by default triggered in Android by pressing volume down and power button simultaneously. Please note, I don't want to take screenshot of my own activity or running activity. I want to, for example, launch a dialog activity with a button, and when the button is clicked, I finish my activity and start a background task that will take a screenshot of the visible section including status-bar, like holding volume-down and power does. How do I achieve this?

推荐答案

如果您要截取另一个 Activity 的屏幕截图,则必须以某种方式获取对包含要截屏的屏幕部分的视图的引用,或者只是捕获整个屏幕.如果是整个屏幕,您可以使用

If you are taking a screenshot of another Activity you will somehow have to get a reference to the View that encompasses the section of the screen you want to screenshot OR just capture the entire screen. If it is the entire screen you can get that with

// get the top level view from the activity in this context
View theView = getWindow().getDecorView();

然后将该视图捕获为位图,您可以使用

Then to capture that View as a Bitmap you can use

// Take the Screen shot of a View
public static Bitmap takeScreenshot(final View theView) {
    theView.setDrawingCacheEnabled(true);
    return theView.getDrawingCache();
}    

// Release the drawing cache after you have captured the Bitmap
// from takeScreenshot()
public static void clearScreenshotCache(final View theView) {
    theView.setDrawingCacheEnabled(false);
}

从那里您可以通过调用 .compress 在位图上.

From there you can save the Bitmap into a JPEG or PNG by calling .compress on the Bitmap.

至于在后台线程上运行任务,有足够的文档介绍了几种方法,例如 这里.

As for running a task on a background thread there's enough documentation on several ways to do that like here.

希望能帮助您入门!

这篇关于哪些 Android 代码模拟了屏幕截图功能(同时降低音量和电源按钮)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆