Skip to content

Commit eb1acad

Browse files
committed
区分DrawableText 水平和垂直居中的方式
1 parent 2212d50 commit eb1acad

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

DrawableText.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# 还没开始写

Loadingbutton-support/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 17
99
targetSdkVersion 28
1010
versionCode 100
11-
versionName "1.0.0-alpha1"
11+
versionName "1.0.0-alpha4"
1212
}
1313

1414
buildTypes {

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* 2、设置Drawable的大小 √
2828
* 3、文字居中,图片靠边居中 √
2929
* 4、多次setCompoundDrawablesRelative,图片会发生偏移 √
30-
* 5、寻找一个合适的测量文字大小的时机,避免多次测量消耗性能
30+
* 5、寻找一个合适的测量文字大小的时机,避免多次测量
3131
* 6、在draw时,避免用取出旧的drawable的bounds绘制,需要预先取出并存储起来,还需要注意在存储bounds时是不是有平移过 √
3232
* 7、
3333
*/
@@ -51,7 +51,8 @@ public class DrawableTextView extends AppCompatTextView {
5151
private float mTextHeight;
5252

5353
private boolean firstLayout;
54-
private boolean isCenter; //Gravity是否是居中
54+
private boolean isCenterHorizontal; //Gravity是否水平居中
55+
private boolean isCenterVertical; //Gravity是否垂直居中
5556
private boolean enableCenterDrawables; //drawable跟随文本居中
5657
private boolean enableTextInCenter; //默认情况下文字与图片共同居中,开启后文字在最中间,图片紧挨
5758

@@ -108,8 +109,8 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
108109
super.onLayout(changed, left, top, right, bottom);
109110
if (enableCenterDrawables) {
110111
final int absoluteGravity = Gravity.getAbsoluteGravity(getGravity(), getLayoutDirection());
111-
isCenter = (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.CENTER_HORIZONTAL
112-
|| (absoluteGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.CENTER_VERTICAL;
112+
isCenterHorizontal = (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.CENTER_HORIZONTAL;
113+
isCenterVertical = (absoluteGravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.CENTER_VERTICAL;
113114
}
114115

115116
if (!firstLayout) {
@@ -129,7 +130,7 @@ protected void onFirstLayout(int left, int top, int right, int bottom) {
129130
@Override
130131
protected void onDraw(Canvas canvas) {
131132

132-
if (enableCenterDrawables && isCenter) {
133+
if (enableCenterDrawables && (isCenterHorizontal | isCenterVertical)) {
133134

134135
//画布的偏移量
135136
int transX = 0, transY = 0;
@@ -139,7 +140,9 @@ protected void onDraw(Canvas canvas) {
139140
int offset = (int) calcOffset(POSITION.START);
140141
mDrawables[POSITION.START].setBounds(bounds.left + offset, bounds.top,
141142
bounds.right + offset, bounds.bottom);
142-
transX -= (mDrawablesBounds[POSITION.START].width() + getCompoundDrawablePadding()) >> 1;
143+
144+
if (isCenterHorizontal)
145+
transX -= (mDrawablesBounds[POSITION.START].width() + getCompoundDrawablePadding()) >> 1;
143146
}
144147

145148
if (mDrawables[POSITION.TOP] != null) {
@@ -149,7 +152,8 @@ protected void onDraw(Canvas canvas) {
149152
mDrawables[POSITION.TOP].setBounds(bounds.left, bounds.top + offset,
150153
bounds.right, bounds.bottom + offset);
151154

152-
transY -= (mDrawablesBounds[POSITION.TOP].height() + getCompoundDrawablePadding()) >> 1;
155+
if (isCenterVertical)
156+
transY -= (mDrawablesBounds[POSITION.TOP].height() + getCompoundDrawablePadding()) >> 1;
153157
}
154158

155159
if (mDrawables[POSITION.END] != null) {
@@ -158,7 +162,8 @@ protected void onDraw(Canvas canvas) {
158162
mDrawables[POSITION.END].setBounds(bounds.left + offset, bounds.top,
159163
bounds.right + offset, bounds.bottom);
160164

161-
transX += (mDrawablesBounds[POSITION.END].width() + getCompoundDrawablePadding()) >> 1;
165+
if (isCenterHorizontal)
166+
transX += (mDrawablesBounds[POSITION.END].width() + getCompoundDrawablePadding()) >> 1;
162167
}
163168

164169
if (mDrawables[POSITION.BOTTOM] != null) {
@@ -167,7 +172,8 @@ protected void onDraw(Canvas canvas) {
167172
mDrawables[POSITION.BOTTOM].setBounds(bounds.left, bounds.top + offset,
168173
bounds.right, bounds.bottom + offset);
169174

170-
transY += (mDrawablesBounds[POSITION.BOTTOM].height() + getCompoundDrawablePadding()) >> 1;
175+
if (isCenterVertical)
176+
transY += (mDrawablesBounds[POSITION.BOTTOM].height() + getCompoundDrawablePadding()) >> 1;
171177
}
172178

173179
if (enableTextInCenter) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ private void init(Context context, AttributeSet attrs) {
9797

9898
//getConfig
9999
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LoadingButton);
100-
enableShrink = array.getBoolean(R.styleable.LoadingButton_enableShrink, false);
101-
disableClickOnLoading = array.getBoolean(R.styleable.LoadingButton_disableClickOnLoading, false);
100+
enableShrink = array.getBoolean(R.styleable.LoadingButton_enableShrink, true);
101+
disableClickOnLoading = array.getBoolean(R.styleable.LoadingButton_disableClickOnLoading, true);
102102
mShrinkDuration = array.getInt(R.styleable.LoadingButton_shrinkDuration, 450);
103103
int loadingDrawableSize = array.getDimensionPixelSize(R.styleable.LoadingButton_loadingEndDrawableSize, (int) (enableShrink ? getTextSize() * 2 : getTextSize()));
104104
int loadingDrawableColor = array.getColor(R.styleable.LoadingButton_loadingDrawableColor, getTextColors().getDefaultColor());

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](),加载动画来自于[CircularProgressDrawable](https://developer.android.google.cn/reference/android/support/v4/widget/CircularProgressDrawable?hl=en)
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)
44

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

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

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

3232

0 commit comments

Comments
 (0)