diff --git a/app/build.gradle b/app/build.gradle index dc9105a..f52e34b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,14 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 26 - buildToolsVersion "26.0.2" + compileSdkVersion 28 defaultConfig { applicationId "com.xw.sample.vectorcompattextview" minSdkVersion 14 - targetSdkVersion 26 - versionCode 2 - versionName "2.0" + targetSdkVersion 28 + versionCode 3 + versionName "2.1" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true } @@ -21,11 +20,11 @@ android { } dependencies { - compile fileTree(include: ['*.jar'], dir: 'libs') - androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + implementation fileTree(include: ['*.jar'], dir: 'libs') + androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:26.0.0-alpha1' - testCompile 'junit:junit:4.12' - compile project(':vectorcompattextview') + implementation 'com.android.support:appcompat-v7:28.0.0' + testImplementation 'junit:junit:4.12' + implementation project(':vectorcompattextview') } diff --git a/app/src/main/java/com/xw/sample/vectorcompattextview/MainActivity.java b/app/src/main/java/com/xw/sample/vectorcompattextview/MainActivity.java index 83323fa..4a262ce 100644 --- a/app/src/main/java/com/xw/sample/vectorcompattextview/MainActivity.java +++ b/app/src/main/java/com/xw/sample/vectorcompattextview/MainActivity.java @@ -13,7 +13,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - final VectorCompatTextView textView = (VectorCompatTextView) findViewById(R.id.checkable_text_view); + final VectorCompatTextView textView = findViewById(R.id.checkable_text_view); textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/build.gradle b/build.gradle index f93d608..2d45a15 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.0.0' + classpath 'com.android.tools.build:gradle:3.3.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 41825a0..b03fc56 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Nov 07 14:08:50 CST 2017 +#Mon Apr 01 09:58:58 CST 2019 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip diff --git a/vectorcompattextview/build.gradle b/vectorcompattextview/build.gradle index 50e610c..479b85d 100644 --- a/vectorcompattextview/build.gradle +++ b/vectorcompattextview/build.gradle @@ -1,14 +1,13 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 27 - buildToolsVersion "27.0.3" + compileSdkVersion 28 defaultConfig { minSdkVersion 9 - targetSdkVersion 27 - versionCode 13 - versionName "2.5" + targetSdkVersion 28 + versionCode 14 + versionName "2.6" } buildTypes { @@ -20,5 +19,5 @@ android { } dependencies { - provided 'com.android.support:appcompat-v7:27.0.2' + compileOnly 'com.android.support:appcompat-v7:28.0.0' } diff --git a/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java b/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java index 0782459..291dcf6 100644 --- a/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java +++ b/vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java @@ -1,16 +1,19 @@ package com.xw.repo; import android.content.Context; +import android.content.res.Resources; 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; +import android.support.annotation.NonNull; import android.support.v4.graphics.drawable.DrawableCompat; import android.support.v7.content.res.AppCompatResources; import android.support.v7.widget.AppCompatCheckedTextView; import android.util.AttributeSet; +import android.util.TypedValue; import android.view.ViewTreeObserver; import com.xw.repo.vectorcompattextview.R; @@ -84,20 +87,20 @@ private void initAttrs(Context context, AttributeSet attrs) { mDrawableHeight = a.getDimensionPixelSize(R.styleable.VectorCompatTextView_drawableHeight, 0); a.recycle(); - if (mDrawableWidth < 0) - mDrawableWidth = 0; - if (mDrawableHeight < 0) - mDrawableHeight = 0; - if (isDrawableAdjustTextWidth) - isDrawableAdjustViewWidth = false; - if (isDrawableAdjustTextHeight) - isDrawableAdjustViewHeight = false; - initDrawables(dl, dt, dr, db); } } private void initDrawables(final Drawable... drawables) { + if (mDrawableWidth < 0) + mDrawableWidth = 0; + if (mDrawableHeight < 0) + mDrawableHeight = 0; + if (isDrawableAdjustTextWidth) + isDrawableAdjustViewWidth = false; + if (isDrawableAdjustTextHeight) + isDrawableAdjustViewHeight = false; + for (Drawable drawable : drawables) { tintDrawable(drawable); } @@ -142,13 +145,10 @@ public void onGlobalLayout() { private void tintDrawable(Drawable drawable) { if (drawable != null) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - drawable = DrawableCompat.wrap(drawable).mutate(); - } if (isTintDrawableInTextColor) { - DrawableCompat.setTint(drawable, getCurrentTextColor()); + DrawableCompat.setTint(drawable.mutate(), getCurrentTextColor()); } else if (mDrawableCompatColor != 0) { - DrawableCompat.setTint(drawable, mDrawableCompatColor); + DrawableCompat.setTint(drawable.mutate(), mDrawableCompatColor); } } } @@ -313,4 +313,87 @@ protected void drawableStateChanged() { } } } + + + //////////////////////////////////////////////////////////////////////////////////////////////// + public static class CompoundDrawableConfigBuilder { + + private VectorCompatTextView mTextView; + + private Drawable mDrawableLeft_, mDrawableTop_, mDrawableRight_, mDrawableBottom_; + private boolean isTintDrawableInTextColor_; + private int mDrawableColor_; + private boolean isDrawableAdjustTextWidth_; + private boolean isDrawableAdjustTextHeight_; + private boolean isDrawableAdjustViewWidth_; + private boolean isDrawableAdjustViewHeight_; + private int mDrawableWidth_; + private int mDrawableHeight_; + + public CompoundDrawableConfigBuilder(@NonNull VectorCompatTextView textView) { + mTextView = textView; + } + + public CompoundDrawableConfigBuilder setCompoundDrawables(Drawable l, Drawable t, Drawable r, Drawable b) { + mDrawableLeft_ = l; + mDrawableTop_ = t; + mDrawableRight_ = r; + mDrawableBottom_ = b; + return this; + } + + public CompoundDrawableConfigBuilder tintDrawableInTextColor() { + isTintDrawableInTextColor_ = true; + return this; + } + + public CompoundDrawableConfigBuilder setDrawableColor(@ColorInt int color) { + mDrawableColor_ = color; + return this; + } + + public CompoundDrawableConfigBuilder drawableWidthAdjustTextWidth() { + isDrawableAdjustTextWidth_ = true; + return this; + } + + public CompoundDrawableConfigBuilder drawableHeightAdjustTextHeight() { + isDrawableAdjustTextHeight_ = true; + return this; + } + + public CompoundDrawableConfigBuilder drawableWidthAdjustViewWidth() { + isDrawableAdjustViewWidth_ = true; + return this; + } + + public CompoundDrawableConfigBuilder drawableHeightAdjustViewHeight() { + isDrawableAdjustViewHeight_ = true; + return this; + } + + public CompoundDrawableConfigBuilder setDrawableWidth(int widthInDp) { + mDrawableWidth_ = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, widthInDp, + Resources.getSystem().getDisplayMetrics()); + return this; + } + + public CompoundDrawableConfigBuilder setDrawableHeight(int heightInDp) { + mDrawableHeight_ = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, heightInDp, + Resources.getSystem().getDisplayMetrics()); + return this; + } + + public void build() { + mTextView.isTintDrawableInTextColor = isTintDrawableInTextColor_; + mTextView.mDrawableCompatColor = mDrawableColor_; + mTextView.isDrawableAdjustTextWidth = isDrawableAdjustTextWidth_; + mTextView.isDrawableAdjustTextHeight = isDrawableAdjustTextHeight_; + mTextView.isDrawableAdjustViewWidth = isDrawableAdjustViewWidth_; + mTextView.isDrawableAdjustViewHeight = isDrawableAdjustViewHeight_; + mTextView.mDrawableWidth = mDrawableWidth_; + mTextView.mDrawableHeight = mDrawableHeight_; + mTextView.initDrawables(mDrawableLeft_, mDrawableTop_, mDrawableRight_, mDrawableBottom_); + } + } } \ No newline at end of file