Skip to content

Commit 02b4ec6

Browse files
committed
Top and bottom loading err fixed
1 parent b99b62c commit 02b4ec6

File tree

4 files changed

+126
-80
lines changed

4 files changed

+126
-80
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,11 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
113113
onFirstLayout(left, top, right, bottom);
114114
firstLayout = true;
115115
}
116-
117116
}
118117

119118
protected void onFirstLayout(int left, int top, int right, int bottom) {
120-
mTextWidth = measureTextWidth();
121-
mTextHeight = measureTextHeight();
119+
measureTextWidth();
120+
measureTextHeight();
122121
}
123122

124123
/**
@@ -214,19 +213,22 @@ protected int getCanvasTransY() {
214213
* 测量文字的宽度,通过Paint测量所有文字的长度,
215214
* 但是这个数据不一定准确,文本还有可能换行,还需要通过{@link #getLineBounds}来获取文本的最大宽度
216215
*/
217-
protected float measureTextWidth() {
216+
protected void measureTextWidth() {
218217
final Rect textBounds = new Rect();
219218
getLineBounds(0, textBounds);
220219
final float width = getPaint().measureText(getText().toString());
221220
final float maxWidth = textBounds.width();
222-
return width <= maxWidth ? width : maxWidth;
221+
mTextWidth = width <= maxWidth || maxWidth == 0 ? width : maxWidth;
223222
}
224223

225224
/**
226225
* 获取文本的高度,通过{@link #getLineHeight}乘文本的行数
227226
*/
228-
protected float measureTextHeight() {
229-
return getLineHeight() * getLineCount();
227+
protected void measureTextHeight() {
228+
if (getText().length() > 0)
229+
mTextHeight = getLineHeight() * getLineCount();
230+
else
231+
mTextHeight = 0;
230232
}
231233

232234
protected float getTextWidth() {
@@ -240,8 +242,15 @@ public float getTextHeight() {
240242
@Override
241243
public void setText(CharSequence text, BufferType type) {
242244
super.setText(text, type);
243-
mTextWidth = measureTextWidth();
244-
mTextHeight = measureTextHeight();
245+
measureTextWidth();
246+
measureTextHeight();
247+
248+
/*Log.d("mTextHeight", "Text: " + getText());
249+
Log.d("mTextHeight", "getWidth: " + getWidth());
250+
Log.d("mTextHeight", "getHeight: " + getHeight());
251+
Log.d("mTextHeight", "mTextHeight: " + mTextHeight);
252+
Log.d("mTextHeight", "getLineCount: " + getLineCount());
253+
Log.d("mTextHeight", "getLineHeight: " + getLineHeight());*/
245254
}
246255

247256
/**

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

Lines changed: 75 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.graphics.Rect;
1414
import android.graphics.drawable.Drawable;
1515
import android.util.AttributeSet;
16+
import android.view.View;
1617

1718
import androidx.annotation.DrawableRes;
1819
import androidx.annotation.NonNull;
@@ -32,7 +33,7 @@
3233
* 先执行 beginShrinkAnim(true) 后执行 beginShrinkAnim(false);
3334
* <p>
3435
* 8、多次start和end 会出错
35-
* 9、设置完Drawable大小后,start后再次设置rootView大小失控
36+
* 9、设置完Drawable大小后,start后再次设置rootView大小失控,是因为原来是wrap_content
3637
* 10、start和compete同时按Loading没有关
3738
*/
3839
@SuppressWarnings({"UnusedReturnValue,SameParameterValue", "unused"})
@@ -65,7 +66,7 @@ interface STATE {
6566
private int mLoadingPosition;
6667

6768

68-
private boolean isShrinkAnimReverse;
69+
private boolean nextShrinkReverse;
6970
private boolean isCancel;
7071
private boolean isFail;
7172

@@ -108,7 +109,7 @@ private void init(Context context, AttributeSet attrs) {
108109

109110
mLoadingSize = loadingDrawableSize;
110111
mLoadingPosition = loadingDrawablePosition;
111-
setDrawable(loadingDrawablePosition, mLoadingDrawable, loadingDrawableSize, loadingDrawableSize);
112+
setDrawable(mLoadingPosition, mLoadingDrawable, loadingDrawableSize, loadingDrawableSize);
112113

113114
//initLoadingDrawable
114115
if (endCompleteDrawableResId != -1 || endFailDrawableResId != -1) {
@@ -119,11 +120,10 @@ private void init(Context context, AttributeSet attrs) {
119120

120121
//initShrinkAnimator
121122
setUpShrinkAnimator();
122-
//TODO
123-
setEnableTextInCenter(true);
124-
/* if (mLoadingPosition % 2 == 0 || enableShrink)
125-
setEnableTextInCenter(true);
126-
*/
123+
124+
//Start|End -> true Top|Bottom ->false
125+
setEnableTextInCenter(mLoadingPosition % 2 == 0);
126+
127127
if (getRootView().isInEditMode()) {
128128
mLoadingDrawable.setStartEndTrim(0, 0.8f);
129129
}
@@ -150,7 +150,7 @@ public void onAnimationUpdate(ValueAnimator animation) {
150150
//onAnimationStart(Animator animation, boolean isReverse) 在7.0测试没有调用fuck
151151
@Override
152152
public void onAnimationStart(Animator animation) {
153-
if (!isShrinkAnimReverse) {
153+
if (!nextShrinkReverse) {
154154
//开始收缩
155155
curStatus = STATE.SHRINKING;
156156
if (mOnLoadingListener != null) {
@@ -176,21 +176,19 @@ public void onAnimationStart(Animator animation) {
176176

177177
@Override
178178
public void onAnimationEnd(Animator animation) {
179-
if (!isShrinkAnimReverse) {
179+
if (!nextShrinkReverse) {
180180
//收缩结束
181181
curStatus = STATE.LOADING;
182-
if (mOnLoadingListener != null) {
183-
mOnLoadingListener.onLoadingStart();
184-
}
185182
startLoading();
183+
nextShrinkReverse = true;
186184

187185
} else {
188186
//恢复结束
189187
curStatus = STATE.IDE;
190-
endCallbackListener();
191188
restoreStatus();
189+
endCallbackListener();
190+
nextShrinkReverse = false;
192191
}
193-
isShrinkAnimReverse = !isShrinkAnimReverse;
194192
}
195193

196194
});
@@ -233,10 +231,19 @@ private void restoreStatus() {
233231
setCompoundDrawablePadding(mDrawablePaddingSaved);
234232
setCompoundDrawablesRelative(mDrawablesSaved[POSITION.START], mDrawablesSaved[POSITION.TOP], mDrawablesSaved[POSITION.END], mDrawablesSaved[POSITION.BOTTOM]);
235233
setEnableTextInCenter(mEnableTextInCenterSaved);
236-
237234
getLayoutParams().width = mRootViewSizeSaved[0];
238235
getLayoutParams().height = mRootViewSizeSaved[1];
239236
requestLayout();
237+
238+
addOnLayoutChangeListener(new OnLayoutChangeListener() {
239+
@Override
240+
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
241+
measureTextHeight();
242+
measureTextWidth();
243+
removeOnLayoutChangeListener(this);
244+
}
245+
});
246+
240247
}
241248

242249

@@ -246,7 +253,7 @@ private void beginShrinkAnim(boolean isReverse, boolean lastFrame) {
246253
//如果上一个动画还在执行,就结束到最后一帧
247254
mShrinkAnimator.end();
248255
}
249-
isShrinkAnimReverse = isReverse;
256+
nextShrinkReverse = isReverse;
250257
if (!isReverse) {
251258
mShrinkAnimator.start();
252259
} else {
@@ -287,6 +294,7 @@ private void cancelAllRunning(boolean withAnim) {
287294
} else {
288295
stopLoading();
289296
endCallbackListener();
297+
restoreStatus();
290298
curStatus = STATE.IDE;
291299
}
292300
break;
@@ -301,8 +309,7 @@ private void cancelAllRunning(boolean withAnim) {
301309
if (!withAnim)
302310
mShrinkAnimator.end();
303311
else {
304-
endCallbackListener();
305-
curStatus = STATE.IDE;
312+
beginShrinkAnim(true, true);
306313
}
307314
break;
308315
}
@@ -325,17 +332,16 @@ public void start() {
325332
if (enableShrink) {
326333
beginShrinkAnim(false, false);
327334
} else {
335+
saveStatus();
328336
startLoading();
329337
}
330338
}
331339

332340
public void complete() {
333341
if (mEndDrawable != null) {
334-
if (curStatus == STATE.LOADING)
335-
mEndDrawable.show(true, true);
336-
else
337-
mEndDrawable.show(true, false);
342+
mEndDrawable.show(true);
338343
} else {
344+
//TODO 测试
339345
if (curStatus == STATE.LOADING)
340346
beginShrinkAnim(true, false);
341347
else
@@ -346,7 +352,7 @@ public void complete() {
346352
public void fail() {
347353
isFail = true;
348354
if (mEndDrawable != null) {
349-
mEndDrawable.show(false, true);
355+
mEndDrawable.show(false);
350356
} else {
351357
beginShrinkAnim(true, false);
352358
}
@@ -398,12 +404,14 @@ public LoadingButton setLoadingColor(@NonNull int... colors) {
398404
return this;
399405
}
400406

407+
401408
public void setLoadingPosition(@POSITION int position) {
402-
if (!enableShrink) {
403-
setDrawable(mLoadingPosition, null, 0, 0);
404-
mLoadingPosition = position;
405-
setDrawable(position, getLoadingDrawable(), getLoadingDrawableSize(), getLoadingDrawableSize());
406-
}
409+
boolean enableTextInCenter = position % 2 == 0;
410+
setEnableTextInCenter(enableTextInCenter);
411+
mEnableTextInCenterSaved = enableTextInCenter;
412+
setDrawable(mLoadingPosition, null, 0, 0);
413+
mLoadingPosition = position;
414+
setDrawable(position, getLoadingDrawable(), getLoadingDrawableSize(), getLoadingDrawableSize());
407415
}
408416

409417
public LoadingButton setLoadingEndDrawableSize(@Px int size) {
@@ -462,7 +470,7 @@ public LoadingButton setEndDrawableDuration(long milliseconds) {
462470
public long getEndDrawableDuration() {
463471
if (mEndDrawable != null)
464472
return mEndDrawable.duration;
465-
return 0;
473+
return EndDrawable.DEFAULT_APPEAR_DURATION;
466474
}
467475

468476
public LoadingButton setEndDrawableAppearTime(long milliseconds) {
@@ -487,8 +495,10 @@ public void setCompoundDrawablePadding(int pad) {
487495
mDrawablePaddingSaved = pad;
488496
}
489497

498+
490499
@Override
491500
public void setText(CharSequence text, BufferType type) {
501+
//TODO 测试
492502
if (enableShrink && (curStatus != STATE.IDE)) {
493503
text = "";
494504
}
@@ -538,7 +548,6 @@ public class EndDrawable {
538548
private float animValue;
539549
int[] offsetTemp = new int[]{0, 0};
540550
private boolean isShowing;
541-
private boolean shrink;
542551
private Runnable mRunnable;
543552

544553
private EndDrawable(@Nullable Drawable completeDrawable, @Nullable Drawable failDrawable) {
@@ -562,11 +571,12 @@ private void init() {
562571
@Override
563572
public void run() {
564573
setAnimValue(0);
565-
if (shrink) {
566-
beginShrinkAnim(true, false);
567-
} else {
568-
endCallbackListener();
574+
if (enableShrink)
575+
beginShrinkAnim(true, !nextShrinkReverse);
576+
else {
569577
curStatus = STATE.IDE;
578+
restoreStatus();
579+
endCallbackListener();
570580
}
571581
isShowing = false;
572582
}
@@ -594,7 +604,12 @@ public void onAnimationEnd(Animator animation) {
594604
}
595605

596606

597-
private void show(boolean isSuccess, boolean shrink) {
607+
/**
608+
* 显示EndDrawable
609+
*
610+
* @param isSuccess true:CompleteDrawable fail:FailDrawable
611+
*/
612+
private void show(boolean isSuccess) {
598613
//end running Shrinking
599614
if (mShrinkAnimator.isRunning()) {
600615
mShrinkAnimator.end();
@@ -612,17 +627,21 @@ private void show(boolean isSuccess, boolean shrink) {
612627
}
613628
mAppearAnimator.start();
614629
isShowing = true;
615-
this.shrink = shrink;
616630
}
617631

618632
private void cancel(boolean withAnim) {
619633
isShowing = false;
620634
if (mAppearAnimator.isRunning()) {
621-
//出现中
622635
mAppearAnimator.end();
623636
}
624637
getHandler().removeCallbacks(mRunnable);
625-
beginShrinkAnim(true, !withAnim);
638+
if (enableShrink)
639+
beginShrinkAnim(true, !withAnim);
640+
else {
641+
endCallbackListener();
642+
restoreStatus();
643+
curStatus = STATE.IDE;
644+
}
626645
setAnimValue(0);
627646
}
628647

@@ -639,53 +658,46 @@ private int[] calcOffset(Canvas canvas, Rect bounds, @POSITION int pos) {
639658
final int[] offset = offsetTemp;
640659
offset[0] = canvas.getWidth() / 2;
641660
offset[1] = canvas.getHeight() / 2;
661+
642662
switch (pos) {
643663
case POSITION.START: {
644664
offset[0] -= (int) getTextWidth() / 2 + bounds.width() + getCompoundDrawablePadding();
645-
if (enableShrink && isShrinkAnimReverse) {
665+
if (enableShrink && nextShrinkReverse) {
646666
offset[0] += bounds.width() / 2;
667+
} else if (!isEnableTextInCenter()) {
668+
offset[0] += (bounds.width() + getCompoundDrawablePadding()) / 2;
647669
}
670+
648671
offset[1] -= bounds.height() / 2;
649672
break;
650673
}
651674
case POSITION.TOP: {
652-
//TODO Test
653-
654675
offset[0] -= bounds.width() / 2;
655-
656-
if (enableShrink) {
657-
offset[1] -= bounds.height() / 2;
658-
} else {
659-
offset[1] -= (getTextHeight() + bounds.height() + getCompoundDrawablePadding()) / 2;
660-
if (isEnableTextInCenter()) {
661-
offset[1] -= getCompoundDrawablePadding() / 2;
662-
}
663-
}
664-
665-
/* offset[1] -= (int) getTextHeight() / 2 + bounds.height() + getCompoundDrawablePadding();
666-
if (enableShrink) {
667-
if (isShrinkAnimReverse)
668-
offset[1] += bounds.height() / 2;
676+
offset[1] -= (int) getTextHeight() / 2 + bounds.height() + getCompoundDrawablePadding();
677+
if (enableShrink && nextShrinkReverse) {
678+
offset[1] += bounds.height() / 2;
679+
} else if (!isEnableTextInCenter()) {
680+
offset[1] += (bounds.height() + getCompoundDrawablePadding()) / 2;
669681
}
670-
671-
if (!enableShrink && !isEnableTextInCenter()) {
672-
offset[1] += (bounds.height() + getCompoundDrawablePadding()) >> 1;
673-
}*/
674682
break;
675683
}
676684
case POSITION.END: {
677685
offset[0] += (int) getTextWidth() / 2 + getCompoundDrawablePadding();
678-
if (enableShrink && isShrinkAnimReverse) {
686+
if (enableShrink && nextShrinkReverse) {
679687
offset[0] -= bounds.width() / 2;
688+
} else if (!isEnableTextInCenter()) {
689+
offset[0] -= (bounds.width() + getCompoundDrawablePadding()) / 2;
680690
}
681691
offset[1] -= bounds.height() / 2;
682692
break;
683693
}
684694
case POSITION.BOTTOM: {
685695
offset[0] -= bounds.width() / 2;
686696
offset[1] += (int) getTextHeight() / 2 + getCompoundDrawablePadding();
687-
if (enableShrink && isShrinkAnimReverse) {
688-
offset[1] += bounds.height() / 2;
697+
if (enableShrink && nextShrinkReverse) {
698+
offset[1] -= bounds.height() / 2;
699+
} else if (!isEnableTextInCenter()) {
700+
offset[1] -= (bounds.height() + getCompoundDrawablePadding()) / 2;
689701
}
690702
break;
691703
}

0 commit comments

Comments
 (0)