android引導遮罩
阿新 • • 發佈:2018-12-18
第一步:類的複製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(); } }