android 截屏的三种方法
16lz
2021-01-26
1. 采用API做法,这种做法的好处是不需要特殊的权限处理。不过在一种情况下不能用,就是在打开camera的情况下使用这种方法:
private static Bitmap takeScreenShot(Activity activity) {// View是你需要截图的ViewView view = activity.getWindow().getDecorView();view.setDrawingCacheEnabled(true);view.buildDrawingCache();Bitmap b1 = null;try {b1 = view.getDrawingCache();} catch (OutOfMemoryError e) {// TODO: handle exception}// 获取状态栏高度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();return b;}
2.采用Surface的隐藏方法,不过这种方法需要的源码中重新build.
void takeScreenshot() {// We need to orient the screenshot correctly (and the Surface api seems to take screenshots// only in the natural orientation of the device :!)mDisplay.getRealMetrics(mDisplayMetrics);float[] dims = { mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels };float degrees = getDegreesForRotation(mDisplay.getRotation());boolean requiresRotation = (degrees > 0);if (requiresRotation) {// Get the dimensions of the device in its native orientationmDisplayMatrix.reset();mDisplayMatrix.preRotate(-degrees);mDisplayMatrix.mapPoints(dims);dims[0] = Math.abs(dims[0]);dims[1] = Math.abs(dims[1]);}// Take the screenshotmScreenBitmap = Surface.screenshot((int) dims[0], (int) dims[1]);// mScreenBitmap = android.view.SurfaceControl.screenshot((int) dims[0], (int) dims[1]);if (mScreenBitmap == null) {// notifyScreenshotError(mContext, mNotificationManager);// finisher.run();Log.i("W", "mScreenBitmap null");return;}if (requiresRotation) {// Rotate the screenshot to the current orientationBitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, Bitmap.Config.ARGB_8888);Canvas c = new Canvas(ss);c.translate(ss.getWidth() / 2, ss.getHeight() / 2);c.rotate(degrees);c.translate(-dims[0] / 2, -dims[1] / 2);c.drawBitmap(mScreenBitmap, 0, 0, null);c.setBitmap(null);// Recycle the previous bitmapmScreenBitmap.recycle();mScreenBitmap = ss;}// OptimizationsmScreenBitmap.setHasAlpha(false);mScreenBitmap.prepareToDraw();SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss_SS");String name = sdf.format(new Date()) + ".jpg";savePic(mScreenBitmap, name);// Start the post-screenshot animation// startAnimation(finisher, mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, statusBarVisible, navBarVisible);}
3.采用命令的方式,这种方式的4.0之后都可以(包括4.0),如果要在程序中实现,需要系统权限。
screencap /mnt/sdcard/screen.png
4.0之后也提供screenrecord录制屏幕的方法:
screenrecord --h Usage: screenrecord [options] <filename>Records the device's display to a .mp4 file.Options:--size WIDTHxHEIGHT Set the video size, e.g. "1280x720". Default is the device's main display resolution (if supported), 1280x720 if not. For best results, use a size supported by the AVC encoder.--bit-rate RATE Set the video bit rate, in megabits per second. Default 4Mbps.--time-limit TIME Set the maximum recording time, in seconds. Default / maximum is 180.--rotate Rotate the output 90 degrees.--verbose Display interesting information on stdout.--help Show this message.Recording continues until Ctrl-C is hit or the time limit is reached.
更多相关文章
- Android(安卓)存储路径选择方法
- Android(安卓)-- 超全的 File,Bitmap,Drawable,Uri, FilePath ,byte
- android红米等关于读取本地文件夹图片获取路径的问题的解决
- android中打开本地doc、pdf、ppt、text等文件功能的实现
- Android(安卓)adb方式获取手机总内存和可用内存信息
- Android(安卓)M动态申请获取权限android.permission.READ_PHONE_
- Android获取设备已安装的应用
- Android-常用代码-36片段
- Android获取CPU信息(CPU名字和主频)