[Android][透明遮罩]

1.落笔缘由

之前有遇到要做透明遮罩的需求,现在来好好总结一下。实现遮罩的方式我这里总结了四种,分别是使用FrameLayout实现,PopupWindow实现,Dialog实现和WindowManager实现。

2.例子解析

1)FrameLayout实现

FrameLayout实现的遮罩能够控制遮罩大小,但是无法控制位置。而且能设置动画,但是FrameLayout实现的遮罩有个需要处理的问题,就是透过透明遮罩,是点击下一层的按钮是能触发该按钮的点击事件的。解决这个问题的方法是在透明遮罩的布局里设置android:clickable="true",或者在代码里直接设置setClickable(true);

/**
 * 遮罩
 * 1.是否可以控制遮罩大小和位置
 * 2.是否有动画 有
 *FrameLayout作为底布局,可以控制遮罩大小,但是无法控制遮罩的位置,例如你无法让遮罩居中,因为FrameLayout只能从左上角开始布置布局,而且,你可以点击遮罩下布局的按钮
 * FrameLayout两布局重叠,如何让下层不响应事件?可以在上层布局设置android:clickable="true",经过测试是有效的
 */
public class TestFramelayoutActivity extends Activity{

    private View translucenceLayut = null;
    private Button btnShow,btnHide,btnClose;
    private FrameLayout body = null;
    private Animation inAnimation ,outAnimation;
    private LinearLayout llbody = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.test_framelayout);
        body = (FrameLayout) findViewById(R.id.framebody);
        llbody = (LinearLayout) findViewById(R.id.body);
        inAnimation = AnimationUtils.loadAnimation(this,R.anim.top_in2);
        outAnimation = AnimationUtils.loadAnimation(this,R.anim.top_out2);
        translucenceLayut = LayoutInflater.from(this).inflate(R.layout.translucence_layer_framelayout_layout,null,false);
        body.addView(translucenceLayut,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
        translucenceLayut.setVisibility(View.INVISIBLE);
        btnHide = (Button) findViewById(R.id.btn_hide);
        btnHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                translucenceLayut.startAnimation(outAnimation);
                translucenceLayut.setVisibility(View.INVISIBLE);
            }
        });
        btnShow = (Button) findViewById(R.id.btn_show);
        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                translucenceLayut.startAnimation(inAnimation);
                translucenceLayut.setVisibility(View.VISIBLE);
            }
        });

        btnClose = (Button) translucenceLayut.findViewById(R.id.close);
        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                translucenceLayut.startAnimation(outAnimation);
                translucenceLayut.setVisibility(View.INVISIBLE);
            }
        });
    }
}

2)PopupWindow实现

PopupWindow实现的遮罩,可以控制大小和位置,可以设置动画效果,通过 popup.setAnimationStyle(R.style.popwin_anim_style)设置。但是也有一些需要注意的地方,这些在代码中指出。

/**
 * 可以控制大小和位置
 * 可以设置动画效果,通过 popup.setAnimationStyle(R.style.popwin_anim_style)设置
 */

public class TestPopupWindowActivity extends Activity {

    private Button btnShow = null;
    private PopupWindow popup = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_popupwindow);

        btnShow = (Button) findViewById(R.id.btnShow);
        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (popup!=null)
                {
                    popup.showAtLocation(TestPopupWindowActivity.this.findViewById(R.id.body), Gravity.CENTER, 0, 0);
                }
            }
        });

        popup = new PopupWindow(this);

        View popView = LayoutInflater.from(this).inflate(R.layout.popupwindow_translucence_layer_layout, null);
//        设置透明度setAlpha,在popupWindow里最好设置,因为popupWindow透明效果不是很好
//        popView.setAlpha(0.5f);
        popup.setContentView(popView);
        popup.setAnimationStyle(R.style.popwin_anim_style);
//        popup.setEnterTransition(inAnimation);
//        popup.setExitTransition(outAnimation);

        popup.setFocusable(true);
        //需要设置这个,否则popup的遮罩无法撑满整个屏幕(边上会有漏光)
        popup.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));// 设置背景图片,不能在布局中设置,要通过代码来设置
        DisplayMetrics metric = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(metric);

        int width = metric.widthPixels; // 屏幕宽度(像素)

        int height = metric.heightPixels; // 屏幕高度(像素)


//        popup.setWidth(Integer.parseInt(new DecimalFormat("0").format(width * 0.8)));
//
//        popup.setHeight(Integer.parseInt(new DecimalFormat("0").format(height * 0.8)));

//        popup.setWidth(width);
//        popup.setHeight(height);
        popup.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        popup.setHeight(WindowManager.LayoutParams.MATCH_PARENT);
        Button btnHide = (Button) popView.findViewById(R.id.btn_hide);
        btnHide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popup.dismiss();
            }
        });
    }
}

3)Dialog实现

Dialog实现的遮罩需要处理的问题比较多,不太建议作为遮罩使用。

/**
 * Created by LGY on 2017/4/30.
 * 1.使用dialog确实能控制遮罩大小和位置,dialog本来就自带透明背景,所以如果遮罩如果小于被遮布局,那么看起来效果不好
 * 而且要实现透明的dialog,需要设置dialog的透明度,但是这会使dialog里的控件也都拥有透明度,
 * 即使里面的控件设置了背景色和透明度也没有效果
 * 2.dialog不太适合做遮罩,要处理的细节太多
 */

public class OutlineDialog extends Dialog {


    private  Button btn_hide = null;
    private OnBtnHideClickListner mListner = null;
    public OutlineDialog(Context context) {
        super(context);
    }


    /**
     * @param context
     * @param theme
     */
    public OutlineDialog(Context context, int theme) {
        super(context, theme);
    }


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_translucence_layout);

        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = LayoutParams.MATCH_PARENT; // 宽度
        lp.height = LayoutParams.MATCH_PARENT; // 高度
        lp.alpha = 0.7f;
        dialogWindow.setAttributes(lp);
        dialogWindow.setWindowAnimations(R.style.popwin_anim_style);
        btn_hide = (Button) findViewById(R.id.btn_hide);
        btn_hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListner!=null )
                {
                    mListner.OnBtnHideClick();
                }
            }
        });
    }

    public void setOnBtnHideClickListner(OnBtnHideClickListner mListner)
    {
        this.mListner  = mListner;
    }

    public interface OnBtnHideClickListner
    {
        public void OnBtnHideClick();
    }
}

4)WindowManager实现

WindowManager实现的遮罩可以控制遮罩大小和位置,但是没有动画,至少我还没发现如何实现动画。

/*
* WindowManager可以控制遮罩大小和位置
* 但是,它不适合给遮罩添加转场动画
*
* */
public class TestWindowManagerActivity extends Activity {

    private  WindowManager windowManager = null;
    private LinearLayout translucenceLayout = null;
    private Button btnShow,btnClose;
    WindowManager.LayoutParams params = null;
    private Animation inAnimation ,outAnimation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_window_manager);
        initTranslucenceLayout();
        initWindowManager();


        inAnimation = AnimationUtils.loadAnimation(this,R.anim.top_in);
        outAnimation = AnimationUtils.loadAnimation(this,R.anim.top_out);

        btnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("lgy","123");
                if (windowManager!=null)
                {
//                    translucenceLayout.startAnimation(outAnimation);
                    windowManager.removeView(translucenceLayout);
                }
            }
        });
        btnShow = (Button) findViewById(R.id.btnShow);
        btnShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (windowManager!=null&¶ms!=null)
                {
                    windowManager.addView(translucenceLayout,params);
//                    translucenceLayout.setVisibility(View.INVISIBLE);
//                    translucenceLayout.startAnimation(inAnimation);
//                    translucenceLayout.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void initTranslucenceLayout()
    {
        translucenceLayout = new LinearLayout(this);
        if (translucenceLayout!=null){
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
            translucenceLayout.setLayoutParams(params);
            translucenceLayout.setBackgroundColor(Color.parseColor("#e0999999"));
            translucenceLayout.setOrientation(LinearLayout.VERTICAL);
            translucenceLayout.setGravity(Gravity.CENTER_VERTICAL);
            btnClose = new Button(this);
            if (btnClose!=null)
            {
                params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                btnClose.setLayoutParams(params);
                btnClose.setText("隐藏");
                translucenceLayout.addView(btnClose);
            }

        }
    }


    private void initWindowManager() {
        windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);

        // 设置LayoutParams参数
        params = new WindowManager.LayoutParams();
        // 设置显示的类型,TYPE_PHONE指的是来电话的时候会被覆盖,其他时候会在最前端,显示位置在stateBar下面,其他更多的值请查阅文档
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL  ;
        // 设置显示格式
        params.format = PixelFormat.RGBA_8888;
        // 设置对齐方式
        params.gravity = Gravity.LEFT | Gravity.TOP;
        // 设置宽高
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        //在进行 windowManager.addView操作前,务必要确定当前的activity已经创建运行,否则
        //会报错WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
//        windowManager.addView(translucenceLayout,params);
//        translucenceLayout.setVisibility(View.INVISIBLE);
    }
}

3.源码地址
http://download.csdn.net/detail/lgywsdy/9851645

你可能感兴趣的:([Android][透明遮罩])