Android 给主视图增加蒙层

思想:通过获取主视图获取主视图的父视图,在父视图上加蒙层,效果就是浮在上面的

public void showGuideView() {

    View view = getWindow().getDecorView().findViewById(R.id.activity_main);
    if (view == null) return;

    ViewParent viewParent = view.getParent();
    if (viewParent instanceof FrameLayout) {
        final FrameLayout frameParent = (FrameLayout) viewParent;//整个父布局

        final LinearLayout linearLayout = new LinearLayout(this);//新建一个LinearLayout
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setBackgroundColor(#88000000);//背景设置灰色透明
        linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              frameParent.removeView(linearLayout);
            }
        });

        Rect rect = new Rect();
        Point point = new Point();
        nearby.getGlobalVisibleRect(rect, point);//获得nearby这个控件的宽高以及XY坐标 nearby这个控件对应就是需要高亮显示的地方

        ImageView topGuideview = new ImageView(this);
        topGuideview.setLayoutParams(new ViewGroup.LayoutParams(rect.width(), rect.height()));           topGuideview.setBackgroundResource(R.drawable.iv_topguide);

  Rect rt = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rt);
        topGuideview.setY(point.y - rt.top);//rt.top是手机状态栏的高度
        ImageView bottomGuideview = new ImageView(this);
        bottomGuideview.setLayoutParams(new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
        bottomGuideview.setBackgroundResource(R.drawable.iv_bottomguide);
        bottomGuideview.setY(point.y + topGuideview.getHeight());

        linearLayout.addView(topGuideview);
        linearLayout.addView(bottomGuideview);
        frameParent.addView(linearLayout);
    }
}

效果图:


参考链接地址:https://blog.csdn.net/u013299273/article/details/54137847

https://blog.csdn.net/qq_31743309/article/details/77934273

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值