admin
2021-07-06 abce02c7a61820f5d580f87364d542e817be429c
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
package com.app.hubert.guide.model;
 
import android.graphics.RectF;
import androidx.annotation.ColorInt;
import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import android.view.View;
import android.view.animation.Animation;
 
import com.app.hubert.guide.listener.OnHighlightDrewListener;
import com.app.hubert.guide.listener.OnLayoutInflatedListener;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by hubert
 * <p>
 * Created on 2017/11/16.
 */
 
public class GuidePage {
 
    private List<HighLight> highLights = new ArrayList<>();
    private boolean everywhereCancelable = true;
    private int backgroundColor;
 
    private int layoutResId;
    private int[] clickToDismissIds;
    private OnLayoutInflatedListener onLayoutInflatedListener;
    private OnHighlightDrewListener onHighlightDrewListener;
    private Animation enterAnimation, exitAnimation;
 
    public static GuidePage newInstance() {
        return new GuidePage();
    }
 
    public GuidePage addHighLight(View view) {
        return addHighLight(view, HighLight.Shape.RECTANGLE, 0, 0, null);
    }
 
    public GuidePage addHighLight(View view, RelativeGuide relativeGuide) {
        return addHighLight(view, HighLight.Shape.RECTANGLE, 0, 0, relativeGuide);
    }
 
    public GuidePage addHighLight(View view, HighLight.Shape shape) {
        return addHighLight(view, shape, 0, 0, null);
    }
 
    public GuidePage addHighLight(View view, HighLight.Shape shape, RelativeGuide relativeGuide) {
        return addHighLight(view, shape, 0, 0, relativeGuide);
    }
 
    public GuidePage addHighLight(View view, HighLight.Shape shape, int padding) {
        return addHighLight(view, shape, 0, padding, null);
    }
 
    public GuidePage addHighLight(View view, HighLight.Shape shape, int padding, RelativeGuide relativeGuide) {
        return addHighLight(view, shape, 0, padding, relativeGuide);
    }
 
    /**
     * 添加需要高亮的view
     *
     * @param view          需要高亮的view
     * @param shape         高亮形状{@link com.app.hubert.guide.model.HighLight.Shape}
     * @param round         圆角尺寸,单位dp,仅{@link com.app.hubert.guide.model.HighLight.Shape#ROUND_RECTANGLE}有效
     * @param padding       高亮相对view的padding,单位px
     * @param relativeGuide 相对于高亮的引导布局
     */
    public GuidePage addHighLight(View view, HighLight.Shape shape, int round, int padding,
                                  @Nullable RelativeGuide relativeGuide) {
        HighlightView highlight = new HighlightView(view, shape, round, padding);
        if (relativeGuide != null) {
            relativeGuide.highLight = highlight;
            highlight.setOptions(new HighlightOptions.Builder().setRelativeGuide(relativeGuide).build());
        }
        highLights.add(highlight);
        return this;
    }
 
    public GuidePage addHighLight(RectF rectF) {
        return addHighLight(rectF, HighLight.Shape.RECTANGLE, 0, null);
    }
 
    public GuidePage addHighLight(RectF rectF, RelativeGuide relativeGuide) {
        return addHighLight(rectF, HighLight.Shape.RECTANGLE, 0, relativeGuide);
    }
 
    public GuidePage addHighLight(RectF rectF, HighLight.Shape shape) {
        return addHighLight(rectF, shape, 0, null);
    }
 
    public GuidePage addHighLight(RectF rectF, HighLight.Shape shape, RelativeGuide relativeGuide) {
        return addHighLight(rectF, shape, 0, relativeGuide);
    }
 
    public GuidePage addHighLight(RectF rectF, HighLight.Shape shape, int round) {
        return addHighLight(rectF, shape, round, null);
    }
 
    /**
     * 添加高亮区域
     *
     * @param rectF         高亮区域,相对与anchor view(默认是decorView)
     * @param shape         高亮形状{@link com.app.hubert.guide.model.HighLight.Shape}
     * @param round         圆角尺寸,单位dp,仅{@link com.app.hubert.guide.model.HighLight.Shape#ROUND_RECTANGLE}有效
     * @param relativeGuide 相对于高亮的引导布局
     */
    public GuidePage addHighLight(RectF rectF, HighLight.Shape shape, int round, RelativeGuide relativeGuide) {
        HighlightRectF highlight = new HighlightRectF(rectF, shape, round);
        if (relativeGuide != null) {
            relativeGuide.highLight = highlight;
            highlight.setOptions(new HighlightOptions.Builder().setRelativeGuide(relativeGuide).build());
        }
        highLights.add(highlight);
        return this;
    }
 
    public GuidePage addHighLightWithOptions(View view, HighlightOptions options) {
        return addHighLightWithOptions(view, HighLight.Shape.RECTANGLE, 0, 0, options);
    }
 
    public GuidePage addHighLightWithOptions(View view, HighLight.Shape shape, HighlightOptions options) {
        return addHighLightWithOptions(view, shape, 0, 0, options);
    }
 
    public GuidePage addHighLightWithOptions(View view, HighLight.Shape shape, int round, int padding, HighlightOptions options) {
        HighlightView highlight = new HighlightView(view, shape, round, padding);
        if (options != null) {
            if (options.relativeGuide != null) {
                options.relativeGuide.highLight = highlight;
            }
        }
        highlight.setOptions(options);
        highLights.add(highlight);
        return this;
    }
 
    public GuidePage addHighLightWithOptions(RectF rectF, HighlightOptions options) {
        return addHighLightWithOptions(rectF, HighLight.Shape.RECTANGLE, 0, options);
    }
 
    public GuidePage addHighLightWithOptions(RectF rectF, HighLight.Shape shape, HighlightOptions options) {
        return addHighLightWithOptions(rectF, shape, 0, options);
    }
 
    public GuidePage addHighLightWithOptions(RectF rectF, HighLight.Shape shape, int round, HighlightOptions options) {
        HighlightRectF highlight = new HighlightRectF(rectF, shape, round);
        if (options != null) {
            if (options.relativeGuide != null) {
                options.relativeGuide.highLight = highlight;
            }
        }
        highlight.setOptions(options);
        highLights.add(highlight);
        return this;
    }
 
    /**
     * 添加引导层布局
     *
     * @param resId 布局id
     * @param id    布局中点击消失引导页的控件id
     */
    public GuidePage setLayoutRes(@LayoutRes int resId, int... id) {
        this.layoutResId = resId;
        clickToDismissIds = id;
        return this;
    }
 
    public GuidePage setEverywhereCancelable(boolean everywhereCancelable) {
        this.everywhereCancelable = everywhereCancelable;
        return this;
    }
 
    /**
     * 设置背景色
     */
    public GuidePage setBackgroundColor(@ColorInt int backgroundColor) {
        this.backgroundColor = backgroundColor;
        return this;
    }
 
    /**
     * 设置自定义layout填充监听,用于自定义layout初始化
     *
     * @param onLayoutInflatedListener listener
     */
    public GuidePage setOnLayoutInflatedListener(OnLayoutInflatedListener onLayoutInflatedListener) {
        this.onLayoutInflatedListener = onLayoutInflatedListener;
        return this;
    }
 
    /**
     * 设置进入动画
     */
    public GuidePage setEnterAnimation(Animation enterAnimation) {
        this.enterAnimation = enterAnimation;
        return this;
    }
 
    /**
     * 设置退出动画
     */
    public GuidePage setExitAnimation(Animation exitAnimation) {
        this.exitAnimation = exitAnimation;
        return this;
    }
 
    public boolean isEverywhereCancelable() {
        return everywhereCancelable;
    }
 
    public boolean isEmpty() {
        return layoutResId == 0 && highLights.size() == 0;
    }
 
    public List<HighLight> getHighLights() {
        return highLights;
    }
 
    public int getBackgroundColor() {
        return backgroundColor;
    }
 
    public int getLayoutResId() {
        return layoutResId;
    }
 
    public int[] getClickToDismissIds() {
        return clickToDismissIds;
    }
 
    public OnLayoutInflatedListener getOnLayoutInflatedListener() {
        return onLayoutInflatedListener;
    }
 
    public Animation getEnterAnimation() {
        return enterAnimation;
    }
 
    public Animation getExitAnimation() {
        return exitAnimation;
    }
 
    public List<RelativeGuide> getRelativeGuides() {
        List<RelativeGuide> relativeGuides = new ArrayList<>();
        for (HighLight highLight : highLights) {
            HighlightOptions options = highLight.getOptions();
            if (options != null) {
                if (options.relativeGuide != null) {
                    relativeGuides.add(options.relativeGuide);
                }
            }
        }
        return relativeGuides;
    }
}