Skip to content

Commit

Permalink
未读数超过99显示省略号
Browse files Browse the repository at this point in the history
  • Loading branch information
hijunmeng committed Jul 3, 2017
1 parent b727b36 commit c51d13b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/src/main/java/com/hwj/demo/sb/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onClick(int index) {

public void onClickChangeUnread(View view) {
for (int i = 0; i < binding.sbTab2.getLabelCount(); i++) {
binding.sbTab2.setLabelUnreadCount(i, (int) (Math.random() * 12) - 1);
binding.sbTab2.setLabelUnreadCount(i, (int) (Math.random() * 120) - 1);
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
app:sbar_labelArray="标签1|标签2|标签3|标签4"
app:sbar_unreadMarginRight="10dp"
app:sbar_unreadMarginTop="5dp"
app:sbar_unreadPadding="1dp"
app:sbar_unreadPadding="3dp"
app:sbar_unreadTextSize="8sp"
/>

Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -15,7 +15,7 @@ buildscript {
allprojects {
repositories {
jcenter()
}
}
}

task clean(type: Delete) {
Expand All @@ -26,10 +26,10 @@ ext{
minSdkVersion = 16
targetSdkVersion = 25
compileSdkVersion = 25
buildToolsVersion = '25.0.1'
buildToolsVersion = '26.0.0'

// App dependencies
supportLibraryVersion = '25.0.1'
supportLibraryVersion = '25.3.1'
junitVersion = '4.12'
espressoVersion = '2.2.2'
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
24 changes: 22 additions & 2 deletions lib_segmentbar/src/main/java/com/hwj/junmeng/sb/SegmentBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ public class SegmentBar extends View {
private float textHeight; //标签文字高度
private float unreadTextHeight; //未读数文字高度

private int maxUnreadCount=99;//显示的最大未读数

private String[] texts;//标签数组
private int[] unreadTexts;//标签未读数数组
private int maxUnreadBit;//未读数最大位数,用于计算圆点宽度

OnItemClickListener listener;
GradientDrawable gradientDrawable = new GradientDrawable();
Expand Down Expand Up @@ -172,7 +175,13 @@ protected void onDraw(Canvas canvas) {
unreadPaint.setTextSize(unreadTextSize);
unreadPaint.setColor(unreadBackgroundColor);
int unreadCount = unreadTexts[i];
int unreadTextWidth = getTextWidth(unreadPaint, "" + unreadTexts[i]);
String unreadText="";
if(unreadCount>maxUnreadCount){
unreadText="...";
}else{
unreadText=""+unreadCount;
}
int unreadTextWidth = getTextWidth(unreadPaint, unreadText);
unreadTextHeight = unreadPaint.descent() - unreadPaint.ascent();
float redRadius = unreadTextHeight / 2.0f + unreadPadding;//红点的半径
float redCenterX = labelWidth * (i + 1) - redRadius - unreadMarginRight;//红点的中心x
Expand All @@ -188,7 +197,7 @@ protected void onDraw(Canvas canvas) {
unreadPaint.setTextSize(unreadTextSize);
unreadPaint.setStrokeWidth(borderWidth);
//未读数文字的宽度
canvas.drawText("" + unreadTexts[i], redCenterX - unreadTextWidth / 2.0f, redCenterY + unreadTextHeight / 2.0f - unreadTextHeight / 5.0f, unreadPaint);
canvas.drawText(unreadText, redCenterX - unreadTextWidth / 2.0f, redCenterY + unreadTextHeight / 2.0f - unreadTextHeight / 5.0f, unreadPaint);
}


Expand Down Expand Up @@ -236,6 +245,15 @@ public void setUnreadTextSize(float sp) {
invalidate();
}

/**
* 设置最大未读数,超过此数则显示“...”,默认99
*/
public void setMaxUnreadCount(int maxUnreadCount){
this.maxUnreadCount=maxUnreadCount;
invalidate();
}


/**
* 设置标签未读个数
*
Expand Down Expand Up @@ -267,6 +285,8 @@ public void setUnreadBackgroundColor(int color) {
invalidate();
}



/**
* 设置未读数上边距
*
Expand Down
Binary file added screenshots/QQ截图20170703231924.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/QQ截图20170703232051.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c51d13b

Please sign in to comment.