|
| 1 | +package com.pizzk.overlay; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.content.Context; |
| 5 | +import android.graphics.Bitmap; |
| 6 | +import android.graphics.Canvas; |
| 7 | +import android.graphics.Color; |
| 8 | +import android.graphics.Paint; |
| 9 | +import android.graphics.PorterDuff; |
| 10 | +import android.graphics.PorterDuffXfermode; |
| 11 | +import android.graphics.RectF; |
| 12 | +import android.util.AttributeSet; |
| 13 | +import android.view.View; |
| 14 | +import android.view.ViewGroup; |
| 15 | +import android.view.ViewParent; |
| 16 | + |
| 17 | +import androidx.annotation.ColorRes; |
| 18 | +import androidx.annotation.NonNull; |
| 19 | +import androidx.annotation.Nullable; |
| 20 | +import androidx.constraintlayout.widget.ConstraintLayout; |
| 21 | +import androidx.constraintlayout.widget.ConstraintSet; |
| 22 | +import androidx.core.content.ContextCompat; |
| 23 | + |
| 24 | +import com.pizzk.overlay.el.Anchor; |
| 25 | +import com.pizzk.overlay.el.Marker; |
| 26 | +import com.pizzk.overlay.el.Overlay; |
| 27 | + |
| 28 | +public class OverlayLayout extends ConstraintLayout { |
| 29 | + private final Paint paint = new Paint(); |
| 30 | + private final RectF rect = new RectF(); |
| 31 | + private int maskColor = Color.TRANSPARENT; |
| 32 | + private Bitmap bitmap = null; |
| 33 | + |
| 34 | + public OverlayLayout(@NonNull Context context) { |
| 35 | + this(context, null); |
| 36 | + } |
| 37 | + |
| 38 | + public OverlayLayout(@NonNull Context context, @Nullable AttributeSet attrs) { |
| 39 | + this(context, attrs, 0); |
| 40 | + } |
| 41 | + |
| 42 | + public OverlayLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 43 | + super(context, attrs, defStyleAttr); |
| 44 | + paint.setColor(Color.TRANSPARENT); |
| 45 | + paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); |
| 46 | + paint.setFlags(Paint.ANTI_ALIAS_FLAG); |
| 47 | + setWillNotDraw(false); |
| 48 | + setClickable(true); |
| 49 | + setMaskColor(R.color.overlay_mask); |
| 50 | + } |
| 51 | + |
| 52 | + public void setMaskColor(@ColorRes int colorId) { |
| 53 | + maskColor = ContextCompat.getColor(getContext(), colorId); |
| 54 | + } |
| 55 | + |
| 56 | + public void setOverlay(Overlay overlay) { |
| 57 | + removeAllViews(); |
| 58 | + setOnClickListener(null); |
| 59 | + if (null == overlay) return; |
| 60 | + ViewParent p = getParent(); |
| 61 | + if (!(p instanceof ViewGroup)) return; |
| 62 | + ViewGroup view = (ViewGroup) p; |
| 63 | + post(() -> { |
| 64 | + try { |
| 65 | + onChangeLayout(view, overlay); |
| 66 | + } catch (Exception e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + public void setVisibility(boolean v) { |
| 73 | + if (v == (View.VISIBLE == getVisibility())) return; |
| 74 | + setVisibility(v ? View.VISIBLE : View.GONE); |
| 75 | + } |
| 76 | + |
| 77 | + private void onChangeLayout(ViewGroup viewGroup, Overlay overlay) { |
| 78 | + //准备画布 |
| 79 | + |
| 80 | + Bitmap bmp = null != bitmap ? bitmap : Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); |
| 81 | + bitmap = bmp; |
| 82 | + bmp.eraseColor(Color.TRANSPARENT); |
| 83 | + Canvas canvas = new Canvas(bmp); |
| 84 | + canvas.drawColor(maskColor); |
| 85 | + //计算位置 |
| 86 | + int[] vXY = new int[2]; |
| 87 | + getLocationOnScreen(vXY); |
| 88 | + int[] vAnchorXY = new int[2]; |
| 89 | + for (Anchor e : overlay.anchors) { |
| 90 | + View vAnchor = e.getFind().onFind(viewGroup, e); |
| 91 | + if (null == vAnchor) return; |
| 92 | + //计算宽度及位置 |
| 93 | + vAnchor.getLocationOnScreen(vAnchorXY); |
| 94 | + rect.left = vAnchorXY[0] - vXY[0] - 0f; |
| 95 | + rect.top = vAnchorXY[1] - vXY[1] - 0f; |
| 96 | + rect.right = rect.left + vAnchor.getWidth(); |
| 97 | + rect.bottom = rect.top + vAnchor.getHeight(); |
| 98 | + //镂空样式 |
| 99 | + rect.inset(-e.outset, -e.outset); |
| 100 | + //锚点生产及绘制 |
| 101 | + e.getDraw().onDraw(canvas, paint, e, rect); |
| 102 | + View anchor = onFakeAnchor(e.id, rect); |
| 103 | + //标记层布局 |
| 104 | + for (Marker marker : overlay.markers) { |
| 105 | + if (marker.anchor != e.id) continue; |
| 106 | + onLayoutMarker(viewGroup.getContext(), marker, anchor); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + private View onFakeAnchor(int id, RectF rc) { |
| 112 | + View v = getViewById(id); |
| 113 | + if (null != v) return v; |
| 114 | + View view = new View(getContext()); |
| 115 | + view.setId(id); |
| 116 | + addView(view, (int) rc.width(), (int) rc.height()); |
| 117 | + ConstraintSet cs = new ConstraintSet(); |
| 118 | + cs.clone(this); |
| 119 | + int iid = ConstraintSet.PARENT_ID; |
| 120 | + cs.connect(id, ConstraintSet.START, iid, ConstraintSet.START, (int) rc.left); |
| 121 | + cs.connect(id, ConstraintSet.TOP, iid, ConstraintSet.TOP, (int) rc.top); |
| 122 | + cs.applyTo(this); |
| 123 | + return view; |
| 124 | + } |
| 125 | + |
| 126 | + @SuppressLint("ResourceType") |
| 127 | + private void onLayoutMarker(Context context, Marker marker, View anchor) { |
| 128 | + View v = marker.getMake().onMake(context, marker); |
| 129 | + if (null == v) return; |
| 130 | + if (v.getId() <= 0) v.setId(marker.id + marker.hashCode()); |
| 131 | + addView(v); |
| 132 | + ConstraintSet cs = new ConstraintSet(); |
| 133 | + cs.clone(this); |
| 134 | + marker.getLayout().onLayout(cs, v, anchor); |
| 135 | + cs.applyTo(this); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + protected void onDraw(Canvas canvas) { |
| 140 | + if (null != bitmap && null != canvas) { |
| 141 | + canvas.drawBitmap(bitmap, 0f, 0f, null); |
| 142 | + } |
| 143 | + super.onDraw(canvas); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + protected void onDetachedFromWindow() { |
| 148 | + super.onDetachedFromWindow(); |
| 149 | + if (null != bitmap) bitmap.recycle(); |
| 150 | + bitmap = null; |
| 151 | + } |
| 152 | +} |
0 commit comments