# ScreenLib **Repository Path**: mjsoftking/screen-lib ## Basic Information - **Project Name**: ScreenLib - **Description**: 安卓 基于MediaProjection进行屏幕录制、截屏,每次开始时都会唤起用户授权对话框 - **Primary Language**: Android - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 6 - **Created**: 2022-05-17 - **Last Updated**: 2022-05-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ScreenLib [![License](https://img.shields.io/badge/License%20-Apache%202-337ab7.svg)](https://www.apache.org/licenses/LICENSE-2.0) [![API](https://img.shields.io/badge/API-21%2B-brightgreen.svg?style=flat)](https://android-arsenal.com/api?level=21) [![](https://jitpack.io/v/com.gitee.osard/screen-lib.svg)](https://jitpack.io/#com.gitee.osard/screen-lib) ### 版本记录 #### V1.0.0版本(2022.5.17) 1. 由1.0.0.beta2发布,功能参考本文档 ### 一、介绍 1. 基于MediaProjection进行屏幕录制、截屏,每次开始时都会唤起用户授权对话框; 2. 保存view、activity为图片。 ### 二、工程引入工具包准备 **com.android.tools.build:gradle:4.2.2及以下版本,在工程的 build.gradle 文件添加** ``` allprojects { repositories { google() mavenCentral() //jitpack 仓库 maven { url 'https://jitpack.io' } } } ``` **com.android.tools.build:gradle:7.0.0及以上版本,在工程的 settings.gradle 文件添加** ``` dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() //jitpack 仓库 maven { url 'https://jitpack.io' } } } ``` **APP的build.gradle文件添加** ``` dependencies { ... implementation 'com.gitee.osard:screen-lib:+' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'org.greenrobot:eventbus:3.2.0' } ``` ### 三、使用 1. **ScreenCaptureUtils保存View、Activity为图片** ```java /** * 保存时未设置路径,则默认放置“手机存储-Android-data-包名-Cache-Pictures”下 * 文件名默认为时间戳 */ public class ScreenCaptureUtils { public static ScreenCaptureUtils createInstance(); /** * @param path 保存的路径,空路径无效,相对路径将无法得知存储位置 */ public ScreenCaptureUtils setImagePath(String path); /** * @param path 保存的路径 * @param videoName 保存文件的名字 */ public ScreenCaptureUtils setImagePath(String path, String videoName); /** * @param imageName 保存文件的名字 */ public ScreenCaptureUtils setImageName(String imageName); /** * 截图当前Activity *

* 当前 Activity 不可见时获取不到 View */ public ScreenCaptureUtils setView(@NonNull Activity activity); /** * 截图当前View */ public ScreenCaptureUtils setView(@NonNull View view); /** * 获取视图的截图 * * @return 返回bitmap,null:出现异常或 view=null */ public Bitmap screenCapture(); /** * 将视图的截图保存到指定的文件,保存为JPG * * @param quality 压缩质量 0-100,参考{Bitmap.CompressFormat#JPEG} * @return 返回保存的全路径,null:出现异常或 view=null */ public String saveBitmapToJpg(int quality); /** * 将视图的截图保存到指定的文件,保存为PNG * * @return 返回保存的全路径,null:出现异常或 view=null */ public String saveBitmapToPng(); /** * 将视图的截图保存到指定的文件 * * @param format 格式,参考{Bitmap.CompressFormat} * @param quality 压缩质量,参考{Bitmap.CompressFormat} * @return 返回保存的全路径,null:出现异常或 view=null */ public String saveBitmap(Bitmap.CompressFormat format, int quality); } ``` 2. **ScreenRecordUtils视频录制** - **开始录制和结束录制,使用前必须初始化** ```java /** * 开始录制时未设置路径,则默认放置“手机存储-Android-data-包名-Cache-Movies”下 * 文件名默认为时间戳 *

* 作者:MJSoftKing */ public class ScreenRecordUtils { /** * 使用自身service初始化视频录制 * * @param application app */ public static void init(Application application); /** * 使用继承ScreenRecordService重写前台通知栏的服务初始化 * 多次初始化时,以第一次为准 * * @param application APP * @param serviceName 继承ScreenRecordService的服务 */ public static void init(Application application, Class serviceName); /** * 使用竖屏或横屏开始录制。 * * @param videoPath 视频存放全路径,例如:“/storage/emulated/0/Android/data/包名/cache/Movies/” * @param videoName 视频名称,后缀为”.mp4“。 * @param direction 录制屏幕方向 */ public static boolean start(@Nullable String videoPath, @Nullable String videoName, @Nullable ScreenRecordDirectionEnum direction); /** * 使用竖屏或横屏开始录制。 * * @param videoPath 视频存放全路径,例如:“/storage/emulated/0/Android/data/包名/cache/Movies/” * @param videoName 视频名称,后缀为”.mp4“。 */ public static boolean start(@Nullable String videoPath, @Nullable String videoName); /** * 使用竖屏或横屏开始录制。 * * @param direction 录制屏幕方向 */ public static boolean start(@Nullable ScreenRecordDirectionEnum direction); /** * 使用调用时的屏幕方向开始录制 */ public static boolean start(); /** * 停止录制,成功录制时,触发{ScreenRecordSuccessEvent} 事件 */ public static boolean stop(); } ``` - **ScreenRecordService录制服务可覆盖资源** string 资源 ```html 录屏服务 录屏服务运行中 ``` mipmap 图标资源 ```text ic_screen_record_notice_logo ``` - **继承ScreenRecordService,重写前台通知栏与MediaFormat** ```java public class ScreenRecordService extends Service { /** * 通知ID,ID固定为“int最大值 - 1111”,与其他通知ID不可重复 */ protected final static int NOTICE_ID; /** * 录制屏幕宽度 */ protected int mWindowWidth; /** * 录制屏幕高度 */ protected int mWindowHeight; /** * 录制屏幕DPI */ protected int mScreenDensity; /** * 继承service后重写此Notification方法,自定义自己的前台通知栏 * 结束录制调用 {Service#stopSelf()}即可结束,不支持暂停和继续 */ protected Notification getForegroundNotice(); /** * 获取录制MediaFormat */ protected MediaFormat getMediaFormat(); } ``` 继承服务,初始化时必须传递继承后的服务class ```java public class App extends Application { @Override public void onCreate() { super.onCreate(); //继承服务后,必须传递服务,此处举例 ScreenRecordUtils.init(this, ScreenRecordService.class); } } ``` - 录制事件 事件类 ```java /** * 事件的实现接口 */ public interface BaseScreenRecordEvent extends Serializable { } /** * 录制出现错误时触发 */ public class ScreenRecordFailedEvent implements BaseScreenRecordEvent { /** * 录制发生错误时的异常message */ public String getMessage(); /** * 异常对象 */ public Throwable getE(); } /** * 录制开始时触发 */ public class ScreenRecordStartEvent implements BaseScreenRecordEvent { } /** * 录制结束时触发 */ public class ScreenRecordStopEvent implements BaseScreenRecordEvent { } /** * 录制成功时触发,返回视频全路径 */ public class ScreenRecordSuccessEvent implements BaseScreenRecordEvent { /** * 录制开始的时间 */ public Date getStartDate(); /** * 录制文件存放路径,后缀为“.mp4” */ public String getVideoPathName(); } /** * 用户取消录制触发(弹出确认框时点击取消) */ public class ScreenRecordUserCancelEvent implements BaseScreenRecordEvent { } ``` 事件接收 ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EventBus.getDefault().register(this); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void event(BaseScreenRecordEvent event) { Log.w(TAG, event.getClass().getName()); if (event instanceof ScreenRecordSuccessEvent) { ScreenRecordSuccessEvent srse = (ScreenRecordSuccessEvent) event; Log.w(TAG, srse.getVideoPathName()); new File(srse.getVideoPathName()).renameTo(new File( getExternalFilesDir(Environment.DIRECTORY_MOVIES).getAbsolutePath() + File.separator + System.currentTimeMillis() + ".mp4")); } } } ```