Don't show shadow for text with light color

This commit is contained in:
serso 2016-05-11 23:30:30 +02:00
parent 06dfc074b1
commit 55004531eb
2 changed files with 23 additions and 6 deletions

View File

@ -100,7 +100,7 @@ public class DirectionDragButton extends DragButton implements DirectionDragView
} }
this.highContrast = highContrast; this.highContrast = highContrast;
this.textView.setHighContrast(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); setShadowLayer(applyDimension(COMPLEX_UNIT_DIP, SHADOW_RADIUS_DPS, getResources().getDisplayMetrics()), 0, 0, BLACK);
} else { } else {
setShadowLayer(0, 0, 0, BLACK); setShadowLayer(0, 0, 0, BLACK);

View File

@ -3,20 +3,20 @@ package org.solovyev.android.views.dragbutton;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF; import android.graphics.PointF;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.ColorUtils;
import android.text.TextPaint; import android.text.TextPaint;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import org.solovyev.android.Check; import org.solovyev.android.Check;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
@ -111,6 +111,7 @@ public class DirectionTextView {
private String value = ""; private String value = "";
private float scale; private float scale;
private int color; private int color;
private int contrastColor;
private float alpha; private float alpha;
private boolean visible = true; private boolean visible = true;
private boolean highContrast; private boolean highContrast;
@ -136,9 +137,15 @@ public class DirectionTextView {
} }
alpha = defAlpha; alpha = defAlpha;
color = defColor; color = defColor;
contrastColor = makeContrastColor(color);
initPaint(base); 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) { public void initPaint(@NonNull TextPaint base) {
paint.set(base); paint.set(base);
paint.setColor(color); paint.setColor(color);
@ -161,7 +168,7 @@ public class DirectionTextView {
} }
private void initPaintShadow() { private void initPaintShadow() {
if (highContrast) { if (highContrast && needsShadow(color)) {
paint.setShadowLayer(applyDimension(COMPLEX_UNIT_DIP, SHADOW_RADIUS_DPS, view.getResources().getDisplayMetrics()), 0, 0, BLACK); paint.setShadowLayer(applyDimension(COMPLEX_UNIT_DIP, SHADOW_RADIUS_DPS, view.getResources().getDisplayMetrics()), 0, 0, BLACK);
} else { } else {
paint.setShadowLayer(0, 0, 0, BLACK); paint.setShadowLayer(0, 0, 0, BLACK);
@ -202,9 +209,11 @@ public class DirectionTextView {
return; return;
} }
this.color = color; this.color = color;
this.contrastColor = makeContrastColor(color);
this.alpha = alpha; this.alpha = alpha;
paint.setColor(color); paint.setColor(color);
paint.setAlpha(intAlpha()); paint.setAlpha(intAlpha());
initPaintShadow();
invalidate(false); invalidate(false);
} }
@ -224,7 +233,7 @@ public class DirectionTextView {
calculatePosition(); calculatePosition();
} }
if (highContrast) { if (highContrast) {
paint.setColor(Color.WHITE); paint.setColor(contrastColor);
paint.setAlpha(255); paint.setAlpha(255);
} }
final int width = view.getWidth(); final int width = view.getWidth();
@ -296,4 +305,12 @@ public class DirectionTextView {
return visible && !TextUtils.isEmpty(value); 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);
}
} }