Skip to content

Commit

Permalink
Auto adjust bounds' width
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed May 31, 2016
1 parent 15bff66 commit 4c1e5db
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 39 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@ SpannableString spannableString =
textView.setText(spannableString);
```

If the drawable's bounds was setted by manual or content view. It will auto cut the text to adjust the bounds' width.

![](art/1.png)

You can also use the badge drawable for ImageView and other more view.
Binary file added art/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion lib-badge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.1"
versionName "1.2"
}
buildTypes {
release {
Expand Down
68 changes: 59 additions & 9 deletions lib-badge/src/main/java/cn/nekocode/badge/BadgeDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class BadgeDrawable extends Drawable {
private Paint paint;
private Paint.FontMetrics fontMetrics;

private boolean needSetBounds = false;
private boolean isAutoSetBounds = false;

public static class Builder {
private BadgeDrawable badgeDrawable;
Expand Down Expand Up @@ -185,23 +185,26 @@ public String getText2() {
return text2;
}

public void setNeedSetBounds(boolean needSetBounds) {
this.needSetBounds = needSetBounds;
public void setAutoSetBounds(boolean autoSetBounds) {
this.isAutoSetBounds = autoSetBounds;
}

private void measureBadge() {
switch (badgeType) {
case TYPE_ONLY_ONE_TEXT:
text1Width = (int) paint.measureText(text1);
badgeHeight = (int) (textSize * 1.4f);
badgeWidth = (int) (paint.measureText(text1) + textSize * 0.4f);
badgeWidth = (int) (text1Width + textSize * 0.4f);

setCornerRadius(DEFAULT_CORNER_RADIUS);
break;

case TYPE_WITH_TWO_TEXT:
badgeHeight = (int) (textSize * 1.4f);
text1Width = (int) paint.measureText(text1);
text2Width = (int) paint.measureText(text2);
badgeHeight = (int) (textSize * 1.4f);
badgeWidth = (int) (text1Width + text2Width + textSize * 0.6f);

setCornerRadius(DEFAULT_CORNER_RADIUS);
break;

Expand All @@ -210,10 +213,38 @@ private void measureBadge() {
setCornerRadius(badgeHeight);
}

if (needSetBounds)
if (isAutoSetBounds)
setBounds(0, 0, badgeWidth, badgeHeight);
}

@Override
public void setBounds(int left, int top, int right, int bottom) {
super.setBounds(left, top, right, bottom);
if (isAutoSetBounds)
return;

int boundsWidth = right - left;
switch (badgeType) {
case TYPE_ONLY_ONE_TEXT:
if(!isAutoSetBounds && boundsWidth < badgeWidth) {
text1Width = (int) (boundsWidth - textSize * 0.4f);
text1Width = text1Width > 0 ? text1Width : 0;

badgeWidth = (int) (text1Width + textSize * 0.4f);
}
break;

case TYPE_WITH_TWO_TEXT:
if(!isAutoSetBounds && boundsWidth < badgeWidth) {
text2Width = (int) (boundsWidth - text1Width - textSize * 0.6f);
text2Width = text2Width > 0 ? text2Width : 0;

badgeWidth = (int) (text1Width + text2Width + textSize * 0.6f);
}
break;
}
}

@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
Expand All @@ -237,7 +268,7 @@ public void draw(Canvas canvas) {
case TYPE_ONLY_ONE_TEXT:
paint.setColor(textColor);
canvas.drawText(
text1,
cutText(text1, text1Width),
textCx,
textCy + textCyOffset,
paint);
Expand All @@ -262,7 +293,7 @@ public void draw(Canvas canvas) {

paint.setColor(badgeColor);
canvas.drawText(
text2,
cutText(text2, text2Width),
bounds.width() - marginLeftAndRight - text2Width / 2f - textSize * 0.2f,
textCy + textCyOffset,
paint);
Expand Down Expand Up @@ -301,10 +332,29 @@ private String cutNumber(int number, int width) {
return "...";
}

private String cutText(String text, int width) {
if (paint.measureText(text) <= width)
return text;

String suffix = "...";
while(paint.measureText(text + suffix) > width) {
if(text.length() > 0)
text = text.substring(0, text.length() - 1);

if(text.length() == 0) {
suffix = suffix.substring(0, suffix.length() - 1);

if(suffix.length() == 0) break;
}
}

return text + suffix;
}

public SpannableString toSpannable() {
final SpannableString spanStr = new SpannableString(" ");
spanStr.setSpan(new ImageSpan(this, ImageSpan.ALIGN_BOTTOM), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
needSetBounds = true;
isAutoSetBounds = true;
setBounds(0, 0, badgeWidth, badgeHeight);

return spanStr;
Expand Down
58 changes: 29 additions & 29 deletions sample/src/main/java/cn/nekocode/badge/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,37 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView)findViewById(R.id.tvHelloWorld);
final ImageView imageView = (ImageView)findViewById(R.id.imageView);
final TextView textView = (TextView) findViewById(R.id.tvHelloWorld);
final ImageView imageView = (ImageView) findViewById(R.id.imageView);

final BadgeDrawable drawable =
new BadgeDrawable.Builder()
.type(BadgeDrawable.TYPE_NUMBER)
.number(9)
.build();
final BadgeDrawable drawable =
new BadgeDrawable.Builder()
.type(BadgeDrawable.TYPE_NUMBER)
.number(9)
.build();

final BadgeDrawable drawable2 =
new BadgeDrawable.Builder()
.type(BadgeDrawable.TYPE_ONLY_ONE_TEXT)
.badgeColor(0xff336699)
.text1("VIP")
.build();
final BadgeDrawable drawable2 =
new BadgeDrawable.Builder()
.type(BadgeDrawable.TYPE_ONLY_ONE_TEXT)
.badgeColor(0xff336699)
.text1("VIP")
.build();

final BadgeDrawable drawable3 =
new BadgeDrawable.Builder()
.type(BadgeDrawable.TYPE_WITH_TWO_TEXT)
.badgeColor(0xffCC9933)
.text1("LEVEL")
.text2("10")
.build();
final BadgeDrawable drawable3 =
new BadgeDrawable.Builder()
.type(BadgeDrawable.TYPE_WITH_TWO_TEXT)
.badgeColor(0xffCC9933)
.text1("LEVEL")
.text2("10")
.build();

final BadgeDrawable drawable4 =
new BadgeDrawable.Builder()
.type(BadgeDrawable.TYPE_NUMBER)
.number(999)
.badgeColor(0xff666666)
.textColor(0xffFFFF00)
.build();
final BadgeDrawable drawable4 =
new BadgeDrawable.Builder()
.type(BadgeDrawable.TYPE_NUMBER)
.number(999)
.badgeColor(0xff666666)
.textColor(0xffFFFF00)
.build();

SpannableString spannableString =
new SpannableString(TextUtils.concat(
Expand All @@ -60,11 +60,11 @@ protected void onCreate(Bundle savedInstanceState) {
drawable4.toSpannable()
));

if(textView != null) {
if (textView != null) {
textView.setText(spannableString);
}

if(imageView != null) {
if (imageView != null) {
final BadgeDrawable drawable5 =
new BadgeDrawable.Builder()
.type(BadgeDrawable.TYPE_WITH_TWO_TEXT)
Expand Down

0 comments on commit 4c1e5db

Please sign in to comment.