第一步:类的复制HighLightLayoutFang
public class HighLightLayoutFang extends RelativeLayout {
public HighLightLayoutFang(Context context) {
super(context);
init();
}
public HighLightLayoutFang(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public HighLightLayoutFang(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void addRes(int res) {
View view = View.inflate(getContext(), res, null);
addView(view);
}
public void init() {
setBackgroundColor(Color.TRANSPARENT);
}
ArrayList<Path> paths = new ArrayList<>();
int size = 0;
public void setView(View... views) {
size = views.length;
for (final View view : views) {
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Path path = new Path();
int[] position = new int[2];
view.getLocationInWindow(position);
float x = position[0] + view.getWidth() / 2f;
float y = position[1] + view.getHeight() / 2f;
float r;
if (view.getWidth() > view.getHeight()) {
r = view.getWidth() / 2f;
} else {
r = view.getHeight() / 2f;
}
path.addCircle(x, y, r, Path.Direction.CW);
//根据需求更改:圆形、正方形、圆角正方形、都是根据x轴Y轴来计算的
//这个是矩形的
// path.addRect(position[0], position[1], position[0]+ view.getWidth(),position[1]+view.getHeight(), Path.Direction.CW);
//下面是圆角正方形
path.addRoundRect(new RectF(position[0], position[1], position[0]+ view.getWidth(),position[1]+view.getHeight()), radiusArray, Path.Direction.CW);
paths.add(path);
if (paths.size() >= size) {
invalidate();
}
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
}
Paint paint = new Paint();
@SuppressLint("NewApi")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
int layerId = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null, Canvas.ALL_SAVE_FLAG);
paint.setColor(Color.parseColor("#b2000000"));
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
paint.setColor(Color.parseColor("#ff000000"));
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
for (Path path : paths) {
canvas.drawPath(path, paint);
}
//canvas.drawRect(new Rect(0,0,100,100), paint);
paint.setXfermode(null);
canvas.restoreToCount(layerId);
canvas.restore();
}
}
第二步://判断是否第一次登陆 如果是第一次登陆就有功能引导页面否则没有create_new() 写在进页面的地方
public void create_new() {
addImg();
boolean theFirst = SPUtils.getSp(this).getBoolean(TAG, true);
if (theFirst) {
SPUtils.getEditor(this).putBoolean(TAG, false).commit();
} else {
}
}
第三步://新功能引导页 的做法 添加需要遮罩的Id 和显示的View
public void addImg() {
boolean theFirst = SPUtils.getSp(getActivity()).getBoolean(SPTAG, true);
if (theFirst) {
//第一次启动
HighLightLayoutFang view = new HighLightLayoutFang(getActivity());
view.addRes(R.layout.view_guide_simple);
view.setView(rootView.findViewById(R.id.sjgz), rootView.findViewById(R.id.wdjz), rootView.findViewById(R.id.kf), rootView.findViewById(R.id.jsq));
((ViewGroup) getActivity().getWindow().getDecorView()).addView(view);
view.invalidate();
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((ViewGroup) getActivity().getWindow().getDecorView()).removeView(view);
}
});
SPUtils.getEditor(getActivity()).putBoolean(SPTAG, false).commit();
}
}