最近在开发的过程中,需要调用录屏功能。
大概功能就是:点击录屏button,返回主界面并显示半透明的自定义控件。
控件可以随意拖动,点击“开始”就开始录制并由timer来计时。
点击“停止”停止录制,点击“完成”停止录制并返回app。
本篇先介绍如何实现自定义控件的功能:
public class ScreenGathering extends ViewGroup {
protected Context mContext;
protected View mMainView;
protected VideoThread mVideoThread;
protected Timer mRecordTimer = new Timer();
protected WindowManager mWindowManager;
protected WindowManager.LayoutParams mControlParams;
protected Button mStopRecord;
protected Button mStartRecord;
protected Button mCompleteRecord;
protected int mScreenWidth;
protected int mScreenHeight;
protected boolean isRecording = false;
protected int nowWidth;
protected int nowHeight;
protected int initX;
protected int initY;
protected float touchX;
protected float touchY;
protected boolean isDragging = false;
protected final int MOVING_START_THRESHOLD = 50;
//获取当前GPU支持最大的Media播放分辨率
protected MediaCodecInfo.VideoCapabilities mVideoCapability = getSupportVideoCapability("Video");
//自定义ViewGroup都要定义onLayout函数
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}
public ScreenGathering(final Activity activity){
super(activity);
mContext = activity.getBaseContext();
//加载View视图
mMainView = View.inflate(activity, R.layout.vie_recording, null);
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
//设置LayoutParams参数
if(Build.VERSION.SDK_INT >= 26) {
mControlParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
/*窗口类型:OOS版本特有的窗口类型,OOS以上使用SYSTEM_ALERT_WINDOW权限必须使用TYPE_APPLICATION_OVERLAY
* 特性:1.应用的提示窗口始终显示在状态栏和输入法等关键系统窗口下面
* 2.系统可以移动使用TYPE_APPLICATION_OVERLAY窗口类型的窗口或者调整其大小,以改善屏幕显示效果
* 3.通过打开通知栏,用户可以访问设置来阻止应用显示使用TYPE_APPLICATION_OVERLAY窗口类型显示的提示窗口
*/
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
//设置窗口的模式:不许获得焦点、允许窗口扩展到屏幕之外。
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.Layout