From 55004531ebe15d5219b01d7949ee6fa7f2d340b1 Mon Sep 17 00:00:00 2001 From: serso Date: Wed, 11 May 2016 23:30:30 +0200 Subject: [PATCH] Don't show shadow for text with light color --- .../views/dragbutton/DirectionDragButton.java | 2 +- .../views/dragbutton/DirectionTextView.java | 27 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/views/dragbutton/DirectionDragButton.java b/app/src/main/java/org/solovyev/android/views/dragbutton/DirectionDragButton.java index 3d44f3e2..0d3c69ad 100644 --- a/app/src/main/java/org/solovyev/android/views/dragbutton/DirectionDragButton.java +++ b/app/src/main/java/org/solovyev/android/views/dragbutton/DirectionDragButton.java @@ -100,7 +100,7 @@ public class DirectionDragButton extends DragButton implements DirectionDragView } this.highContrast = highContrast; this.textView.setHighContrast(highContrast); - if (highContrast) { + if (highContrast && DirectionTextView.needsShadow(getCurrentTextColor())) { setShadowLayer(applyDimension(COMPLEX_UNIT_DIP, SHADOW_RADIUS_DPS, getResources().getDisplayMetrics()), 0, 0, BLACK); } else { setShadowLayer(0, 0, 0, BLACK); diff --git a/app/src/main/java/org/solovyev/android/views/dragbutton/DirectionTextView.java b/app/src/main/java/org/solovyev/android/views/dragbutton/DirectionTextView.java index f268318c..3af01572 100644 --- a/app/src/main/java/org/solovyev/android/views/dragbutton/DirectionTextView.java +++ b/app/src/main/java/org/solovyev/android/views/dragbutton/DirectionTextView.java @@ -3,20 +3,20 @@ package org.solovyev.android.views.dragbutton; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; -import android.graphics.Color; import android.graphics.PointF; import android.graphics.Rect; import android.graphics.Typeface; +import android.support.annotation.ColorInt; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v4.content.ContextCompat; +import android.support.v4.graphics.ColorUtils; import android.text.TextPaint; import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; import android.widget.TextView; - import com.google.common.base.Strings; - import org.solovyev.android.Check; import org.solovyev.android.calculator.R; @@ -111,6 +111,7 @@ public class DirectionTextView { private String value = ""; private float scale; private int color; + private int contrastColor; private float alpha; private boolean visible = true; private boolean highContrast; @@ -136,9 +137,15 @@ public class DirectionTextView { } alpha = defAlpha; color = defColor; + contrastColor = makeContrastColor(color); initPaint(base); } + private int makeContrastColor(int color) { + final int colorRes = isLightColor(color) ? R.color.cpp_button_text : R.color.cpp_text_inverse; + return ContextCompat.getColor(view.getContext(), colorRes); + } + public void initPaint(@NonNull TextPaint base) { paint.set(base); paint.setColor(color); @@ -161,7 +168,7 @@ public class DirectionTextView { } private void initPaintShadow() { - if (highContrast) { + if (highContrast && needsShadow(color)) { paint.setShadowLayer(applyDimension(COMPLEX_UNIT_DIP, SHADOW_RADIUS_DPS, view.getResources().getDisplayMetrics()), 0, 0, BLACK); } else { paint.setShadowLayer(0, 0, 0, BLACK); @@ -202,9 +209,11 @@ public class DirectionTextView { return; } this.color = color; + this.contrastColor = makeContrastColor(color); this.alpha = alpha; paint.setColor(color); paint.setAlpha(intAlpha()); + initPaintShadow(); invalidate(false); } @@ -224,7 +233,7 @@ public class DirectionTextView { calculatePosition(); } if (highContrast) { - paint.setColor(Color.WHITE); + paint.setColor(contrastColor); paint.setAlpha(255); } final int width = view.getWidth(); @@ -296,4 +305,12 @@ public class DirectionTextView { return visible && !TextUtils.isEmpty(value); } } + + private static boolean isLightColor(@ColorInt int color) { + return ColorUtils.calculateLuminance(color) > 0.5f; + } + + public static boolean needsShadow(@ColorInt int color) { + return isLightColor(color); + } }