diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index b83a03a..d810bd6 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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"/> diff --git a/vectorcompattextview/build.gradle b/vectorcompattextview/build.gradle index 1441081..6ed6a21 100644 --- a/vectorcompattextview/build.gradle +++ b/vectorcompattextview/build.gradle @@ -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 { @@ -20,5 +20,5 @@ android { } dependencies { - provided 'com.android.support:appcompat-v7:27.0.0' + provided 'com.android.support:appcompat-v7:27.0.2' } diff --git a/vectorcompattextview/src/main/AndroidManifest.xml b/vectorcompattextview/src/main/AndroidManifest.xml index 03b4fad..ec14075 100644 --- a/vectorcompattextview/src/main/AndroidManifest.xml +++ b/vectorcompattextview/src/main/AndroidManifest.xml @@ -1,11 +1,5 @@ - + - - - + diff --git a/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java b/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java index 968a803..9f8b6eb 100644 --- a/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java +++ b/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java @@ -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; @@ -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; @@ -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(); @@ -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); } @@ -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]); @@ -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); diff --git a/vectorcompattextview/src/main/res/values/attrs.xml b/vectorcompattextview/src/main/res/values/attrs.xml index d933e63..251351b 100644 --- a/vectorcompattextview/src/main/res/values/attrs.xml +++ b/vectorcompattextview/src/main/res/values/attrs.xml @@ -9,6 +9,8 @@ + + diff --git a/vectorcompattextview/src/main/res/values/strings.xml b/vectorcompattextview/src/main/res/values/strings.xml deleted file mode 100644 index 83ee4a8..0000000 --- a/vectorcompattextview/src/main/res/values/strings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - VectorCompatTextView -