diff --git a/app/src/main/java/q/rorbin/badgeviewdemo/MainActivity.java b/app/src/main/java/q/rorbin/badgeviewdemo/MainActivity.java index 84e6bda..2c0fb4b 100644 --- a/app/src/main/java/q/rorbin/badgeviewdemo/MainActivity.java +++ b/app/src/main/java/q/rorbin/badgeviewdemo/MainActivity.java @@ -3,20 +3,13 @@ import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; -import android.graphics.drawable.Drawable; -import android.os.PersistableBundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; -import android.text.SpannableString; -import android.text.SpannableStringBuilder; -import android.text.Spanned; import android.text.TextUtils; import android.text.TextWatcher; -import android.text.style.ImageSpan; import android.util.DisplayMetrics; -import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; @@ -48,9 +41,10 @@ public class MainActivity extends AppCompatActivity { List radioButtons = new ArrayList<>(); CompoundButton lastRadioButton; SeekBar seekBar_offsetx, seekBar_padding, seekBar_offsety, seekBar_numbersize; - Switch swicth_exact, swicth_draggable, swicth_shadow; + Switch switch_exact, switch_draggable, switch_shadow, switch_highlight; List badges; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -58,7 +52,7 @@ protected void onCreate(Bundle savedInstanceState) { initView(); initListener(); initBadge(); - swicth_draggable.setChecked(true); + switch_draggable.setChecked(true); } private void initBadge() { @@ -102,9 +96,10 @@ private void initView() { seekBar_offsety = (SeekBar) findViewById(R.id.seekBar_offsety); seekBar_padding = (SeekBar) findViewById(R.id.seekBar_padding); seekBar_numbersize = (SeekBar) findViewById(R.id.seekBar_numbersize); - swicth_exact = (Switch) findViewById(R.id.swicth_exact); - swicth_draggable = (Switch) findViewById(R.id.swicth_draggable); - swicth_shadow = (Switch) findViewById(R.id.swicth_shadow); + switch_exact = (Switch) findViewById(R.id.switch_exact); + switch_draggable = (Switch) findViewById(R.id.switch_draggable); + switch_shadow = (Switch) findViewById(R.id.switch_shadow); + switch_highlight = (Switch) findViewById(R.id.switch_highlight); } private void initListener() { @@ -262,9 +257,9 @@ public void afterTextChanged(Editable s) { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { for (Badge badge : badges) { - if (buttonView == swicth_exact) { + if (buttonView == switch_exact) { badge.setExactMode(isChecked); - } else if (buttonView == swicth_draggable) { + } else if (buttonView == switch_draggable) { badge.setOnDragStateChangedListener(isChecked ? new Badge.OnDragStateChangedListener() { @Override @@ -288,15 +283,18 @@ public void onDragStateChanged(int dragState, Badge badge, View targetView) { } } } : null); - } else if (buttonView == swicth_shadow) { + } else if (buttonView == switch_shadow) { badge.setShowShadow(isChecked); + } else if (buttonView == switch_highlight) { + badge.setHighlightMode(isChecked); } } } }; - swicth_exact.setOnCheckedChangeListener(onCheckedChangeListener); - swicth_draggable.setOnCheckedChangeListener(onCheckedChangeListener); - swicth_shadow.setOnCheckedChangeListener(onCheckedChangeListener); + switch_exact.setOnCheckedChangeListener(onCheckedChangeListener); + switch_draggable.setOnCheckedChangeListener(onCheckedChangeListener); + switch_shadow.setOnCheckedChangeListener(onCheckedChangeListener); + switch_highlight.setOnCheckedChangeListener(onCheckedChangeListener); } private void selectorColor(final OnColorClickListener l) { diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8e27069..0f7efcb 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -266,7 +266,7 @@ android:textColor="#FFFFFF" /> @@ -348,7 +348,7 @@ android:textColor="#FFFFFF" /> + + + @@ -396,7 +408,7 @@ android:textColor="#FFFFFF" /> diff --git a/badgeviewlib/src/main/java/q/rorbin/badgeview/Badge.java b/badgeviewlib/src/main/java/q/rorbin/badgeview/Badge.java index 6dd6b41..091b24f 100644 --- a/badgeviewlib/src/main/java/q/rorbin/badgeview/Badge.java +++ b/badgeviewlib/src/main/java/q/rorbin/badgeview/Badge.java @@ -23,6 +23,10 @@ public interface Badge { boolean isExactMode(); + Badge setHighlightMode(boolean isHighlightMode); + + boolean isHighlightMode(); + Badge setShowShadow(boolean showShadow); boolean isShowShadow(); diff --git a/badgeviewlib/src/main/java/q/rorbin/badgeview/QBadgeView.java b/badgeviewlib/src/main/java/q/rorbin/badgeview/QBadgeView.java index c96a30f..bb32827 100644 --- a/badgeviewlib/src/main/java/q/rorbin/badgeview/QBadgeView.java +++ b/badgeviewlib/src/main/java/q/rorbin/badgeview/QBadgeView.java @@ -52,6 +52,7 @@ public class QBadgeView extends View implements Badge { protected int mBadgeGravity; protected float mGravityOffsetX; protected float mGravityOffsetY; + protected boolean mHighlightMode; protected float mDefalutRadius; protected float mFinalDragDistance; @@ -155,7 +156,7 @@ public Badge bindTarget(final View targetView) { ViewGroup.LayoutParams targetParams = targetView.getLayoutParams(); targetContainer.removeView(targetView); final BadgeContainer badgeContainer = new BadgeContainer(getContext()); - if(targetContainer instanceof RelativeLayout){ + if (targetContainer instanceof RelativeLayout) { badgeContainer.setId(targetView.getId()); } targetContainer.addView(badgeContainer, index, targetParams); @@ -394,7 +395,7 @@ private void drawBadge(Canvas canvas, PointF center, float radius) { if (center.x == -1000 && center.y == -1000) { return; } - if (mBadgeText.isEmpty() || mBadgeText.length() == 1) { + if (mBadgeText.length() <= 1 || mHighlightMode) { mBadgeBackgroundRect.left = center.x - (int) radius; mBadgeBackgroundRect.top = center.y - (int) radius; mBadgeBackgroundRect.right = center.x + (int) radius; @@ -422,7 +423,7 @@ private void drawBadge(Canvas canvas, PointF center, float radius) { } } } - if (!mBadgeText.isEmpty()) { + if (!mBadgeText.isEmpty() && !mHighlightMode) { canvas.drawText(mBadgeText, center.x, (mBadgeBackgroundRect.bottom + mBadgeBackgroundRect.top - mBadgeTextFontMetrics.bottom - mBadgeTextFontMetrics.top) / 2f, @@ -448,7 +449,7 @@ private void drawBadgeBackground(Canvas canvas) { canvas.drawBitmap(mBitmapClip, left, top, mBadgeBackgroundPaint); canvas.restore(); mBadgeBackgroundPaint.setXfermode(null); - if (mBadgeText.isEmpty() || mBadgeText.length() == 1) { + if (mBadgeText.length() <= 1 || mHighlightMode) { canvas.drawCircle(mBadgeBackgroundRect.centerX(), mBadgeBackgroundRect.centerY(), mBadgeBackgroundRect.width() / 2f, mBadgeBackgroundBorderPaint); } else { @@ -472,7 +473,7 @@ private void createClipLayer() { mBitmapClip.recycle(); } float radius = getBadgeCircleRadius(); - if (mBadgeText.isEmpty() || mBadgeText.length() == 1) { + if (mBadgeText.length() <= 1 || mHighlightMode) { mBitmapClip = Bitmap.createBitmap((int) radius * 2, (int) radius * 2, Bitmap.Config.ARGB_4444); Canvas srcCanvas = new Canvas(mBitmapClip); @@ -493,7 +494,7 @@ private void createClipLayer() { } private float getBadgeCircleRadius() { - if (mBadgeText.isEmpty()) { + if (mBadgeText.isEmpty() || mHighlightMode) { return mBadgePadding; } else if (mBadgeText.length() == 1) { return mBadgeTextRect.height() > mBadgeTextRect.width() ? @@ -551,12 +552,18 @@ private void findBadgeCenter() { private void measureText() { mBadgeTextRect.left = 0; mBadgeTextRect.top = 0; - if (TextUtils.isEmpty(mBadgeText)) { + + String text = mBadgeText; + if (mHighlightMode) { + text = ""; + } + + if (TextUtils.isEmpty(text) && !mHighlightMode) { mBadgeTextRect.right = 0; mBadgeTextRect.bottom = 0; } else { mBadgeTextPaint.setTextSize(mBadgeTextSize); - mBadgeTextRect.right = mBadgeTextPaint.measureText(mBadgeText); + mBadgeTextRect.right = mBadgeTextPaint.measureText(text); mBadgeTextFontMetrics = mBadgeTextPaint.getFontMetrics(); mBadgeTextRect.bottom = mBadgeTextFontMetrics.descent - mBadgeTextFontMetrics.ascent; } @@ -607,14 +614,12 @@ public void hide(boolean animate) { @Override public Badge setBadgeNumber(int badgeNumber) { mBadgeNumber = badgeNumber; - if (mBadgeNumber < 0) { - mBadgeText = ""; + if (mBadgeNumber <= 0) { + mBadgeText = null; } else if (mBadgeNumber > 99) { mBadgeText = mExact ? String.valueOf(mBadgeNumber) : "99+"; } else if (mBadgeNumber > 0 && mBadgeNumber <= 99) { mBadgeText = String.valueOf(mBadgeNumber); - } else if (mBadgeNumber == 0) { - mBadgeText = null; } measureText(); invalidate(); @@ -654,6 +659,19 @@ public boolean isExactMode() { return mExact; } + @Override + public Badge setHighlightMode(boolean isHighlightMode) { + mHighlightMode = isHighlightMode; + measureText(); + invalidate(); + return this; + } + + @Override + public boolean isHighlightMode() { + return mHighlightMode; + } + @Override public Badge setShowShadow(boolean showShadow) { mShowShadow = showShadow; @@ -832,7 +850,7 @@ private class BadgeContainer extends ViewGroup { @Override protected void dispatchRestoreInstanceState(SparseArray container) { - if(!(getParent() instanceof RelativeLayout)){ + if (!(getParent() instanceof RelativeLayout)) { super.dispatchRestoreInstanceState(container); } }