Skip to content

Commit b6f3e7b

Browse files
author
fengpeng
committed
Marker add view
1 parent aef43a5 commit b6f3e7b

File tree

5 files changed

+72
-27
lines changed

5 files changed

+72
-27
lines changed

app/src/main/java/com/pizzk/overlay/app/MainActivity.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,22 @@ class MainActivity : AppCompatActivity() {
5959
.marker(R.layout.tv2_marker)
6060
//
6161
.anchor(Anchor.rect(R.id.vRecycler, radius = 10, outset = 5))
62+
.marker(R.id.vRecycler, Marker.iv(baseContext, R.mipmap.ic_launcher))
6263
.build()
6364
overlay.marker(R.layout.tv1_marker, m1Layout)
6465
overlay.marker(R.layout.tv2_marker, m2Layout)
66+
overlay.marker(R.id.vRecycler, object : Marker.MarkerLayout() {
67+
override fun onLayout(cs: ConstraintSet, marker: View, anchor: View) {
68+
super.onLayout(cs, marker, anchor)
69+
connect(ConstraintSet.START)
70+
connect(ConstraintSet.END)
71+
connect(ConstraintSet.BOTTOM, ConstraintSet.TOP, 10)
72+
}
73+
})
6574
//特殊情况:从RecyclerView中获取定位子元素,不能使用使用常规的findViewById
6675
overlay.anchor(R.id.vRecycler, object : Anchor.Find {
67-
override fun onFind(parent: ViewGroup, id: Int): View? {
68-
val v: ViewGroup? = findViewById(id)
76+
override fun onFind(parent: ViewGroup, e: Anchor): View? {
77+
val v: ViewGroup? = findViewById(e.id)
6978
return v?.getChildAt(2)
7079
}
7180
})

lib/src/main/java/com/pizzk/overlay/OverlayLayout.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package com.pizzk.overlay
33
import android.content.Context
44
import android.graphics.*
55
import android.util.AttributeSet
6-
import android.view.LayoutInflater
76
import android.view.View
87
import android.view.ViewGroup
98
import androidx.annotation.ColorRes
10-
import androidx.annotation.IdRes
119
import androidx.constraintlayout.widget.ConstraintLayout
1210
import androidx.constraintlayout.widget.ConstraintSet
1311
import androidx.core.content.ContextCompat
@@ -71,9 +69,8 @@ class OverlayLayout : ConstraintLayout {
7169
getLocationOnScreen(vXY)
7270
val vAnchorXY = IntArray(2)
7371
//
74-
val lf = LayoutInflater.from(viewGroup.context)
7572
overlay.anchors.forEach { e: Anchor ->
76-
val vAnchor: View = e.find.onFind(viewGroup, e.id) ?: return@forEach
73+
val vAnchor: View = e.find.onFind(viewGroup, e) ?: return@forEach
7774
//计算宽度及位置
7875
vAnchor.getLocationOnScreen(vAnchorXY)
7976
rect.left = vAnchorXY[0] - vXY[0] - 0f
@@ -88,11 +85,11 @@ class OverlayLayout : ConstraintLayout {
8885
val anchor = onFakeAnchor(e.id, rect.toRect())
8986
//标记层布局
9087
val markers = overlay.markers.filter { it.anchor == e.id }
91-
markers.forEach { onLayoutMarker(lf, it, anchor) }
88+
markers.forEach { onLayoutMarker(viewGroup.context, it, anchor) }
9289
}
9390
}
9491

95-
private fun onFakeAnchor(@IdRes id: Int, rc: Rect): View {
92+
private fun onFakeAnchor(id: Int, rc: Rect): View {
9693
val v: View? = getViewById(id)
9794
if (null != v) return v
9895
val view = View(context)
@@ -106,9 +103,9 @@ class OverlayLayout : ConstraintLayout {
106103
return view
107104
}
108105

109-
private fun onLayoutMarker(lf: LayoutInflater, marker: Marker, anchor: View) {
110-
val v: View = lf.inflate(marker.id, null)
111-
if (v.id <= 0) v.id = marker.id + 0
106+
private fun onLayoutMarker(context: Context, marker: Marker, anchor: View) {
107+
val v: View = marker.make.onMake(context, marker) ?: return
108+
if (v.id <= 0) v.id = marker.id + marker.hashCode()
112109
addView(v)
113110
val cs = ConstraintSet()
114111
cs.clone(this)

lib/src/main/java/com/pizzk/overlay/el/Anchor.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import android.graphics.Paint
55
import android.graphics.RectF
66
import android.view.View
77
import android.view.ViewGroup
8-
import androidx.annotation.IdRes
98

109
/**
1110
* 锚点信息类
1211
*/
1312
class Anchor(
14-
@IdRes
1513
val id: Int,
1614
val radius: Int,
1715
val circle: Boolean,
@@ -31,7 +29,7 @@ class Anchor(
3129
* 锚点查找接口,默认使用findViewById,可自定义实现查找匹配
3230
*/
3331
interface Find {
34-
fun onFind(parent: ViewGroup, @IdRes id: Int): View?
32+
fun onFind(parent: ViewGroup, e: Anchor): View?
3533
}
3634

3735
/**
@@ -57,8 +55,8 @@ class Anchor(
5755
}
5856

5957
open class AnchorFind : Find {
60-
override fun onFind(parent: ViewGroup, @IdRes id: Int): View? {
61-
return parent.findViewById(id)
58+
override fun onFind(parent: ViewGroup, e: Anchor): View? {
59+
return parent.findViewById(e.id)
6260
}
6361
}
6462

@@ -69,14 +67,14 @@ class Anchor(
6967
/**
7068
* 构建矩形锚点
7169
*/
72-
fun rect(@IdRes id: Int, radius: Int = 0, outset: Int = 0): Anchor {
70+
fun rect(id: Int, radius: Int = 0, outset: Int = 0): Anchor {
7371
return Anchor(id, radius, circle = false, outset)
7472
}
7573

7674
/**
7775
* 构建圆形锚点
7876
*/
79-
fun circle(@IdRes id: Int, outset: Int = 0): Anchor {
77+
fun circle(id: Int, outset: Int = 0): Anchor {
8078
return Anchor(id, radius = 0, circle = true, outset)
8179
}
8280
}

lib/src/main/java/com/pizzk/overlay/el/Marker.kt

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
package com.pizzk.overlay.el
22

3+
import android.content.Context
4+
import android.view.LayoutInflater
35
import android.view.View
6+
import android.widget.ImageView
7+
import androidx.annotation.DrawableRes
48
import androidx.annotation.IdRes
5-
import androidx.annotation.LayoutRes
69
import androidx.constraintlayout.widget.ConstraintSet
710

811
class Marker(
9-
@LayoutRes
1012
val id: Int,
11-
@IdRes
1213
val anchor: Int
1314
) {
15+
internal var make: Make = delegateMake
1416
internal var layout: Layout = delegateLayout
17+
internal var view: View? = null
18+
19+
interface Make {
20+
fun onMake(context: Context, marker: Marker): View?
21+
}
1522

1623
interface Layout {
1724
fun onLayout(cs: ConstraintSet, marker: View, anchor: View)
1825
}
1926

27+
/**
28+
* 提供Marker视图
29+
*/
30+
open class MarkerMake : Make {
31+
override fun onMake(context: Context, marker: Marker): View? {
32+
val view: View? = marker.view
33+
if (null != view) return view
34+
return LayoutInflater.from(context).inflate(marker.id, null)
35+
}
36+
}
37+
2038
/**
2139
* Marker在OverlayLayout中的默认布局实现
2240
*/
@@ -67,5 +85,17 @@ class Marker(
6785

6886
companion object {
6987
private val delegateLayout: Layout by lazy { MarkerLayout() }
88+
private val delegateMake: Make by lazy { MarkerMake() }
89+
90+
fun iv(context: Context, @DrawableRes res: Int): View? {
91+
return try {
92+
val view = ImageView(context)
93+
view.setImageResource(res)
94+
view
95+
} catch (e: Exception) {
96+
e.printStackTrace()
97+
null
98+
}
99+
}
70100
}
71101
}

lib/src/main/java/com/pizzk/overlay/el/Overlay.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.pizzk.overlay.el
22

3-
import androidx.annotation.IdRes
4-
import androidx.annotation.LayoutRes
3+
import android.view.View
54

65
class Overlay(val anchors: List<Anchor>, val markers: List<Marker>) {
76

@@ -21,26 +20,38 @@ class Overlay(val anchors: List<Anchor>, val markers: List<Marker>) {
2120
return this
2221
}
2322

24-
fun marker(@LayoutRes id: Int): Builder {
23+
fun marker(id: Int): Builder {
2524
markers.add(Marker(id, anchor))
2625
return this
2726
}
2827

28+
fun marker(id: Int, view: View?): Builder {
29+
val marker = Marker(id, anchor)
30+
marker.view = view
31+
markers.add(marker)
32+
return this
33+
}
34+
2935
fun build(): Overlay = Overlay(anchors, markers)
3036
}
3137

32-
fun anchor(@IdRes id: Int, draw: Anchor.Draw) {
38+
fun anchor(id: Int, draw: Anchor.Draw) {
3339
val anchor = anchors.find { it.id == id } ?: return
3440
anchor.draw = draw
3541
}
3642

37-
fun anchor(@IdRes id: Int, find: Anchor.Find) {
43+
fun anchor(id: Int, find: Anchor.Find) {
3844
val anchor = anchors.find { it.id == id } ?: return
3945
anchor.find = find
4046
}
4147

42-
fun marker(@LayoutRes id: Int, layout: Marker.Layout) {
48+
fun marker(id: Int, layout: Marker.Layout) {
4349
val marker = markers.find { it.id == id } ?: return
4450
marker.layout = layout
4551
}
52+
53+
fun marker(id: Int, make: Marker.Make) {
54+
val marker = markers.find { it.id == id } ?: return
55+
marker.make = make
56+
}
4657
}

0 commit comments

Comments
 (0)