Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support highlight mode #60

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions app/src/main/java/q/rorbin/badgeviewdemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,17 +41,18 @@ public class MainActivity extends AppCompatActivity {
List<RadioButton> 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<Badge> badges;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
initBadge();
swicth_draggable.setChecked(true);
switch_draggable.setChecked(true);
}

private void initBadge() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
android:textColor="#FFFFFF" />

<Switch
android:id="@+id/swicth_shadow"
android:id="@+id/switch_shadow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
Expand Down Expand Up @@ -348,7 +348,7 @@
android:textColor="#FFFFFF" />

<Switch
android:id="@+id/swicth_exact"
android:id="@+id/switch_exact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
Expand Down Expand Up @@ -378,6 +378,18 @@
android:paddingRight="10dp"
android:textColor="#FFFFFF" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HighlightMode : "
android:textColor="#FFFFFF" />

<Switch
android:id="@+id/switch_highlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:checked="false" />

</LinearLayout>

Expand All @@ -396,7 +408,7 @@
android:textColor="#FFFFFF" />

<Switch
android:id="@+id/swicth_draggable"
android:id="@+id/switch_draggable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
Expand Down
4 changes: 4 additions & 0 deletions badgeviewlib/src/main/java/q/rorbin/badgeview/Badge.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public interface Badge {

boolean isExactMode();

Badge setHighlightMode(boolean isHighlightMode);

boolean isHighlightMode();

Badge setShowShadow(boolean showShadow);

boolean isShowShadow();
Expand Down
44 changes: 31 additions & 13 deletions badgeviewlib/src/main/java/q/rorbin/badgeview/QBadgeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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() ?
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -832,7 +850,7 @@ private class BadgeContainer extends ViewGroup {

@Override
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
if(!(getParent() instanceof RelativeLayout)){
if (!(getParent() instanceof RelativeLayout)) {
super.dispatchRestoreInstanceState(container);
}
}
Expand Down