基于dialog封装防截屏的实现

除了可以封装一个非dialog实现外也可以封装一个不和触摸也看不见的控件,调用显示方法实现一个activity多个webview,fragmet中不同的防截屏设置。



import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;


public class PageScreenshot {

    private Context mContext;
    private Dialog mScreenshotDialog;

    public PageScreenshot(Context context) {
        this.mContext = context;
    }

    /**
     * 弹出为了防截屏创建的对话框
     */
    public void disableCapture() {
        if (mScreenshotDialog != null && mScreenshotDialog.isShowing()) {
            return;
        }
        //builder.setMessage("");

        mScreenshotDialog = new Dialog(mContext,0);
        mScreenshotDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        Window window = mScreenshotDialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.width=0;
        params.height=0;

        //dialog位置
        window.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
        //透明度
//        params.alpha = 1.0f;
        mScreenshotDialog.getWindow().setBackgroundDrawable(new ColorDrawable());

        window.setAttributes(params);
        mScreenshotDialog.getWindow().setDimAmount(0);//设置昏暗度为0
        mScreenshotDialog.getWindow().setWindowAnimations(0);
        mScreenshotDialog.getWindow().setTitle("");
        WindowManager.LayoutParams attributes = mScreenshotDialog.getWindow().getAttributes();
        attributes.alpha=0.01f;
        mScreenshotDialog.getWindow().setAttributes(attributes);

        mScreenshotDialog.setCancelable(false);
        try{

        mScreenshotDialog. requestWindowFeature(Window.FEATURE_NO_TITLE);
        }catch (Exception e){


        }
        mScreenshotDialog.setContentView(new TextView(mContext),new FrameLayout.LayoutParams(1,1,Gravity.TOP));

        //设置其不获取焦点
        mScreenshotDialog.show();
        mScreenshotDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        onShow(mScreenshotDialog);
    }

    public void enableCapture() {
                if (mScreenshotDialog != null && mScreenshotDialog.isShowing()) {
                    mScreenshotDialog.dismiss();
                    onDismiss(mScreenshotDialog);
                    mScreenshotDialog = null;
                }
    }



    public static void onDismiss(Dialog dialog) {
  //添加禁止截屏代码

    }

    public static void onShow(Dialog dialog) {
    
    }
}

你可能感兴趣的:(基于dialog封装防截屏的实现)