Skip to content

Commit

Permalink
Added the 'CompoundDrawableConfigBuilder'.
Browse files Browse the repository at this point in the history
  • Loading branch information
woxingxiao committed Apr 1, 2019
1 parent 1c73de6 commit 03380f9
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 34 deletions.
19 changes: 9 additions & 10 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -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')
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
11 changes: 5 additions & 6 deletions vectorcompattextview/build.gradle
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -20,5 +19,5 @@ android {
}

dependencies {
provided 'com.android.support:appcompat-v7:27.0.2'
compileOnly 'com.android.support:appcompat-v7:28.0.0'
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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_);
}
}
}

0 comments on commit 03380f9

Please sign in to comment.