服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - GuideView的封装实现app功能引导页

GuideView的封装实现app功能引导页

2022-10-07 16:38你的益达啊 Android

这篇文章主要为大家详细介绍了GuideView的封装实现app功能引导页,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了GuideView的封装实现app功能引导页的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package oschina.comxianbing100.yindao;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
 
import java.util.List;
 
/**
 * author : Majunbao
 * date : 2019/3/4 16:32
 * description :App第一次打开功能蒙版 引导
 */
public class GuideView extends RelativeLayout implements ViewTreeObserver.OnGlobalLayoutListener {
 private final String TAG = getClass().getSimpleName();
 private Context mContent;
 private List<View> mViews;
 private boolean first = true;
 /**
  * targetView前缀。SHOW_GUIDE_PREFIX + targetView.getId()作为保存在SP文件的key。
  */
 private static final String SHOW_GUIDE_PREFIX = "show_guide_on_view_";
 /**
  * GuideView 偏移量
  */
 private int offsetX, offsetY;
 /**
  * targetView 的外切圆半径
  */
 private int radius;
 /**
  * 需要显示提示信息的View
  */
 private View targetView;
 /**
  * 自定义View
  */
 private View customGuideView;
 /**
  * 透明圆形画笔
  */
 private Paint mCirclePaint;
 /**
  * 背景色画笔
  */
 private Paint mBackgroundPaint;
 /**
  * targetView是否已测量
  */
 private boolean isMeasured;
 /**
  * targetView圆心
  */
 private int[] center;
 /**
  * 绘图层叠模式
  */
 private PorterDuffXfermode porterDuffXfermode;
 /**
  * 绘制前景bitmap
  */
 private Bitmap bitmap;
 /**
  * 背景色和透明度,格式 #aarrggbb
  */
 private int backgroundColor;
 /**
  * Canvas,绘制bitmap
  */
 private Canvas temp;
 /**
  * 相对于targetView的位置.在target的那个方向
  */
 private Direction direction;
 
 /**
  * 形状
  */
 private MyShape myShape;
 /**
  * targetView左上角坐标
  */
 private int[] location;
 private boolean onClickExit;
 private OnClickCallback onclickListener;
 private RelativeLayout guideViewLayout;
 
 public void restoreState() {
  Log.v(TAG, "restoreState");
  offsetX = offsetY = 0;
  radius = 0;
  mCirclePaint = null;
  mBackgroundPaint = null;
  isMeasured = false;
  center = null;
  porterDuffXfermode = null;
  bitmap = null;
  needDraw = true;
  //  backgroundColor = Color.parseColor("#00000000");
  temp = null;
  //  direction = null;
 
 }
 
 public int[] getLocation() {
  return location;
 }
 
 public void setLocation(int[] location) {
  this.location = location;
 }
 
 public GuideView(Context context) {
  super(context);
  this.mContent = context;
  init();
 }
 
 public int getRadius() {
  return radius;
 }
 
 public void setRadius(int radius) {
  this.radius = radius;
 }
 
 public void setOffsetX(int offsetX) {
  this.offsetX = offsetX;
 }
 
 public void setOffsetY(int offsetY) {
  this.offsetY = offsetY;
 }
 
 public void setDirection(Direction direction) {
  this.direction = direction;
 }
 
 public void setShape(MyShape shape) {
  this.myShape = shape;
 }
 
 public void setCustomGuideView(View customGuideView) {
  this.customGuideView = customGuideView;
  if (!first) {
   restoreState();
  }
 }
 
 public void setBgColor(int background_color) {
  this.backgroundColor = background_color;
 }
 
 public View getTargetView() {
  return targetView;
 }
 
 public void setTargetView(View targetView) {
  this.targetView = targetView;
  //  restoreState();
  if (!first) {
   //   guideViewLayout.removeAllViews();
  }
 }
 
 private void init() {
 }
 
 public void showOnce() {
  if (targetView != null) {
   mContent.getSharedPreferences(TAG, Context.MODE_PRIVATE).edit().putBoolean(generateUniqId(targetView), true).commit();
  }
 }
 
 private boolean hasShown() {
  if (targetView == null)
   return true;
  return mContent.getSharedPreferences(TAG, Context.MODE_PRIVATE).getBoolean(generateUniqId(targetView), false);
 }
 
 private String generateUniqId(View v) {
  return SHOW_GUIDE_PREFIX + v.getId();
 }
 
 public int[] getCenter() {
  return center;
 }
 
 public void setCenter(int[] center) {
  this.center = center;
 }
 
 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
 public void hide() {
  Log.v(TAG, "hide");
  if (customGuideView != null) {
   targetView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
   this.removeAllViews();
   ((FrameLayout) ((Activity) mContent).getWindow().getDecorView()).removeView(this);
   restoreState();
  }
 }
 
 public void show() {
  Log.v(TAG, "show");
  if (hasShown())
   return;
 
  if (targetView != null) {
   targetView.getViewTreeObserver().addOnGlobalLayoutListener(this);
  }
 
  this.setBackgroundResource(R.color.transparent);
 
  ((FrameLayout) ((Activity) mContent).getWindow().getDecorView()).addView(this);
  first = false;
 }
 
 /**
  * 添加提示文字,位置在targetView的下边
  * 在屏幕窗口,添加蒙层,蒙层绘制总背景和透明圆形,圆形下边绘制说明文字
  */
 private void createGuideView() {
  Log.v(TAG, "createGuideView");
 
  // 添加到蒙层
  //  if (guideViewLayout == null) {
  //   guideViewLayout = new RelativeLayout(mContent);
  //  }
 
  // Tips布局参数
  LayoutParams guideViewParams;
  guideViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  guideViewParams.setMargins(0, center[1] + radius + 10, 0, 0);
 
  if (customGuideView != null) {
 
   //   LayoutParams guideViewParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
   if (direction != null) {
    int width = this.getWidth();
    int height = this.getHeight();
 
    int left = center[0] - radius;
    int right = center[0] + radius;
    int top = center[1] - radius;
    int bottom = center[1] + radius;
    switch (direction) {
     case TOP:
      this.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
      guideViewParams.setMargins(offsetX, offsetY - height + top, -offsetX, height - top - offsetY);
      break;
     case LEFT:
      this.setGravity(Gravity.RIGHT);
      guideViewParams.setMargins(offsetX - width + left, top + offsetY, width - left - offsetX, -top - offsetY);
      break;
     case BOTTOM:
      this.setGravity(Gravity.CENTER_HORIZONTAL);
      guideViewParams.setMargins(offsetX, bottom + offsetY, -offsetX, -bottom - offsetY);
      break;
     case RIGHT:
      guideViewParams.setMargins(right + offsetX, top + offsetY, -right - offsetX, -top - offsetY);
      break;
     case LEFT_TOP:
      this.setGravity(Gravity.RIGHT | Gravity.BOTTOM);
      guideViewParams.setMargins(offsetX - width + left, offsetY - height + top, width - left - offsetX, height - top - offsetY);
      break;
     case LEFT_BOTTOM:
      this.setGravity(Gravity.RIGHT);
      guideViewParams.setMargins(offsetX - width + left, bottom + offsetY, width - left - offsetX, -bottom - offsetY);
      break;
     case RIGHT_TOP:
      this.setGravity(Gravity.BOTTOM);
      guideViewParams.setMargins(right + offsetX, offsetY - height + top, -right - offsetX, height - top - offsetY);
      break;
     case RIGHT_BOTTOM:
      guideViewParams.setMargins(right + offsetX, bottom + offsetY, -right - offsetX, -top - offsetY);
      break;
    }
   } else {
    guideViewParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    guideViewParams.setMargins(offsetX, offsetY, -offsetX, -offsetY);
   }
 
   //   guideViewLayout.addView(customGuideView);
 
   this.addView(customGuideView, guideViewParams);
  }
 }
 
 /**
  * 获得targetView 的宽高,如果未测量,返回{-1, -1}
  *
  * @return
  */
 private int[] getTargetViewSize() {
  int[] location = {-1, -1};
  if (isMeasured) {
   location[0] = targetView.getWidth();
   location[1] = targetView.getHeight();
  }
  return location;
 }
 
 /**
  * 获得targetView 的半径
  *
  * @return
  */
 private int getTargetViewRadius() {
  if (isMeasured) {
   int[] size = getTargetViewSize();
   int x = size[0];
   int y = size[1];
 
   return (int) (Math.sqrt(x * x + y * y) / 2);
  }
  return -1;
 }
 
 boolean needDraw = true;
 
 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  Log.v(TAG, "onDraw");
 
  if (!isMeasured)
   return;
 
  if (targetView == null)
   return;
 
  //  if (!needDraw) return;
 
  drawBackground(canvas);
 
 }
 
 private void drawBackground(Canvas canvas) {
  Log.v(TAG, "drawBackground");
  needDraw = false;
  // 先绘制bitmap,再将bitmap绘制到屏幕
  bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
  temp = new Canvas(bitmap);
 
  // 背景画笔
  Paint bgPaint = new Paint();
  if (backgroundColor != 0)
   bgPaint.setColor(backgroundColor);
  else
   bgPaint.setColor(getResources().getColor(R.color.shadow));
 
  // 绘制屏幕背景
  temp.drawRect(0, 0, temp.getWidth(), temp.getHeight(), bgPaint);
 
  // targetView 的透明圆形画笔
  if (mCirclePaint == null)
   mCirclePaint = new Paint();
  porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT);// 或者CLEAR
  mCirclePaint.setXfermode(porterDuffXfermode);
  mCirclePaint.setAntiAlias(true);
 
  if (myShape != null) {
   RectF oval = new RectF();
   switch (myShape) {
    case CIRCULAR://圆形
     temp.drawCircle(center[0], center[1], radius, mCirclePaint);//绘制圆形
     break;
    case ELLIPSE://椭圆
     //RectF对象
     oval.left = center[0] - 150;        //左边
     oval.top = center[1] - 50;         //上边
     oval.right = center[0] + 150;        //右边
     oval.bottom = center[1] + 50;        //下边
     temp.drawOval(oval, mCirclePaint);     //绘制椭圆
     break;
    case RECTANGULAR://圆角矩形
     //RectF对象
     oval.left = center[0] - 150;        //左边
     oval.top = center[1] - 50;         //上边
     oval.right = center[0] + 150;        //右边
     oval.bottom = center[1] + 50;        //下边
     temp.drawRoundRect(oval, radius, radius, mCirclePaint);     //绘制圆角矩形
     break;
   }
  } else {
   temp.drawCircle(center[0], center[1], radius, mCirclePaint);//绘制圆形
  }
 
  // 绘制到屏幕
  canvas.drawBitmap(bitmap, 0, 0, bgPaint);
  bitmap.recycle();
 }
 
 public void setOnClickExit(boolean onClickExit) {
  this.onClickExit = onClickExit;
 }
 
 public void setOnclickListener(OnClickCallback onclickListener) {
  this.onclickListener = onclickListener;
 }
 
 private void setClickInfo() {
  final boolean exit = onClickExit;
  setOnClickListener(new OnClickListener() {
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   public void onClick(View v) {
    if (onclickListener != null) {
     onclickListener.onClickedGuideView();
    }
    if (exit) {
     hide();
    }
   }
  });
 }
 
 @Override
 public void onGlobalLayout() {
  if (isMeasured)
   return;
  if (targetView.getHeight() > 0 && targetView.getWidth() > 0) {
   isMeasured = true;
  }
 
  // 获取targetView的中心坐标
  if (center == null) {
   // 获取右上角坐标
   location = new int[2];
   targetView.getLocationInWindow(location);
   center = new int[2];
   // 获取中心坐标
   center[0] = location[0] + targetView.getWidth() / 2;
   center[1] = location[1] + targetView.getHeight() / 2;
  }
  // 获取targetView外切圆半径
  if (radius == 0) {
   radius = getTargetViewRadius();
  }
  // 添加GuideView
  createGuideView();
 }
 
 /**
  * 定义GuideView相对于targetView的方位,共八种。不设置则默认在targetView下方
  */
 enum Direction {
  LEFT, TOP, RIGHT, BOTTOM,
  LEFT_TOP, LEFT_BOTTOM,
  RIGHT_TOP, RIGHT_BOTTOM
 }
 
 /**
  * 定义目标控件的形状,共3种。圆形,椭圆,带圆角的矩形(可以设置圆角大小),不设置则默认是圆形
  */
 enum MyShape {
  CIRCULAR, ELLIPSE, RECTANGULAR
 }
 
 /**
  * GuideView点击Callback
  */
 interface OnClickCallback {
  void onClickedGuideView();
 }
 
 public static class Builder {
  static GuideView guiderView;
  static Builder instance = new Builder();
  Context mContext;
 
  private Builder() {
  }
 
  public Builder(Context ctx) {
   mContext = ctx;
  }
 
  public static Builder newInstance(Context ctx) {
   guiderView = new GuideView(ctx);
   return instance;
  }
 
  public Builder setTargetView(View target) {
   guiderView.setTargetView(target);
   return instance;
  }
 
  public Builder setBgColor(int color) {
   guiderView.setBgColor(color);
   return instance;
  }
 
  public Builder setDirction(Direction dir) {
   guiderView.setDirection(dir);
   return instance;
  }
 
  public Builder setShape(MyShape shape) {
   guiderView.setShape(shape);
   return instance;
  }
 
  public Builder setOffset(int x, int y) {
   guiderView.setOffsetX(x);
   guiderView.setOffsetY(y);
   return instance;
  }
 
  public Builder setRadius(int radius) {
   guiderView.setRadius(radius);
   return instance;
  }
 
  public Builder setCustomGuideView(View view) {
   guiderView.setCustomGuideView(view);
   return instance;
  }
 
  public Builder setCenter(int X, int Y) {
   guiderView.setCenter(new int[]{X, Y});
   return instance;
  }
 
  public Builder showOnce() {
   guiderView.showOnce();
   return instance;
  }
 
  public GuideView build() {
   guiderView.setClickInfo();
   return guiderView;
  }
 
  public Builder setOnclickExit(boolean onclickExit) {
   guiderView.setOnClickExit(onclickExit);
   return instance;
  }
 
  public Builder setOnclickListener(final OnClickCallback callback) {
   guiderView.setOnclickListener(callback);
   return instance;
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/nideyida1/article/details/88199286

延伸 · 阅读

精彩推荐