Skip to content

Commit 1db449c

Browse files
gsajithafohrman
authored andcommitted
Refactor dpToPx method into ViewUtils
PiperOrigin-RevId: 235243747
1 parent 6c3b510 commit 1db449c

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

lib/java/com/google/android/material/chip/Chip.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import android.annotation.TargetApi;
2323
import android.content.Context;
2424
import android.content.res.ColorStateList;
25-
import android.content.res.Resources;
2625
import android.content.res.TypedArray;
2726
import android.graphics.Outline;
2827
import android.graphics.PorterDuff.Mode;
@@ -67,7 +66,6 @@
6766
import android.text.TextUtils.TruncateAt;
6867
import android.util.AttributeSet;
6968
import android.util.Log;
70-
import android.util.TypedValue;
7169
import android.view.Gravity;
7270
import android.view.KeyEvent;
7371
import android.view.MotionEvent;
@@ -285,7 +283,8 @@ private void initMinTouchTarget(Context context, AttributeSet attrs, int defStyl
285283
R.style.Widget_MaterialComponents_Chip_Action);
286284
ensureMinTouchTargetSize = a.getBoolean(R.styleable.Chip_ensureMinTouchTargetSize, false);
287285

288-
float defaultMinTouchTargetSize = (float) Math.ceil(dpToPx(MIN_TOUCH_TARGET_DP, getContext()));
286+
float defaultMinTouchTargetSize =
287+
(float) Math.ceil(ViewUtils.dpToPx(getContext(), MIN_TOUCH_TARGET_DP));
289288
minTouchTargetSize =
290289
(int)
291290
Math.ceil(
@@ -2301,9 +2300,4 @@ private void insetChipBackgroundDrawable(
23012300
insetBackgroundDrawable =
23022301
new InsetDrawable(chipDrawable, insetLeft, insetTop, insetRight, insetBottom);
23032302
}
2304-
2305-
private static float dpToPx(@Dimension(unit = Dimension.DP) int dp, Context context) {
2306-
Resources r = context.getResources();
2307-
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
2308-
}
23092303
}

lib/java/com/google/android/material/internal/ViewUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818

1919
import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;
2020

21+
import android.content.Context;
22+
import android.content.res.Resources;
2123
import android.graphics.PorterDuff;
24+
import androidx.annotation.Dimension;
2225
import androidx.annotation.RestrictTo;
2326
import androidx.core.view.ViewCompat;
27+
import android.util.TypedValue;
2428
import android.view.View;
2529

2630
/**
@@ -52,4 +56,9 @@ public static PorterDuff.Mode parseTintMode(int value, PorterDuff.Mode defaultMo
5256
public static boolean isLayoutRtl(View view) {
5357
return ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
5458
}
59+
60+
public static float dpToPx(Context context, @Dimension(unit = Dimension.DP) int dp) {
61+
Resources r = context.getResources();
62+
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
63+
}
5564
}

lib/java/com/google/android/material/tabs/TabLayout.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,11 +1545,6 @@ private void updateTabViewLayoutParams(LinearLayout.LayoutParams lp) {
15451545
}
15461546
}
15471547

1548-
@Dimension(unit = Dimension.PX)
1549-
int dpToPx(@Dimension(unit = Dimension.DP) int dps) {
1550-
return Math.round(getResources().getDisplayMetrics().density * dps);
1551-
}
1552-
15531548
@Override
15541549
protected void onDraw(Canvas canvas) {
15551550
// Draw tab background layer for each tab item
@@ -1567,7 +1562,11 @@ protected void onDraw(Canvas canvas) {
15671562
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15681563
// If we have a MeasureSpec which allows us to decide our height, try and use the default
15691564
// height
1570-
final int idealHeight = dpToPx(getDefaultHeight()) + getPaddingTop() + getPaddingBottom();
1565+
final int idealHeight =
1566+
(int)
1567+
(ViewUtils.dpToPx(getContext(), getDefaultHeight())
1568+
+ getPaddingTop()
1569+
+ getPaddingBottom());
15711570
switch (MeasureSpec.getMode(heightMeasureSpec)) {
15721571
case MeasureSpec.AT_MOST:
15731572
heightMeasureSpec =
@@ -1588,7 +1587,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
15881587
tabMaxWidth =
15891588
requestedTabMaxWidth > 0
15901589
? requestedTabMaxWidth
1591-
: specWidth - dpToPx(TAB_MIN_WIDTH_MARGIN);
1590+
: (int) (specWidth - ViewUtils.dpToPx(getContext(), TAB_MIN_WIDTH_MARGIN));
15921591
}
15931592

15941593
// Now super measure itself using the (possibly) modified height spec
@@ -2495,7 +2494,7 @@ private void updateTextAndIcon(
24952494
int iconMargin = 0;
24962495
if (hasText && iconView.getVisibility() == VISIBLE) {
24972496
// If we're showing both text and icon, add some margin bottom to the icon
2498-
iconMargin = dpToPx(DEFAULT_GAP_TEXT_ICON);
2497+
iconMargin = (int) ViewUtils.dpToPx(getContext(), DEFAULT_GAP_TEXT_ICON);
24992498
}
25002499
if (inlineLabel) {
25012500
if (iconMargin != MarginLayoutParamsCompat.getMarginEnd(lp)) {
@@ -2653,7 +2652,7 @@ protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec
26532652
return;
26542653
}
26552654

2656-
final int gutter = dpToPx(FIXED_WRAP_GUTTER_MIN);
2655+
final int gutter = (int) ViewUtils.dpToPx(getContext(), FIXED_WRAP_GUTTER_MIN);
26572656
boolean remeasure = false;
26582657

26592658
if (largestTabWidth * count <= getMeasuredWidth() - gutter * 2) {
@@ -2815,9 +2814,10 @@ public void onAnimationEnd(Animator animator) {
28152814
*/
28162815
private void calculateTabViewContentBounds(TabView tabView, RectF contentBounds) {
28172816
int tabViewContentWidth = tabView.getContentWidth();
2817+
int minIndicatorWidth = (int) ViewUtils.dpToPx(getContext(), MIN_INDICATOR_WIDTH);
28182818

2819-
if (tabViewContentWidth < dpToPx(MIN_INDICATOR_WIDTH)) {
2820-
tabViewContentWidth = dpToPx(MIN_INDICATOR_WIDTH);
2819+
if (tabViewContentWidth < minIndicatorWidth) {
2820+
tabViewContentWidth = minIndicatorWidth;
28212821
}
28222822

28232823
int tabViewCenter = (tabView.getLeft() + tabView.getRight()) / 2;

0 commit comments

Comments
 (0)