Skip to content

Commit 8a6eb28

Browse files
Merge pull request #10 from Omega-R/feature/drawable_size
drawable size attribute added
2 parents eda152c + b2cdcfd commit 8a6eb28

File tree

8 files changed

+78
-47
lines changed

8 files changed

+78
-47
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ apply plugin: 'kotlin-android'
33
apply plugin: 'kotlin-android-extensions'
44

55
android {
6-
compileSdkVersion rootProject.compileSdkVersion
6+
compileSdkVersion rootProject.compile_sdk_version
77

88
defaultConfig {
99
applicationId "com.omega"
10-
minSdkVersion rootProject.minSdkVersion
11-
targetSdkVersion rootProject.targetSdkVersion
10+
minSdkVersion rootProject.min_sdk_version
11+
targetSdkVersion rootProject.target_sdk_version
1212
versionCode 1
1313
versionName "1.0"
1414
}
@@ -23,8 +23,8 @@ android {
2323

2424
dependencies {
2525
api fileTree(dir: 'libs', include: ['*.jar'])
26-
api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
27-
api "androidx.appcompat:appcompat:$appCompatLibraryVersion"
26+
api "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
27+
api "androidx.appcompat:appcompat:$app_compat_version"
2828

2929
api project(":center_icon_button")
3030
}

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6+
xmlns:app="http://schemas.android.com/apk/res-auto"
67
android:orientation="vertical"
78
tools:context="com.omega.MainActivity">
89

@@ -22,9 +23,9 @@
2223
android:drawableRight="@drawable/ic_android_black_24dp"
2324
android:text="Great"
2425
android:textSize="14sp"
26+
app:drawableSize="40dp"
2527
android:theme="@style/AccentButtonStyle" />
2628

27-
2829
<com.omega_r.libs.OmegaCenterIconButton
2930
android:layout_width="match_parent"
3031
android:layout_height="wrap_content"

build.gradle

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
buildscript {
2-
ext.kotlinVersion = '1.2.71'
3-
ext.appCompatLibraryVersion = '1.0.0'
4-
ext.targetSdkVersion = 28
5-
ext.compileSdkVersion = 28
6-
ext.minSdkVersion = 14
7-
2+
ext.kotlin_version = '1.3.41'
3+
ext.app_compat_version = '1.0.2'
4+
ext.target_sdk_version = 28
5+
ext.compile_sdk_version = 28
6+
ext.min_sdk_version = 14
87

98
repositories {
109
google()
@@ -13,8 +12,8 @@ buildscript {
1312
maven { url 'https://jitpack.io' }
1413
}
1514
dependencies {
16-
classpath 'com.android.tools.build:gradle:3.2.1'
17-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
15+
classpath 'com.android.tools.build:gradle:3.4.2'
16+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1817
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
1918
}
2019
}

center_icon_button/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ apply plugin: 'com.github.dcendents.android-maven'
33
group = 'com.github.Omega-R'
44

55
android {
6-
compileSdkVersion rootProject.compileSdkVersion
6+
compileSdkVersion rootProject.compile_sdk_version
77

88
defaultConfig {
9-
minSdkVersion rootProject.minSdkVersion
10-
targetSdkVersion rootProject.targetSdkVersion
9+
minSdkVersion rootProject.min_sdk_version
10+
targetSdkVersion rootProject.target_sdk_version
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -22,5 +22,5 @@ android {
2222

2323
dependencies {
2424
implementation fileTree(dir: 'libs', include: ['*.jar'])
25-
compileOnly "androidx.appcompat:appcompat:$appCompatLibraryVersion"
25+
compileOnly "androidx.appcompat:appcompat:$app_compat_version"
2626
}

center_icon_button/src/main/java/com/omega_r/libs/OmegaCenterIconButton.java

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
import android.text.method.TransformationMethod;
1010
import android.util.AttributeSet;
1111

12+
import androidx.annotation.ColorInt;
13+
import androidx.annotation.NonNull;
14+
import androidx.annotation.Nullable;
15+
import androidx.appcompat.widget.AppCompatButton;
16+
import androidx.core.graphics.drawable.DrawableCompat;
17+
1218
import com.omega_r.libs.centericonbutton.R;
1319

1420
import java.util.ArrayList;
1521
import java.util.List;
1622
import java.util.StringTokenizer;
1723

18-
import androidx.annotation.ColorInt;
19-
import androidx.annotation.DrawableRes;
20-
import androidx.annotation.Nullable;
21-
import androidx.appcompat.widget.AppCompatButton;
22-
import androidx.core.graphics.drawable.DrawableCompat;
23-
2424
public class OmegaCenterIconButton extends AppCompatButton {
2525

2626
private static final String DELIMITERS = "\n";
@@ -32,9 +32,10 @@ public class OmegaCenterIconButton extends AppCompatButton {
3232

3333
private Rect textBoundsRect;
3434
@ColorInt
35-
private int tintColor = Color.TRANSPARENT;
35+
private int mTintColor = Color.TRANSPARENT;
3636
private int mLeftPadding;
3737
private int mRightPadding;
38+
private int mDrawableSize;
3839

3940
public OmegaCenterIconButton(Context context) {
4041
super(context);
@@ -54,42 +55,69 @@ public OmegaCenterIconButton(Context context, AttributeSet attrs, int defStyleAt
5455
private void init(Context context, AttributeSet attrs) {
5556
if (attrs != null) {
5657
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.OmegaCenterIconButton);
57-
tintColor = typedArray.getColor(R.styleable.OmegaCenterIconButton_drawableTint, Color.TRANSPARENT);
58+
mTintColor = typedArray.getColor(R.styleable.OmegaCenterIconButton_drawableTint, Color.TRANSPARENT);
59+
mDrawableSize = typedArray.getDimensionPixelSize(R.styleable.OmegaCenterIconButton_drawableSize, -1);
60+
5861
float defaultDrawablePadding = getResources().getDimension(R.dimen.omega_default_drawable_padding);
5962
int drawablePadding = (int) typedArray.getDimension(R.styleable.OmegaCenterIconButton_android_drawablePadding, defaultDrawablePadding);
6063
setCompoundDrawablePadding(drawablePadding);
6164

62-
updateTint();
65+
updateDrawables();
6366
typedArray.recycle();
6467
}
6568
mLeftPadding = getPaddingLeft();
6669
mRightPadding = getPaddingRight();
6770
}
6871

69-
private void updateTint() {
70-
if (tintColor != Color.TRANSPARENT) {
72+
private void updateDrawables() {
73+
if (mTintColor != Color.TRANSPARENT || mDrawableSize != -1) {
7174
Drawable[] drawables = getCompoundDrawables();
7275
if (drawables.length != DRAWABLES_LENGTH) return;
7376

7477
Drawable[] wrappedDrawables = new Drawable[DRAWABLES_LENGTH];
7578
for (int i = 0; i < DRAWABLES_LENGTH; i++) {
7679
Drawable drawable = drawables[i];
7780
if (drawable != null) {
78-
Drawable wrappedDrawable = DrawableCompat.wrap(drawable).mutate();
79-
DrawableCompat.setTint(wrappedDrawable, tintColor);
81+
Drawable wrappedDrawable = drawable;
82+
if (mTintColor != Color.TRANSPARENT) {
83+
wrappedDrawable = getTintedDrawable(wrappedDrawable);
84+
}
85+
if (mDrawableSize > 0) {
86+
wrappedDrawable = updateDrawableBounds(wrappedDrawable);
87+
}
8088
wrappedDrawables[i] = wrappedDrawable;
8189
}
8290
}
83-
setCompoundDrawablesWithIntrinsicBounds(wrappedDrawables[DRAWABLE_LEFT_POSITION],
84-
wrappedDrawables[DRAWABLE_TOP_POSITION],
85-
wrappedDrawables[DRAWABLE_RIGHT_POSITION],
86-
wrappedDrawables[DRAWABLE_BOTTOM_POSITION]);
91+
if (mDrawableSize > 0) {
92+
setCompoundDrawables(wrappedDrawables[DRAWABLE_LEFT_POSITION],
93+
wrappedDrawables[DRAWABLE_TOP_POSITION],
94+
wrappedDrawables[DRAWABLE_RIGHT_POSITION],
95+
wrappedDrawables[DRAWABLE_BOTTOM_POSITION]);
96+
} else {
97+
setCompoundDrawablesWithIntrinsicBounds(wrappedDrawables[DRAWABLE_LEFT_POSITION],
98+
wrappedDrawables[DRAWABLE_TOP_POSITION],
99+
wrappedDrawables[DRAWABLE_RIGHT_POSITION],
100+
wrappedDrawables[DRAWABLE_BOTTOM_POSITION]);
101+
}
87102
}
88103
}
89104

105+
@NonNull
106+
private Drawable getTintedDrawable(@NonNull Drawable drawable) {
107+
Drawable mutate = DrawableCompat.wrap(drawable).mutate();
108+
DrawableCompat.setTint(mutate, mTintColor);
109+
return mutate;
110+
}
111+
112+
@NonNull
113+
private Drawable updateDrawableBounds(@NonNull Drawable drawable) {
114+
drawable.getBounds().set(0, 0, mDrawableSize, mDrawableSize);
115+
return drawable;
116+
}
117+
90118
@Override
91-
public void setCompoundDrawablesWithIntrinsicBounds(@DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {
92-
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
119+
public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
120+
super.setCompoundDrawables(left, top, right, bottom);
93121
updatePadding();
94122
}
95123

@@ -127,7 +155,7 @@ private void updatePadding(int width) {
127155
if (width == 0) return;
128156

129157
Drawable[] compoundDrawables = getCompoundDrawables();
130-
if (compoundDrawables.length == 0 || compoundDrawables.length != DRAWABLES_LENGTH) return;
158+
if (compoundDrawables.length != DRAWABLES_LENGTH) return;
131159

132160
Drawable leftDrawable = compoundDrawables[DRAWABLE_LEFT_POSITION];
133161
Drawable rightDrawable = compoundDrawables[DRAWABLE_RIGHT_POSITION];
@@ -137,15 +165,17 @@ private void updatePadding(int width) {
137165
int iconPadding = Math.max(getCompoundDrawablePadding(), 1);
138166
int paddingSize;
139167

168+
int leftWidth = leftDrawable == null ? 0 : leftDrawable.getBounds().width();
169+
int rightWidth = rightDrawable == null ? 0 : rightDrawable.getBounds().width();
170+
140171
if (leftDrawable != null && rightDrawable != null) {
141-
paddingSize = (width - leftDrawable.getIntrinsicWidth() - rightDrawable.getIntrinsicWidth() - textWidth - iconPadding * 4) / 2;
172+
paddingSize = (width - leftWidth - rightWidth - textWidth - iconPadding * 4) / 2;
142173
} else if (leftDrawable != null) {
143-
paddingSize = (width - leftDrawable.getIntrinsicWidth() - iconPadding * 2 - textWidth) / 2;
174+
paddingSize = (width - leftWidth - iconPadding * 2 - textWidth) / 2;
144175
} else {
145-
paddingSize = (width - rightDrawable.getIntrinsicWidth() - iconPadding * 2 - textWidth) / 2;
176+
paddingSize = (width - rightWidth - iconPadding * 2 - textWidth) / 2;
146177
}
147178

148-
149179
super.setPadding(Math.max(mLeftPadding, paddingSize), getPaddingTop(), Math.max(paddingSize, mRightPadding), getPaddingBottom());
150180
}
151181

@@ -173,7 +203,7 @@ private String divideText() {
173203
return isAllCaps() ? list.get(0).toUpperCase() : list.get(0);
174204
}
175205
String longPart = list.get(0);
176-
for(int i = 0; i < list.size() - 1; i++) {
206+
for (int i = 0; i < list.size() - 1; i++) {
177207
if (list.get(i + 1).length() > list.get(i).length()) {
178208
longPart = list.get(i + 1);
179209
}
@@ -184,7 +214,7 @@ private String divideText() {
184214

185215
public boolean isAllCaps() {
186216
TransformationMethod method = getTransformationMethod();
187-
if(method == null) return false;
217+
if (method == null) return false;
188218

189219
return method.getClass().getSimpleName().equals("AllCapsTransformationMethod");
190220
}

center_icon_button/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
<declare-styleable name="OmegaCenterIconButton">
55
<attr name="drawableTint" format="color"/>
6+
<attr name="drawableSize" format="dimension"/>
67
<attr name="android:drawablePadding"/>
78
</declare-styleable>
89

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Tue Oct 16 18:21:13 CEST 2018
1+
#Tue Jul 30 18:22:21 MSK 2019
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip

0 commit comments

Comments
 (0)