Skip to content

Commit 4381e1f

Browse files
author
roman_tcaregorodtcev
committed
Fixed review
1 parent ef966d7 commit 4381e1f

File tree

2 files changed

+20
-35
lines changed

2 files changed

+20
-35
lines changed

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="40sp"
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"

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

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
import android.content.Context;
44
import android.content.res.TypedArray;
5-
import android.graphics.Bitmap;
65
import android.graphics.Color;
76
import android.graphics.Paint;
87
import android.graphics.Rect;
9-
import android.graphics.drawable.BitmapDrawable;
108
import android.graphics.drawable.Drawable;
11-
import android.graphics.drawable.ScaleDrawable;
129
import android.text.method.TransformationMethod;
1310
import android.util.AttributeSet;
1411

@@ -25,8 +22,6 @@
2522
import java.util.List;
2623
import java.util.StringTokenizer;
2724

28-
import static android.view.Gravity.NO_GRAVITY;
29-
3025
public class OmegaCenterIconButton extends AppCompatButton {
3126

3227
private static final String DELIMITERS = "\n";
@@ -89,15 +84,15 @@ private void updateDrawables() {
8984
wrappedDrawable = getTintedDrawable(wrappedDrawable);
9085
}
9186
if (mDrawableSize != -1) {
92-
wrappedDrawable = getScaleDrawable(wrappedDrawable);
87+
wrappedDrawable = updateDrawableBounds(wrappedDrawable);
9388
}
9489
wrappedDrawables[i] = wrappedDrawable;
9590
}
9691
}
97-
setCompoundDrawablesWithIntrinsicBounds(wrappedDrawables[DRAWABLE_LEFT_POSITION],
98-
wrappedDrawables[DRAWABLE_TOP_POSITION],
99-
wrappedDrawables[DRAWABLE_RIGHT_POSITION],
100-
wrappedDrawables[DRAWABLE_BOTTOM_POSITION]);
92+
setCompoundDrawables(wrappedDrawables[DRAWABLE_LEFT_POSITION],
93+
wrappedDrawables[DRAWABLE_TOP_POSITION],
94+
wrappedDrawables[DRAWABLE_RIGHT_POSITION],
95+
wrappedDrawables[DRAWABLE_BOTTOM_POSITION]);
10196
}
10297
}
10398

@@ -109,27 +104,14 @@ private Drawable getTintedDrawable(@NonNull Drawable drawable) {
109104
}
110105

111106
@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-
107+
private Drawable updateDrawableBounds(@NonNull Drawable drawable) {
108+
drawable.getBounds().set(0, 0, mDrawableSize, mDrawableSize);
127109
return drawable;
128110
}
129111

130112
@Override
131-
public void setCompoundDrawablesWithIntrinsicBounds(@DrawableRes int left, @DrawableRes int top, @DrawableRes int right, @DrawableRes int bottom) {
132-
super.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);
113+
public void setCompoundDrawables(@Nullable Drawable left, @Nullable Drawable top, @Nullable Drawable right, @Nullable Drawable bottom) {
114+
super.setCompoundDrawables(left, top, right, bottom);
133115
updatePadding();
134116
}
135117

@@ -167,7 +149,7 @@ private void updatePadding(int width) {
167149
if (width == 0) return;
168150

169151
Drawable[] compoundDrawables = getCompoundDrawables();
170-
if (compoundDrawables.length == 0 || compoundDrawables.length != DRAWABLES_LENGTH) return;
152+
if (compoundDrawables.length != DRAWABLES_LENGTH) return;
171153

172154
Drawable leftDrawable = compoundDrawables[DRAWABLE_LEFT_POSITION];
173155
Drawable rightDrawable = compoundDrawables[DRAWABLE_RIGHT_POSITION];
@@ -177,15 +159,17 @@ private void updatePadding(int width) {
177159
int iconPadding = Math.max(getCompoundDrawablePadding(), 1);
178160
int paddingSize;
179161

162+
int leftWidth = leftDrawable == null ? 0 : leftDrawable.getBounds().width();
163+
int rightWidth = rightDrawable == null ? 0 : rightDrawable.getBounds().width();
164+
180165
if (leftDrawable != null && rightDrawable != null) {
181-
paddingSize = (width - leftDrawable.getIntrinsicWidth() - rightDrawable.getIntrinsicWidth() - textWidth - iconPadding * 4) / 2;
166+
paddingSize = (width - leftWidth - rightWidth - textWidth - iconPadding * 4) / 2;
182167
} else if (leftDrawable != null) {
183-
paddingSize = (width - leftDrawable.getIntrinsicWidth() - iconPadding * 2 - textWidth) / 2;
168+
paddingSize = (width - leftWidth - iconPadding * 2 - textWidth) / 2;
184169
} else {
185-
paddingSize = (width - rightDrawable.getIntrinsicWidth() - iconPadding * 2 - textWidth) / 2;
170+
paddingSize = (width - rightWidth - iconPadding * 2 - textWidth) / 2;
186171
}
187172

188-
189173
super.setPadding(Math.max(mLeftPadding, paddingSize), getPaddingTop(), Math.max(paddingSize, mRightPadding), getPaddingBottom());
190174
}
191175

@@ -213,7 +197,7 @@ private String divideText() {
213197
return isAllCaps() ? list.get(0).toUpperCase() : list.get(0);
214198
}
215199
String longPart = list.get(0);
216-
for(int i = 0; i < list.size() - 1; i++) {
200+
for (int i = 0; i < list.size() - 1; i++) {
217201
if (list.get(i + 1).length() > list.get(i).length()) {
218202
longPart = list.get(i + 1);
219203
}
@@ -224,7 +208,7 @@ private String divideText() {
224208

225209
public boolean isAllCaps() {
226210
TransformationMethod method = getTransformationMethod();
227-
if(method == null) return false;
211+
if (method == null) return false;
228212

229213
return method.getClass().getSimpleName().equals("AllCapsTransformationMethod");
230214
}

0 commit comments

Comments
 (0)