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 ) {
0 commit comments