Skip to content

Commit 677dcf7

Browse files
author
lemon
committed
update:targetView is clickable when showing guideView
1 parent 3a69399 commit 677dcf7

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

.idea/caches/build_file_checksums.ser

0 Bytes
Binary file not shown.

app/src/main/java/easily/tech/guideview/MainActivity.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.os.Bundle
55
import android.view.View
66
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
77
import android.widget.RelativeLayout
8+
import android.widget.Toast
89
import easily.tech.guideview.lib.GuideViewBundle
910
import easily.tech.guideview.lib.GuideViewBundle.Direction.*
1011
import easily.tech.guideview.lib.GuideViewBundle.TransparentOutline.TYPE_OVAL
@@ -31,6 +32,10 @@ class MainActivity : AppCompatActivity() {
3132
val hintViewRight: View = View.inflate(this, R.layout.guideview_right, null)
3233
val hintViewBottom: View = View.inflate(this, R.layout.guideview_bottom, null)
3334

35+
tvContent.setOnClickListener {
36+
Toast.makeText(this, "target view is clicked", Toast.LENGTH_SHORT).show()
37+
}
38+
3439
hintViewLeft.tvLeftNext.setOnClickListener {
3540
guideViewFragment.onNext()
3641
}
@@ -54,6 +59,7 @@ class MainActivity : AppCompatActivity() {
5459
.setHintViewMargin(0, -160, 0, 0)
5560
.setTransparentSpace(space, space, space, space)
5661
.setOutlineType(TYPE_RECT)
62+
.setTargetViewClickable(true)
5763
.setHintViewParams(params)
5864
.setHintViewDirection(LEFT).build())
5965
.addGuidViewBundle(GuideViewBundle.Builder()

lib/src/main/java/easily/tech/guideview/lib/GuideView.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.os.Build;
1414
import android.util.DisplayMetrics;
1515
import android.view.Gravity;
16+
import android.view.MotionEvent;
1617
import android.view.View;
1718
import android.view.ViewGroup;
1819
import android.widget.FrameLayout;
@@ -37,6 +38,11 @@
3738
@SuppressLint("ViewConstructor")
3839
final class GuideView extends RelativeLayout {
3940

41+
interface TargetViewClickListener {
42+
void onGuideViewClicked();
43+
}
44+
45+
4046
private boolean hasAddHintView = false;
4147
public boolean isShowing = false;
4248
private int[] targetViewLocation = new int[2];
@@ -50,6 +56,7 @@ final class GuideView extends RelativeLayout {
5056
private Paint transparentPaint;
5157
private GuideViewBundle bundle;
5258
private FrameLayout decorView;
59+
private TargetViewClickListener targetViewClickListener;
5360

5461
GuideView(Context context, GuideViewBundle bundle) {
5562
super(context);
@@ -63,6 +70,24 @@ final class GuideView extends RelativeLayout {
6370
decorView = (FrameLayout) ((Activity) getContext()).getWindow().getDecorView();
6471
}
6572

73+
@Override
74+
public boolean dispatchTouchEvent(MotionEvent ev) {
75+
if (bundle.isTargetViewClickAble() && isTouchOnTargetView(ev)) {
76+
// dispatch touch event the the root activity
77+
// make the targetView receive the touch event
78+
if (getContext() instanceof Activity) {
79+
((Activity) getContext()).dispatchTouchEvent(ev);
80+
}
81+
if (ev.getAction() == MotionEvent.ACTION_UP) {
82+
if (targetViewClickListener != null) {
83+
targetViewClickListener.onGuideViewClicked();
84+
}
85+
}
86+
return true;
87+
}
88+
return super.dispatchTouchEvent(ev);
89+
}
90+
6691
@Override
6792
protected void onDraw(Canvas canvas) {
6893
if (bundle == null) {
@@ -105,6 +130,25 @@ private void drawBackGround(Canvas canvas) {
105130
canvas.drawBitmap(bitmap, 0, 0, backgroundPaint);
106131
}
107132

133+
private boolean isTouchOnTargetView(MotionEvent ev) {
134+
if (bundle == null || bundle.getTargetView() == null) {
135+
return false;
136+
}
137+
int yAxis = (int) ev.getRawY();
138+
int xAxis = (int) ev.getRawX();
139+
View targetView = bundle.getTargetView();
140+
int[] location = new int[2];
141+
targetView.getLocationOnScreen(location);
142+
int left = location[0];
143+
int top = location[1];
144+
int right = left + targetView.getMeasuredWidth();
145+
int bottom = top + targetView.getMeasuredHeight();
146+
if (yAxis >= top && yAxis <= bottom && xAxis >= left && xAxis <= right) {
147+
return true;
148+
}
149+
return false;
150+
}
151+
108152

109153
private void addHintView() {
110154
if (hasAddHintView || bundle.getHintView() == null) {
@@ -163,6 +207,10 @@ private boolean getTargetViewPosition() {
163207
return false;
164208
}
165209

210+
public void setTargetViewClickListener(TargetViewClickListener targetViewClickListener) {
211+
this.targetViewClickListener = targetViewClickListener;
212+
}
213+
166214
public void show() {
167215
if (bundle.getTargetView() == null) {
168216
return;

lib/src/main/java/easily/tech/guideview/lib/GuideViewBundle.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ public boolean isDismissOnClicked() {
115115
return config.isDismissOnClicked;
116116
}
117117

118+
public boolean isTargetViewClickAble() {
119+
return config.isTargetViewClickable;
120+
}
121+
122+
public boolean isDismissOnTouchInTargetView() {
123+
return config.isDismissOnClickTargetView;
124+
}
125+
118126
public static class Builder {
119127

120128
private static int MASK_LAYER_COLOR = 0xd9000000;
@@ -138,6 +146,13 @@ public static class Builder {
138146
private boolean hasTransparentLayer = true;
139147
// whether click the whole screen can dismissed the guideView.If false,you need to handle the click and dismiss event yourself
140148
private boolean isDismissOnClicked = true;
149+
150+
151+
private boolean isDismissOnClickTargetView=true;
152+
153+
private boolean isTargetViewClickable;
154+
155+
141156
private int hintViewDirection;
142157

143158
private int outlineType = TYPE_OVAL;
@@ -199,6 +214,17 @@ public Builder setMaskColor(int maskColor) {
199214
return this;
200215
}
201216

217+
public Builder setTargetViewClickable(boolean targetViewClickAble) {
218+
isTargetViewClickable = targetViewClickAble;
219+
return this;
220+
}
221+
222+
public Builder setDismissOnTouchInTargetView(boolean dismissOnTouchInTargetView) {
223+
isDismissOnClickTargetView = dismissOnTouchInTargetView;
224+
return this;
225+
}
226+
227+
202228
public GuideViewBundle build() {
203229
return new GuideViewBundle(this);
204230
}

lib/src/main/java/easily/tech/guideview/lib/GuideViewFragment.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ private void showGuideView() {
103103
}
104104
GuideView guideView = new GuideView(getContext(), currentBundle);
105105
wrapClickListener(guideView);
106+
guideView.setTargetViewClickListener(new GuideView.TargetViewClickListener() {
107+
@Override
108+
public void onGuideViewClicked() {
109+
if (currentBundle != null && currentBundle.isDismissOnTouchInTargetView()) {
110+
dismiss();
111+
}
112+
}
113+
});
106114
flContainer.addView(guideView);
107115
guideView.show();
108116
currentGuideView = guideView;

0 commit comments

Comments
 (0)