Skip to content

Commit 3991e66

Browse files
committed
1、修复foreground受平移影响
2、修复只有hint时文字大小没有测量的问题
1 parent 0f6a7b6 commit 3991e66

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

Loadingbutton/src/main/java/com/flod/loadingbutton/DrawableTextView.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
* 4、多次setCompoundDrawablesRelative,图片会发生偏移 √
3030
* 5、寻找一个合适的测量文字大小的时机,避免多次测量消耗性能 √
3131
* 6、在draw时,避免用取出旧的drawable的bounds绘制,需要预先取出并存储起来,还需要注意在存储bounds时是不是有平移过 √
32-
* 7、
32+
* 7、foreground会受平移影响 √
33+
* 8、如果是只有hint没有Text需要也需要测量出文字大小√
3334
*/
3435
@SuppressWarnings({"UnusedReturnValue", "unused", "SameParameterValue"})
3536
public class DrawableTextView extends AppCompatTextView {
@@ -118,6 +119,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
118119
}
119120
}
120121

122+
121123
protected void onFirstLayout(int left, int top, int right, int bottom) {
122124
measureTextWidth();
123125
measureTextHeight();
@@ -176,10 +178,16 @@ protected void onDraw(Canvas canvas) {
176178
this.canvasTransY = transY;
177179
}
178180
}
179-
180181
super.onDraw(canvas);
181182
}
182183

184+
@Override
185+
public void onDrawForeground(Canvas canvas) {
186+
//再次平移回去
187+
canvas.translate(-canvasTransX,-canvasTransY);
188+
super.onDrawForeground(canvas);
189+
}
190+
183191
/**
184192
* 计算drawable居中还需距离
185193
* 如果左右两边都有图片,左图片居中则需要加上右侧图片占用的空间{@link #getCompoundPaddingEnd()},其他同理
@@ -219,7 +227,13 @@ protected int getCanvasTransY() {
219227
protected void measureTextWidth() {
220228
final Rect textBounds = new Rect();
221229
getLineBounds(0, textBounds);
222-
final float width = getPaint().measureText(getText().toString());
230+
String text = "";
231+
if (getText() != null && getText().length() > 0) {
232+
text = getText().toString();
233+
} else if (getHint() != null && getHint().length() > 0) {
234+
text = getHint().toString();
235+
}
236+
final float width = getPaint().measureText(text);
223237
final float maxWidth = textBounds.width();
224238
mTextWidth = width <= maxWidth || maxWidth == 0 ? width : maxWidth;
225239
}
@@ -228,7 +242,8 @@ protected void measureTextWidth() {
228242
* 获取文本的高度,通过{@link #getLineHeight}乘文本的行数
229243
*/
230244
protected void measureTextHeight() {
231-
if (getText().length() > 0)
245+
if ((getText() != null && getText().length() > 0)
246+
|| (getHint() != null && getHint().length() > 0))
232247
mTextHeight = getLineHeight() * getLineCount();
233248
else
234249
mTextHeight = 0;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:color="#9e9e9e">
4+
<item android:id="@android:id/mask"
5+
android:drawable="@android:color/white"/>
6+
</ripple>

0 commit comments

Comments
 (0)