做完一个app之后发现给视图增加全屏的蒙层是很常用的一个功能,之前是在BaseActiviy中写死一个全屏的view,在子类中控制其显示,这样会大大增加基类中的代码量,于是查询在当前类中通过代码来增加蒙层的方法,如下:
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
https://blog.csdn.net/Dreamj1991/article/details/80267464
网友评论