Skip to content

Commit b27f4af

Browse files
committed
修复位移计算错误
1 parent d22c0b6 commit b27f4af

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
dependencies {
2727
//需要是Androidx
28-
implementation 'com.github.FlodCoding:DrawableTextView:1.0.0'
28+
implementation 'com.github.FlodCoding:DrawableTextView:1.0.1'
2929

3030
}
3131

213 KB
Binary file not shown.

drawabletextview/src/main/java/com/flod/drawabletextview/DrawableTextView.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.graphics.Canvas;
66
import android.graphics.Rect;
77
import android.graphics.drawable.Drawable;
8+
import android.text.TextUtils;
89
import android.util.AttributeSet;
910
import android.view.Gravity;
1011

@@ -126,16 +127,19 @@ protected void onDraw(Canvas canvas) {
126127

127128
if (enableCenterDrawables && (isCenterHorizontal | isCenterVertical)) {
128129

130+
//有文字就才位移画布了
131+
boolean textNoEmpty = !TextUtils.isEmpty(getText());
129132
//画布的偏移量
130133
int transX = 0, transY = 0;
131134

135+
132136
if (mDrawables[POSITION.START] != null) {
133137
Rect bounds = mDrawablesBounds[POSITION.START];
134138
int offset = (int) calcOffset(POSITION.START);
135139
mDrawables[POSITION.START].setBounds(bounds.left + offset, bounds.top,
136140
bounds.right + offset, bounds.bottom);
137141

138-
if (isCenterHorizontal)
142+
if (isCenterHorizontal && textNoEmpty)
139143
transX -= (mDrawablesBounds[POSITION.START].width() + getCompoundDrawablePadding()) >> 1;
140144
}
141145

@@ -146,7 +150,7 @@ protected void onDraw(Canvas canvas) {
146150
mDrawables[POSITION.TOP].setBounds(bounds.left, bounds.top + offset,
147151
bounds.right, bounds.bottom + offset);
148152

149-
if (isCenterVertical)
153+
if (isCenterVertical && textNoEmpty)
150154
transY -= (mDrawablesBounds[POSITION.TOP].height() + getCompoundDrawablePadding()) >> 1;
151155
}
152156

@@ -156,7 +160,7 @@ protected void onDraw(Canvas canvas) {
156160
mDrawables[POSITION.END].setBounds(bounds.left + offset, bounds.top,
157161
bounds.right + offset, bounds.bottom);
158162

159-
if (isCenterHorizontal)
163+
if (isCenterHorizontal && textNoEmpty)
160164
transX += (mDrawablesBounds[POSITION.END].width() + getCompoundDrawablePadding()) >> 1;
161165
}
162166

@@ -166,11 +170,12 @@ protected void onDraw(Canvas canvas) {
166170
mDrawables[POSITION.BOTTOM].setBounds(bounds.left, bounds.top + offset,
167171
bounds.right, bounds.bottom + offset);
168172

169-
if (isCenterVertical)
173+
if (isCenterVertical && textNoEmpty)
170174
transY += (mDrawablesBounds[POSITION.BOTTOM].height() + getCompoundDrawablePadding()) >> 1;
171175
}
172176

173-
if (enableTextInCenter) {
177+
178+
if (enableTextInCenter && textNoEmpty) {
174179
canvas.translate(transX, transY);
175180
this.canvasTransX = transX;
176181
this.canvasTransY = transY;

drawabletextview/src/main/res/values/strings.xml

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)