实现App带有控制悬浮窗的录屏小功能(上)

最近在开发的过程中,需要调用录屏功能。

大概功能就是:点击录屏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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值