直接上代码:
/** * 截屏 * @param v 视图 * @param filePath 保存路径 */ private void getScreenHot(View v, String filePath) { try { Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(); canvas.setBitmap(bitmap); v.draw(canvas); try { FileOutputStream fos = new FileOutputStream(filePath); bitmap.compress(CompressFormat.PNG, 100, fos); } catch (FileNotFoundException e) { throw new InvalidParameterException(); } } catch (Exception e) { Log.i("截屏", "内存不足!"); e.printStackTrace(); } }
调用方法:
getScreenHot((View) getWindow().getDecorView(), "/sdcard/test1.png");
本文提供了一个简单的安卓应用程序屏幕截图的方法,通过代码展示了如何捕获当前屏幕的图像并将其保存为PNG格式到指定路径。该方法适用于需要记录应用状态或进行UI测试等场景。
4531

被折叠的 条评论
为什么被折叠?



