Skip to content

Commit c0a4535

Browse files
committed
优化资源释放
1 parent 95b8744 commit c0a4535

File tree

4 files changed

+169
-19
lines changed

4 files changed

+169
-19
lines changed

.idea/codeStyles/Project.xml

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ protected void onDraw(Canvas canvas) {
190190
@Override
191191
public void onDrawForeground(Canvas canvas) {
192192
//再次平移回去
193-
canvas.translate(-canvasTransX,-canvasTransY);
193+
canvas.translate(-canvasTransX, -canvasTransY);
194194
super.onDrawForeground(canvas);
195195
}
196196

@@ -272,19 +272,27 @@ public void setText(CharSequence text, BufferType type) {
272272

273273
/**
274274
* 设置Drawable,并设置宽高
275+
* 默认大小为Drawable的{@link Drawable#getBounds()} ,
276+
* 如果Bounds宽高为0则,取Drawable的内部固定尺寸{@link Drawable#getIntrinsicHeight()}
275277
*
276278
* @param position {@link POSITION}
277279
* @param drawable Drawable
278-
* @param width DX
279-
* @param height DX
280+
* @param width Px
281+
* @param height Px
280282
*/
281283
public void setDrawable(@POSITION int position, @Nullable Drawable drawable, @Px int width, @Px int height) {
282284
mDrawables[position] = drawable;
283285
if (drawable != null) {
284286
Rect bounds = new Rect();
285287
if (width == -1 && height == -1) {
286-
bounds.right = drawable.getIntrinsicWidth();
287-
bounds.bottom = drawable.getIntrinsicHeight();
288+
if (drawable.getBounds().width() > 0 && drawable.getBounds().height() > 0) {
289+
//如果Bounds宽高大于0,则保持默认
290+
final Rect origin = drawable.getBounds();
291+
bounds.set(origin.left, origin.top, origin.right, origin.bottom);
292+
} else {
293+
//否则取Drawable的内部大小
294+
bounds.set(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
295+
}
288296
} else {
289297
bounds.right = width;
290298
bounds.bottom = height;
@@ -338,15 +346,19 @@ private void storeDrawables(@Nullable Drawable start, @Nullable Drawable top, @N
338346
protected Drawable[] copyDrawables(boolean clearOffset) {
339347
Drawable[] drawables = Arrays.copyOf(getDrawables(), 4);
340348
//clear offset
341-
if (clearOffset) {
342-
for (Drawable drawable : drawables) {
343-
if (drawable != null) {
344-
Rect bounds = drawable.getBounds();
345-
bounds.offset(-bounds.left, -bounds.top);
346-
}
349+
if (clearOffset)
350+
clearOffset(drawables);
351+
352+
return drawables;
353+
}
354+
355+
private void clearOffset(Drawable... drawables) {
356+
for (Drawable drawable : drawables) {
357+
if (drawable != null) {
358+
Rect bounds = drawable.getBounds();
359+
bounds.offset(-bounds.left, -bounds.top);
347360
}
348361
}
349-
return drawables;
350362
}
351363

352364
protected int dp2px(float dpValue) {
@@ -404,18 +416,28 @@ public DrawableTextView setDrawableBottom(Drawable drawableBottom) {
404416
return this;
405417
}
406418

407-
public void setEnableCenterDrawables(boolean enable) {
419+
public DrawableTextView setEnableCenterDrawables(boolean enable) {
420+
if (enableCenterDrawables) {
421+
//清除之前的位移
422+
clearOffset(mDrawables);
423+
}
408424
this.enableCenterDrawables = enable;
425+
return this;
409426
}
410427

411-
public void setEnableTextInCenter(boolean enable) {
428+
public DrawableTextView setEnableTextInCenter(boolean enable) {
412429
this.enableTextInCenter = enable;
430+
return this;
413431
}
414432

415433
public boolean isEnableTextInCenter() {
416434
return enableTextInCenter;
417435
}
418436

437+
public boolean isEnableCenterDrawables() {
438+
return enableCenterDrawables;
439+
}
440+
419441
public Drawable[] getDrawables() {
420442
return mDrawables;
421443
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public void onAnimationStart(Animator animation) {
171171
}
172172

173173
saveStatus();
174-
LoadingButton.super.setText("",BufferType.NORMAL);
174+
LoadingButton.super.setText("", BufferType.NORMAL);
175175
setCompoundDrawablePadding(0);
176176
setCompoundDrawablesRelative(mLoadingDrawable, null, null, null);
177177
setEnableTextInCenter(false);
@@ -614,7 +614,6 @@ public void setCompoundDrawablePadding(int pad) {
614614
}
615615

616616

617-
618617
@Override
619618
public void setText(CharSequence text, BufferType type) {
620619
if (enableShrink && isSizeChanging) {
@@ -930,6 +929,19 @@ private Bitmap getBitmap(Drawable drawable) {
930929
return null;
931930
}
932931

932+
933+
@Override
934+
protected void onDetachedFromWindow() {
935+
//release
936+
mShrinkAnimator.cancel();
937+
mLoadingDrawable.stop();
938+
if (mEndDrawable != null)
939+
mEndDrawable.mAppearAnimator.cancel();
940+
941+
super.onDetachedFromWindow();
942+
943+
}
944+
933945
public interface OnLoadingListener {
934946
void onLoadingStart();
935947

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# LoadingButton [![LoadingButton](https://jitpack.io/v/FlodCoding/LoadingButton.svg)](https://jitpack.io/#FlodCoding/LoadingButton)
22

3-
一个小巧灵活的带加载功能的按钮控件,继承自[DrawableTextView](https://github.com/FlodCoding/LoadingButton/blob/master/DrawableText.md),加载动画来自于[CircularProgressDrawable](https://developer.android.google.cn/reference/android/support/v4/widget/CircularProgressDrawable?hl=en)
3+
一个小巧灵活的带加载功能的按钮控件,继承自[DrawableTextView](https://github.com/FlodCoding/DrawableTextView),加载动画来自于[CircularProgressDrawable](https://developer.android.google.cn/reference/android/support/v4/widget/CircularProgressDrawable?hl=en)
44

55
## 特性
66
* 支持按钮收缩
@@ -23,10 +23,10 @@
2323

2424
dependencies {
2525
//Androidx
26-
implementation 'com.github.FlodCoding:LoadingButton:1.0.0-alpha6'
26+
implementation 'com.github.FlodCoding:LoadingButton:1.0.0'
2727

2828
//Support-appcompat
29-
implementation 'com.github.FlodCoding:LoadingButton:1.0.0-alpha4-support'
29+
implementation 'com.github.FlodCoding:LoadingButton:1.0.0-support'
3030
}
3131

3232

0 commit comments

Comments
 (0)