Skip to content

Commit

Permalink
New features: Adjust width or height of text bounds or view bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
woxingxiao committed Dec 23, 2017
1 parent 940c6e4 commit 50a0ff0
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 46 deletions.
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@
android:gravity="center"
android:text="SELECTED TAB"
android:textColor="@drawable/selector_text_color_tab"
app:drawableAdjustTextWidth="true"
app:drawableAdjustViewWidth="true"
app:drawableBottomCompat="@drawable/selector_drawable_tab"
app:drawableHeight="2dp"/>

Expand Down
8 changes: 4 additions & 4 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.1"
buildToolsVersion "27.0.2"

defaultConfig {
minSdkVersion 9
targetSdkVersion 27
versionCode 10
versionName "2.2"
versionCode 11
versionName "2.3"

}
buildTypes {
Expand All @@ -20,5 +20,5 @@ android {
}

dependencies {
provided 'com.android.support:appcompat-v7:27.0.0'
provided 'com.android.support:appcompat-v7:27.0.2'
}
10 changes: 2 additions & 8 deletions vectorcompattextview/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<manifest package="com.xw.repo.vectorcompattextview"
xmlns:android="http://schemas.android.com/apk/res/android">
<manifest package="com.xw.repo.vectorcompattextview">

<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">

</application>
<application/>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorInt;
Expand All @@ -24,6 +26,8 @@ public class VectorCompatTextView extends AppCompatCheckedTextView {
private int mDrawableCompatColor;
private boolean isDrawableAdjustTextWidth;
private boolean isDrawableAdjustTextHeight;
private boolean isDrawableAdjustViewWidth;
private boolean isDrawableAdjustViewHeight;
private int mDrawableWidth;
private int mDrawableHeight;

Expand Down Expand Up @@ -74,6 +78,8 @@ private void initAttrs(Context context, AttributeSet attrs) {
mDrawableCompatColor = a.getColor(R.styleable.VectorCompatTextView_drawableCompatColor, 0);
isDrawableAdjustTextWidth = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustTextWidth, false);
isDrawableAdjustTextHeight = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustTextHeight, false);
isDrawableAdjustViewWidth = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustViewWidth, false);
isDrawableAdjustViewHeight = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustViewHeight, false);
mDrawableWidth = a.getDimensionPixelSize(R.styleable.VectorCompatTextView_drawableWidth, 0);
mDrawableHeight = a.getDimensionPixelSize(R.styleable.VectorCompatTextView_drawableHeight, 0);
a.recycle();
Expand All @@ -82,6 +88,10 @@ private void initAttrs(Context context, AttributeSet attrs) {
mDrawableWidth = 0;
if (mDrawableHeight < 0)
mDrawableHeight = 0;
if (isDrawableAdjustTextWidth)
isDrawableAdjustViewWidth = false;
if (isDrawableAdjustTextHeight)
isDrawableAdjustViewHeight = false;

initDrawables(dl, dt, dr, db);
}
Expand All @@ -92,13 +102,18 @@ private void initDrawables(Drawable... drawables) {
tintDrawable(drawable);
}

if (!isDrawableAdjustTextWidth && !isDrawableAdjustTextHeight && mDrawableWidth == 0
&& mDrawableHeight == 0) {
if (!isDrawableAdjustTextWidth && !isDrawableAdjustTextHeight && !isDrawableAdjustViewWidth &&
!isDrawableAdjustViewHeight && mDrawableWidth == 0 && mDrawableHeight == 0) {
setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]);
} else {
if (isDrawableAdjustTextWidth || isDrawableAdjustTextHeight) {
boolean invalid = (isDrawableAdjustTextWidth && (drawables[0] != null || drawables[2] != null))
|| (isDrawableAdjustTextHeight && (drawables[1] != null || drawables[3] != null));
if (isDrawableAdjustTextWidth || isDrawableAdjustTextHeight || isDrawableAdjustViewWidth ||
isDrawableAdjustViewHeight) {
boolean invalid = (
(isDrawableAdjustTextWidth || isDrawableAdjustViewWidth) &&
(drawables[0] != null || drawables[2] != null))
||
((isDrawableAdjustTextHeight || isDrawableAdjustViewHeight)
&& (drawables[1] != null || drawables[3] != null));
if (invalid) {
if (mDrawableWidth > 0 || mDrawableHeight > 0) {
resizeDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
Expand Down Expand Up @@ -154,35 +169,55 @@ public void onGlobalLayout() {
getViewTreeObserver().removeOnGlobalLayoutListener(this);
}

int measuredWidth = getMeasuredWidth();
int measuredHeight = getMeasuredHeight();
if (isDrawableAdjustTextWidth) {
int h = mDrawableHeight;
int width = 0;
int height = 0;

if (dt != null) {
if (h == 0)
h = measuredWidth * dt.getIntrinsicHeight() / dt.getIntrinsicWidth();
dt.setBounds(0, 0, measuredWidth, h);
}
if (db != null) {
if (h == 0)
h = measuredWidth * db.getIntrinsicHeight() / db.getIntrinsicWidth();
db.setBounds(0, 0, measuredWidth, h);
}
if (isDrawableAdjustTextWidth) {
Paint paint = new Paint();
paint.setTextSize(getTextSize());
CharSequence text = getText();
Rect rect = new Rect();
paint.getTextBounds(text.toString(), 0, text.length(), rect);

width = rect.width();
} else if (isDrawableAdjustViewWidth) {
width = getMeasuredWidth();
}
if (isDrawableAdjustTextHeight) {
int w = mDrawableWidth;
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 = measuredHeight * dl.getIntrinsicWidth() / dl.getIntrinsicHeight();
dl.setBounds(0, 0, w, measuredHeight);
}
if (dr != null) {
if (w == 0)
w = measuredHeight * dr.getIntrinsicWidth() / dr.getIntrinsicHeight();
dr.setBounds(0, 0, w, measuredHeight);
}
int h = mDrawableHeight;
int w = mDrawableWidth;

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);
Expand Down
2 changes: 2 additions & 0 deletions vectorcompattextview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<attr name="drawableCompatColor" format="color|reference"/>
<attr name="drawableAdjustTextWidth" format="boolean"/>
<attr name="drawableAdjustTextHeight" format="boolean"/>
<attr name="drawableAdjustViewWidth" format="boolean"/>
<attr name="drawableAdjustViewHeight" format="boolean"/>
<attr name="drawableWidth" format="dimension|reference"/>
<attr name="drawableHeight" format="dimension|reference"/>
</declare-styleable>
Expand Down
3 changes: 0 additions & 3 deletions vectorcompattextview/src/main/res/values/strings.xml

This file was deleted.

0 comments on commit 50a0ff0

Please sign in to comment.