@@ -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 }
0 commit comments