Map highlight overlay drawable to view

Summary:
For the Sections plugin I need to be able to highlight multiple views at the same time (without highlighting their parent view). This enables us to do that. Let me know if you have any concerns
Depends on D13803412

Reviewed By: astreet

Differential Revision: D13900729

fbshipit-source-id: cdea9c21ceb11563793410b2bb8f7f2c3a44cefb
This commit is contained in:
Mihaela Ogrezeanu
2019-02-04 06:07:17 -08:00
committed by Facebook Github Bot
parent dc412ce0bc
commit 3d03f44f1b
3 changed files with 39 additions and 18 deletions

View File

@@ -17,13 +17,16 @@ import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.view.View;
import java.util.Map;
import java.util.WeakHashMap;
import javax.annotation.Nullable;
public class BoundsDrawable extends Drawable {
public static final int COLOR_HIGHLIGHT_CONTENT = 0x888875c5;
public static final int COLOR_HIGHLIGHT_PADDING = 0x889dd185;
public static final int COLOR_HIGHLIGHT_MARGIN = 0x88f7b77b;
private static @Nullable BoundsDrawable sInstance;
private static @Nullable Map<View, BoundsDrawable> sInstanceMap;
private final TextPaint mTextPaint;
private final Paint mMarginPaint;
@@ -39,17 +42,24 @@ public class BoundsDrawable extends Drawable {
private final float mDensity;
public static BoundsDrawable getInstance(
float density, Rect marginBounds, Rect paddingBounds, Rect contentBounds) {
final BoundsDrawable drawable = getInstance(density);
View view, float density, Rect marginBounds, Rect paddingBounds, Rect contentBounds) {
final BoundsDrawable drawable = getInstance(view, density);
drawable.setBounds(marginBounds, paddingBounds, contentBounds);
return drawable;
}
public static BoundsDrawable getInstance(float density) {
if (sInstance == null) {
sInstance = new BoundsDrawable(density);
public static BoundsDrawable getInstance(View view, float density) {
if (sInstanceMap == null) {
sInstanceMap = new WeakHashMap<>();
}
return sInstance;
if (sInstanceMap.containsKey(view)) {
return sInstanceMap.get(view);
}
final BoundsDrawable drawable = new BoundsDrawable(density);
sInstanceMap.put(view, drawable);
return drawable;
}
private BoundsDrawable(float density) {

View File

@@ -45,7 +45,8 @@ public class HighlightedOverlay {
margin = enclose(margin, padding);
final float density = targetView.getContext().getResources().getDisplayMetrics().density;
final Drawable overlay = BoundsDrawable.getInstance(density, margin, padding, contentBounds);
final Drawable overlay =
BoundsDrawable.getInstance(targetView, density, margin, padding, contentBounds);
targetView.getOverlay().add(overlay);
@@ -60,7 +61,7 @@ public class HighlightedOverlay {
coords[1] + contentBounds.bottom);
final Drawable lineOverlay =
LinesDrawable.getInstance(density, margin, padding, lineContentBounds);
LinesDrawable.getInstance(targetView, density, margin, padding, lineContentBounds);
targetView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
@@ -74,8 +75,8 @@ public class HighlightedOverlay {
}
final float density = targetView.getContext().getResources().getDisplayMetrics().density;
final Drawable overlay = BoundsDrawable.getInstance(density);
final Drawable overlay2 = LinesDrawable.getInstance(density);
final Drawable overlay = BoundsDrawable.getInstance(targetView, density);
final Drawable overlay2 = LinesDrawable.getInstance(targetView, density);
targetView.getRootView().getOverlay().remove(overlay2);
targetView.getOverlay().remove(overlay);

View File

@@ -15,10 +15,12 @@ import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import java.util.Map;
import java.util.WeakHashMap;
import javax.annotation.Nullable;
public class LinesDrawable extends Drawable {
private static @Nullable LinesDrawable sInstance;
private final Rect mWorkRect;
private final Rect mMarginBounds;
@@ -26,19 +28,27 @@ public class LinesDrawable extends Drawable {
private final Rect mContentBounds;
private final float mDensity;
private static @Nullable Map<View, LinesDrawable> sInstanceMap;
public static LinesDrawable getInstance(
float density, Rect marginBounds, Rect paddingBounds, Rect contentBounds) {
final LinesDrawable drawable = getInstance(density);
View view, float density, Rect marginBounds, Rect paddingBounds, Rect contentBounds) {
final LinesDrawable drawable = getInstance(view, density);
drawable.setBounds(marginBounds, paddingBounds, contentBounds);
return drawable;
}
public static LinesDrawable getInstance(float density) {
if (sInstance == null) {
sInstance = new LinesDrawable(density);
public static LinesDrawable getInstance(View view, float density) {
if (sInstanceMap == null) {
sInstanceMap = new WeakHashMap<>();
}
return sInstance;
if (sInstanceMap.containsKey(view)) {
return sInstanceMap.get(view);
}
LinesDrawable drawable = new LinesDrawable(density);
sInstanceMap.put(view, drawable);
return drawable;
}
private LinesDrawable(float density) {