27
27
* 2、设置Drawable的大小 √
28
28
* 3、文字居中,图片靠边居中 √
29
29
* 4、多次setCompoundDrawablesRelative,图片会发生偏移 √
30
- * 5、寻找一个合适的测量文字大小的时机,避免多次测量消耗性能 √
30
+ * 5、寻找一个合适的测量文字大小的时机,避免多次测量 √
31
31
* 6、在draw时,避免用取出旧的drawable的bounds绘制,需要预先取出并存储起来,还需要注意在存储bounds时是不是有平移过 √
32
32
* 7、
33
33
*/
@@ -51,7 +51,8 @@ public class DrawableTextView extends AppCompatTextView {
51
51
private float mTextHeight ;
52
52
53
53
private boolean firstLayout ;
54
- private boolean isCenter ; //Gravity是否是居中
54
+ private boolean isCenterHorizontal ; //Gravity是否水平居中
55
+ private boolean isCenterVertical ; //Gravity是否垂直居中
55
56
private boolean enableCenterDrawables ; //drawable跟随文本居中
56
57
private boolean enableTextInCenter ; //默认情况下文字与图片共同居中,开启后文字在最中间,图片紧挨
57
58
@@ -108,8 +109,8 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
108
109
super .onLayout (changed , left , top , right , bottom );
109
110
if (enableCenterDrawables ) {
110
111
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 ;
113
114
}
114
115
115
116
if (!firstLayout ) {
@@ -129,7 +130,7 @@ protected void onFirstLayout(int left, int top, int right, int bottom) {
129
130
@ Override
130
131
protected void onDraw (Canvas canvas ) {
131
132
132
- if (enableCenterDrawables && isCenter ) {
133
+ if (enableCenterDrawables && ( isCenterHorizontal | isCenterVertical ) ) {
133
134
134
135
//画布的偏移量
135
136
int transX = 0 , transY = 0 ;
@@ -139,7 +140,9 @@ protected void onDraw(Canvas canvas) {
139
140
int offset = (int ) calcOffset (POSITION .START );
140
141
mDrawables [POSITION .START ].setBounds (bounds .left + offset , bounds .top ,
141
142
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 ;
143
146
}
144
147
145
148
if (mDrawables [POSITION .TOP ] != null ) {
@@ -149,7 +152,8 @@ protected void onDraw(Canvas canvas) {
149
152
mDrawables [POSITION .TOP ].setBounds (bounds .left , bounds .top + offset ,
150
153
bounds .right , bounds .bottom + offset );
151
154
152
- transY -= (mDrawablesBounds [POSITION .TOP ].height () + getCompoundDrawablePadding ()) >> 1 ;
155
+ if (isCenterVertical )
156
+ transY -= (mDrawablesBounds [POSITION .TOP ].height () + getCompoundDrawablePadding ()) >> 1 ;
153
157
}
154
158
155
159
if (mDrawables [POSITION .END ] != null ) {
@@ -158,7 +162,8 @@ protected void onDraw(Canvas canvas) {
158
162
mDrawables [POSITION .END ].setBounds (bounds .left + offset , bounds .top ,
159
163
bounds .right + offset , bounds .bottom );
160
164
161
- transX += (mDrawablesBounds [POSITION .END ].width () + getCompoundDrawablePadding ()) >> 1 ;
165
+ if (isCenterHorizontal )
166
+ transX += (mDrawablesBounds [POSITION .END ].width () + getCompoundDrawablePadding ()) >> 1 ;
162
167
}
163
168
164
169
if (mDrawables [POSITION .BOTTOM ] != null ) {
@@ -167,7 +172,8 @@ protected void onDraw(Canvas canvas) {
167
172
mDrawables [POSITION .BOTTOM ].setBounds (bounds .left , bounds .top + offset ,
168
173
bounds .right , bounds .bottom + offset );
169
174
170
- transY += (mDrawablesBounds [POSITION .BOTTOM ].height () + getCompoundDrawablePadding ()) >> 1 ;
175
+ if (isCenterVertical )
176
+ transY += (mDrawablesBounds [POSITION .BOTTOM ].height () + getCompoundDrawablePadding ()) >> 1 ;
171
177
}
172
178
173
179
if (enableTextInCenter ) {
0 commit comments