Skip to content

Commit da60f9b

Browse files
author
roman_tcaregorodtcev
committed
drawable size attribute added
1 parent eda152c commit da60f9b

File tree

7 files changed

+68
-27
lines changed

7 files changed

+68
-27
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: 3 additions & 3 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
}

build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +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
2+
ext.kotlinVersion = '1.3.41'
3+
ext.appCompatLibraryVersion = '1.0.2'
4+
ext.target_Sdk_Version = 28
5+
ext.compile_Sdk_Version = 28
6+
ext.min_Sdk_Version = 14
77

88

99
repositories {
@@ -13,7 +13,7 @@ buildscript {
1313
maven { url 'https://jitpack.io' }
1414
}
1515
dependencies {
16-
classpath 'com.android.tools.build:gradle:3.2.1'
16+
classpath 'com.android.tools.build:gradle:3.4.2'
1717
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
1818
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
1919
}

center_icon_button/build.gradle

Lines changed: 3 additions & 3 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
}

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

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@
22

33
import android.content.Context;
44
import android.content.res.TypedArray;
5+
import android.graphics.Bitmap;
56
import android.graphics.Color;
67
import android.graphics.Paint;
78
import android.graphics.Rect;
9+
import android.graphics.drawable.BitmapDrawable;
810
import android.graphics.drawable.Drawable;
11+
import android.graphics.drawable.ScaleDrawable;
912
import android.text.method.TransformationMethod;
1013
import android.util.AttributeSet;
1114

15+
import androidx.annotation.ColorInt;
16+
import androidx.annotation.DrawableRes;
17+
import androidx.annotation.NonNull;
18+
import androidx.annotation.Nullable;
19+
import androidx.appcompat.widget.AppCompatButton;
20+
import androidx.core.graphics.drawable.DrawableCompat;
21+
1222
import com.omega_r.libs.centericonbutton.R;
1323

1424
import java.util.ArrayList;
1525
import java.util.List;
1626
import java.util.StringTokenizer;
1727

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;
28+
import static android.view.Gravity.NO_GRAVITY;
2329

2430
public class OmegaCenterIconButton extends AppCompatButton {
2531

@@ -32,9 +38,10 @@ public class OmegaCenterIconButton extends AppCompatButton {
3238

3339
private Rect textBoundsRect;
3440
@ColorInt
35-
private int tintColor = Color.TRANSPARENT;
41+
private int mTintColor = Color.TRANSPARENT;
3642
private int mLeftPadding;
3743
private int mRightPadding;
44+
private int mDrawableSize;
3845

3946
public OmegaCenterIconButton(Context context) {
4047
super(context);
@@ -54,29 +61,36 @@ public OmegaCenterIconButton(Context context, AttributeSet attrs, int defStyleAt
5461
private void init(Context context, AttributeSet attrs) {
5562
if (attrs != null) {
5663
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.OmegaCenterIconButton);
57-
tintColor = typedArray.getColor(R.styleable.OmegaCenterIconButton_drawableTint, Color.TRANSPARENT);
64+
mTintColor = typedArray.getColor(R.styleable.OmegaCenterIconButton_drawableTint, Color.TRANSPARENT);
65+
mDrawableSize = typedArray.getDimensionPixelSize(R.styleable.OmegaCenterIconButton_drawableSize, -1);
66+
5867
float defaultDrawablePadding = getResources().getDimension(R.dimen.omega_default_drawable_padding);
5968
int drawablePadding = (int) typedArray.getDimension(R.styleable.OmegaCenterIconButton_android_drawablePadding, defaultDrawablePadding);
6069
setCompoundDrawablePadding(drawablePadding);
6170

62-
updateTint();
71+
updateDrawables();
6372
typedArray.recycle();
6473
}
6574
mLeftPadding = getPaddingLeft();
6675
mRightPadding = getPaddingRight();
6776
}
6877

69-
private void updateTint() {
70-
if (tintColor != Color.TRANSPARENT) {
78+
private void updateDrawables() {
79+
if (mTintColor != Color.TRANSPARENT || mDrawableSize != -1) {
7180
Drawable[] drawables = getCompoundDrawables();
7281
if (drawables.length != DRAWABLES_LENGTH) return;
7382

7483
Drawable[] wrappedDrawables = new Drawable[DRAWABLES_LENGTH];
7584
for (int i = 0; i < DRAWABLES_LENGTH; i++) {
7685
Drawable drawable = drawables[i];
7786
if (drawable != null) {
78-
Drawable wrappedDrawable = DrawableCompat.wrap(drawable).mutate();
79-
DrawableCompat.setTint(wrappedDrawable, tintColor);
87+
Drawable wrappedDrawable = drawable;
88+
if (mTintColor != Color.TRANSPARENT) {
89+
wrappedDrawable = getTintedDrawable(wrappedDrawable);
90+
}
91+
if (mDrawableSize != -1) {
92+
wrappedDrawable = getScaleDrawable(wrappedDrawable);
93+
}
8094
wrappedDrawables[i] = wrappedDrawable;
8195
}
8296
}
@@ -87,6 +101,32 @@ private void updateTint() {
87101
}
88102
}
89103

104+
@NonNull
105+
private Drawable getTintedDrawable(@NonNull Drawable drawable) {
106+
Drawable mutate = DrawableCompat.wrap(drawable).mutate();
107+
DrawableCompat.setTint(mutate, mTintColor);
108+
return mutate;
109+
}
110+
111+
@NonNull
112+
private Drawable getScaleDrawable(@NonNull Drawable drawable) {
113+
if (drawable instanceof BitmapDrawable) {
114+
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
115+
if (bitmap != null) {
116+
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, mDrawableSize, mDrawableSize, true);
117+
if (scaledBitmap != null) return new BitmapDrawable(getResources(), scaledBitmap);
118+
}
119+
}
120+
121+
Drawable scaleDrawable = new ScaleDrawable(drawable, NO_GRAVITY, mDrawableSize, mDrawableSize).getDrawable();
122+
if (scaleDrawable != null) {
123+
scaleDrawable.setBounds(0, 0, mDrawableSize, mDrawableSize);
124+
return scaleDrawable;
125+
}
126+
127+
return drawable;
128+
}
129+
90130
@Override
91131
public void setCompoundDrawablesWithIntrinsicBounds(@DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {
92132
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

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)