Skip to content

Commit c0047d4

Browse files
committed
Enforce a maximum size for action button icons.
Change-Id: Id9b5c1573cd40012229921fa8497cbe5ce340b1e
1 parent fa14d82 commit c0047d4

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

core/java/com/android/internal/view/menu/ActionMenuItemView.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class ActionMenuItemView extends TextView
4848
private int mMinWidth;
4949
private int mSavedPaddingLeft;
5050

51+
private static final int MAX_ICON_SIZE = 32; // dp
52+
private int mMaxIconSize;
53+
5154
public ActionMenuItemView(Context context) {
5255
this(context, null);
5356
}
@@ -67,6 +70,9 @@ public ActionMenuItemView(Context context, AttributeSet attrs, int defStyle) {
6770
com.android.internal.R.styleable.ActionMenuItemView_minWidth, 0);
6871
a.recycle();
6972

73+
final float density = res.getDisplayMetrics().density;
74+
mMaxIconSize = (int) (MAX_ICON_SIZE * density + 0.5f);
75+
7076
setOnClickListener(this);
7177
setOnLongClickListener(this);
7278

@@ -135,7 +141,20 @@ private void updateTextButtonVisibility() {
135141

136142
public void setIcon(Drawable icon) {
137143
mIcon = icon;
138-
setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
144+
int width = icon.getIntrinsicWidth();
145+
int height = icon.getIntrinsicHeight();
146+
if (width > mMaxIconSize) {
147+
final float scale = (float) mMaxIconSize / width;
148+
width = mMaxIconSize;
149+
height *= scale;
150+
}
151+
if (height > mMaxIconSize) {
152+
final float scale = (float) mMaxIconSize / height;
153+
height = mMaxIconSize;
154+
width *= scale;
155+
}
156+
icon.setBounds(0, 0, width, height);
157+
setCompoundDrawables(icon, null, null, null);
139158

140159
updateTextButtonVisibility();
141160
}
@@ -245,7 +264,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
245264
// TextView won't center compound drawables in both dimensions without
246265
// a little coercion. Pad in to center the icon after we've measured.
247266
final int w = getMeasuredWidth();
248-
final int dw = mIcon.getIntrinsicWidth();
267+
final int dw = mIcon.getBounds().width();
249268
super.setPadding((w - dw) / 2, getPaddingTop(), getPaddingRight(), getPaddingBottom());
250269
}
251270
}

0 commit comments

Comments
 (0)