From 3fbc8326e63f4328a1bbbe40efc0461597410bc3 Mon Sep 17 00:00:00 2001 From: woxingxiao Date: Fri, 29 Dec 2017 15:06:11 +0800 Subject: [PATCH] Update drawable size dynamically when text was changed. --- vectorcompattextview/build.gradle | 6 +- .../com/xw/repo/VectorCompatTextView.java | 127 ++++++++++-------- 2 files changed, 71 insertions(+), 62 deletions(-) diff --git a/vectorcompattextview/build.gradle b/vectorcompattextview/build.gradle index 6ed6a21..12d33ad 100644 --- a/vectorcompattextview/build.gradle +++ b/vectorcompattextview/build.gradle @@ -2,13 +2,13 @@ apply plugin: 'com.android.library' android { compileSdkVersion 27 - buildToolsVersion "27.0.2" + buildToolsVersion "27.0.3" defaultConfig { minSdkVersion 9 targetSdkVersion 27 - versionCode 11 - versionName "2.3" + versionCode 12 + versionName "2.4" } buildTypes { diff --git a/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java b/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java index 9f8b6eb..46eb2f6 100644 --- a/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java +++ b/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java @@ -97,7 +97,7 @@ private void initAttrs(Context context, AttributeSet attrs) { } } - private void initDrawables(Drawable... drawables) { + private void initDrawables(final Drawable... drawables) { for (Drawable drawable : drawables) { tintDrawable(drawable); } @@ -121,7 +121,18 @@ private void initDrawables(Drawable... drawables) { setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]); } } else { - adjustDrawables(drawables[0], drawables[1], drawables[2], drawables[3]); + getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { + @Override + public void onGlobalLayout() { + if (Build.VERSION.SDK_INT < 16) { + getViewTreeObserver().removeGlobalOnLayoutListener(this); + } else { + getViewTreeObserver().removeOnGlobalLayoutListener(this); + } + + adjustDrawables(drawables[0], drawables[1], drawables[2], drawables[3]); + } + }); } } else if (mDrawableWidth > 0 || mDrawableHeight > 0) { resizeDrawables(drawables[0], drawables[1], drawables[2], drawables[3]); @@ -159,70 +170,68 @@ private void resizeDrawables(Drawable... drawables) { setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]); } - private void adjustDrawables(final Drawable dl, final Drawable dt, final Drawable dr, final Drawable db) { - getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { - @Override - public void onGlobalLayout() { - if (Build.VERSION.SDK_INT < 16) { - getViewTreeObserver().removeGlobalOnLayoutListener(this); - } else { - getViewTreeObserver().removeOnGlobalLayoutListener(this); - } + @Override + public void setText(CharSequence text, BufferType type) { + super.setText(text, type); - int width = 0; - int height = 0; + if (isDrawableAdjustTextWidth || isDrawableAdjustTextHeight) { + Drawable[] drawables = getCompoundDrawables(); + if (drawables[0] == null && drawables[1] == null && drawables[2] == null && drawables[3] == null) + return; - if (isDrawableAdjustTextWidth) { - Paint paint = new Paint(); - paint.setTextSize(getTextSize()); - CharSequence text = getText(); - Rect rect = new Rect(); - paint.getTextBounds(text.toString(), 0, text.length(), rect); + adjustDrawables(drawables[0], drawables[1], drawables[2], drawables[3]); + } + } - width = rect.width(); - } else if (isDrawableAdjustViewWidth) { - width = getMeasuredWidth(); - } - if (isDrawableAdjustTextHeight) { - Paint paint = new Paint(); - paint.setTextSize(getTextSize()); - CharSequence text = getText(); - Rect rect = new Rect(); - paint.getTextBounds(text.toString(), 0, text.length(), rect); - - height = rect.height(); - } else if (isDrawableAdjustViewHeight) { - height = getMeasuredHeight(); - } + private void adjustDrawables(Drawable dl, Drawable dt, Drawable dr, Drawable db) { + int width = 0; + int height = 0; - int h = mDrawableHeight; - int w = mDrawableWidth; + if (isDrawableAdjustTextWidth) { + Paint paint = new Paint(); + paint.setTextSize(getTextSize()); + CharSequence text = getText(); + Rect rect = new Rect(); + paint.getTextBounds(text.toString(), 0, text.length(), rect); - if (dt != null) { - if (h == 0) - h = width * dt.getIntrinsicHeight() / dt.getIntrinsicWidth(); - dt.setBounds(0, 0, width, h); - } - if (db != null) { - if (h == 0) - h = width * db.getIntrinsicHeight() / db.getIntrinsicWidth(); - db.setBounds(0, 0, width, h); - } + width = rect.width(); + } else if (isDrawableAdjustViewWidth) { + width = getMeasuredWidth(); + } + if (isDrawableAdjustTextHeight) { + Paint paint = new Paint(); + paint.setTextSize(getTextSize()); + CharSequence text = getText(); + Rect rect = new Rect(); + paint.getTextBounds(text.toString(), 0, text.length(), rect); + + height = rect.height(); + } else if (isDrawableAdjustViewHeight) { + height = getMeasuredHeight(); + } - if (dl != null) { - if (w == 0) - w = height * dl.getIntrinsicWidth() / dl.getIntrinsicHeight(); - dl.setBounds(0, 0, w, height); - } - if (dr != null) { - if (w == 0) - w = height * dr.getIntrinsicWidth() / dr.getIntrinsicHeight(); - dr.setBounds(0, 0, w, height); - } + int h = mDrawableHeight; + int w = mDrawableWidth; - setCompoundDrawables(dl, dt, dr, db); - } - }); + if (dt != null) { + if (h == 0) h = width * dt.getIntrinsicHeight() / dt.getIntrinsicWidth(); + dt.setBounds(0, 0, width, h); + } + if (db != null) { + if (h == 0) h = width * db.getIntrinsicHeight() / db.getIntrinsicWidth(); + db.setBounds(0, 0, width, h); + } + + if (dl != null) { + if (w == 0) w = height * dl.getIntrinsicWidth() / dl.getIntrinsicHeight(); + dl.setBounds(0, 0, w, height); + } + if (dr != null) { + if (w == 0) w = height * dr.getIntrinsicWidth() / dr.getIntrinsicHeight(); + dr.setBounds(0, 0, w, height); + } + + setCompoundDrawables(dl, dt, dr, db); } @Override