Skip to content

Commit

Permalink
Update drawable size dynamically when text was changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
woxingxiao committed Dec 29, 2017
1 parent 7b402bd commit 3fbc832
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 62 deletions.
6 changes: 3 additions & 3 deletions vectorcompattextview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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]);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3fbc832

Please sign in to comment.