@@ -264,19 +264,27 @@ public void setText(CharSequence text, BufferType type) {
264264
265265 /**
266266 * 设置Drawable,并设置宽高
267+ * 默认大小为Drawable的{@link Drawable#getBounds()} ,
268+ * 如果Bounds宽高为0则,取Drawable的内部固定尺寸{@link Drawable#getIntrinsicHeight()}
267269 *
268270 * @param position {@link POSITION}
269271 * @param drawable Drawable
270- * @param width DX
271- * @param height DX
272+ * @param width Px
273+ * @param height Px
272274 */
273275 public void setDrawable (@ POSITION int position , @ Nullable Drawable drawable , @ Px int width , @ Px int height ) {
274276 mDrawables [position ] = drawable ;
275277 if (drawable != null ) {
276278 Rect bounds = new Rect ();
277279 if (width == -1 && height == -1 ) {
278- bounds .right = drawable .getIntrinsicWidth ();
279- bounds .bottom = drawable .getIntrinsicHeight ();
280+ if (drawable .getBounds ().width () > 0 && drawable .getBounds ().height () > 0 ) {
281+ //如果Bounds宽高大于0,则保持默认
282+ final Rect origin = drawable .getBounds ();
283+ bounds .set (origin .left , origin .top , origin .right , origin .bottom );
284+ } else {
285+ //否则取Drawable的内部大小
286+ bounds .set (0 , 0 , drawable .getIntrinsicWidth (), drawable .getIntrinsicHeight ());
287+ }
280288 } else {
281289 bounds .right = width ;
282290 bounds .bottom = height ;
@@ -326,28 +334,23 @@ private void storeDrawables(@Nullable Drawable start, @Nullable Drawable top, @N
326334
327335 }
328336
329- private void clearDrawableBounds () {
330- for (Drawable drawable : mDrawables ) {
331- if (drawable != null ) {
332- final Rect bounds = drawable .getBounds ();
333- bounds .set (0 , 0 , bounds .width (), bounds .height ());
334- }
335- }
336- }
337-
338337
339338 protected Drawable [] copyDrawables (boolean clearOffset ) {
340339 Drawable [] drawables = Arrays .copyOf (getDrawables (), 4 );
341340 //clear offset
342- if (clearOffset ) {
343- for (Drawable drawable : drawables ) {
344- if (drawable != null ) {
345- Rect bounds = drawable .getBounds ();
346- bounds .offset (-bounds .left , -bounds .top );
347- }
341+ if (clearOffset )
342+ clearOffset (drawables );
343+
344+ return drawables ;
345+ }
346+
347+ private void clearOffset (Drawable ... drawables ) {
348+ for (Drawable drawable : drawables ) {
349+ if (drawable != null ) {
350+ Rect bounds = drawable .getBounds ();
351+ bounds .offset (-bounds .left , -bounds .top );
348352 }
349353 }
350- return drawables ;
351354 }
352355
353356 protected int dp2px (float dpValue ) {
@@ -408,7 +411,7 @@ public DrawableTextView setDrawableBottom(Drawable drawableBottom) {
408411 public DrawableTextView setEnableCenterDrawables (boolean enable ) {
409412 if (enableCenterDrawables ) {
410413 //清除之前的位移
411- clearDrawableBounds ( );
414+ clearOffset ( mDrawables );
412415 }
413416 this .enableCenterDrawables = enable ;
414417 return this ;
0 commit comments