代码拉取完成,页面将自动刷新
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'
}
/**
* 保存时未设置路径,则默认放置“手机存储-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
* <p>
* 当前 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);
}
/**
* 开始录制时未设置路径,则默认放置“手机存储-Android-data-包名-Cache-Movies”下
* 文件名默认为时间戳
* <p>
* 作者: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<? extends ScreenRecordService> 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();
}
string 资源
<string name="media_record_service_app_name">录屏服务</string>
<string name="sevice_foreground_notice_sub_title" >录屏服务运行中</string>
mipmap 图标资源
ic_screen_record_notice_logo
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
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
//继承服务后,必须传递服务,此处举例
ScreenRecordUtils.init(this, ScreenRecordService.class);
}
}
事件类
/**
* 事件的实现接口
*/
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 {
}
事件接收
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"));
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型