OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.ntp; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.content.res.Resources; |
| 9 import android.util.AttributeSet; |
| 10 import android.view.View; |
| 11 import android.widget.FrameLayout; |
| 12 |
| 13 import com.google.android.apps.chrome.R; |
| 14 |
| 15 import org.chromium.base.ApiCompatibilityUtils; |
| 16 import org.chromium.chrome.browser.util.MathUtils; |
| 17 |
| 18 /** |
| 19 * A layout that arranges most visited items in a grid. |
| 20 * |
| 21 * Intended for use with the new icon-based most visited items. |
| 22 */ |
| 23 public class IconMostVisitedLayout extends FrameLayout { |
| 24 |
| 25 private static final int MAX_COLUMNS = 4; |
| 26 |
| 27 private int mVerticalSpacing; |
| 28 private int mMinHorizontalSpacing; |
| 29 private int mMaxHorizontalSpacing; |
| 30 private int mMaxWidth; |
| 31 |
| 32 /** |
| 33 * @param context The view context in which this item will be shown. |
| 34 * @param attrs The attributes of the XML tag that is inflating the view. |
| 35 */ |
| 36 public IconMostVisitedLayout(Context context, AttributeSet attrs) { |
| 37 super(context, attrs); |
| 38 |
| 39 Resources res = getResources(); |
| 40 mVerticalSpacing = res.getDimensionPixelOffset(R.dimen.icon_most_visited
_vertical_spacing); |
| 41 mMinHorizontalSpacing = res.getDimensionPixelOffset( |
| 42 R.dimen.icon_most_visited_min_horizontal_spacing); |
| 43 mMaxHorizontalSpacing = res.getDimensionPixelOffset( |
| 44 R.dimen.icon_most_visited_max_horizontal_spacing); |
| 45 mMaxWidth = res.getDimensionPixelOffset(R.dimen.icon_most_visited_layout
_max_width); |
| 46 } |
| 47 |
| 48 @Override |
| 49 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 50 int totalWidth = resolveSize(mMaxWidth, widthMeasureSpec); |
| 51 int childCount = getChildCount(); |
| 52 if (childCount == 0) { |
| 53 setMeasuredDimension(totalWidth, resolveSize(0, heightMeasureSpec)); |
| 54 return; |
| 55 } |
| 56 |
| 57 // Measure the children. |
| 58 for (int i = 0; i < childCount; i++) { |
| 59 measureChild(getChildAt(i), MeasureSpec.UNSPECIFIED, MeasureSpec.UNS
PECIFIED); |
| 60 } |
| 61 |
| 62 // Determine the number of columns that will fit. |
| 63 int gridWidth = totalWidth - ApiCompatibilityUtils.getPaddingStart(this) |
| 64 - ApiCompatibilityUtils.getPaddingEnd(this); |
| 65 int childHeight = getChildAt(0).getMeasuredHeight(); |
| 66 int childWidth = getChildAt(0).getMeasuredWidth(); |
| 67 int numColumns = MathUtils.clamp( |
| 68 (gridWidth + mMinHorizontalSpacing) / (childWidth + mMinHorizont
alSpacing), |
| 69 1, MAX_COLUMNS); |
| 70 |
| 71 // Ensure column spacing isn't greater than mMaxHorizontalSpacing. |
| 72 int gridWidthMinusColumns = Math.max(0, gridWidth - numColumns * childWi
dth); |
| 73 int gridSidePadding = gridWidthMinusColumns - mMaxHorizontalSpacing * (n
umColumns - 1); |
| 74 |
| 75 int gridStart = 0; |
| 76 float horizontalSpacing; |
| 77 if (gridSidePadding > 0) { |
| 78 horizontalSpacing = mMaxHorizontalSpacing; |
| 79 gridStart = gridSidePadding / 2; |
| 80 } else { |
| 81 horizontalSpacing = (float) gridWidthMinusColumns / Math.max(1, numC
olumns - 1); |
| 82 } |
| 83 |
| 84 // Arrange the children in a grid. |
| 85 int paddingTop = getPaddingTop(); |
| 86 boolean isRtl = ApiCompatibilityUtils.isLayoutRtl(this); |
| 87 for (int i = 0; i < childCount; i++) { |
| 88 View child = getChildAt(i); |
| 89 int row = i / numColumns; |
| 90 int column = i % numColumns; |
| 91 int childTop = row * (childHeight + mVerticalSpacing); |
| 92 int childStart = gridStart + Math.round(column * (childWidth + horiz
ontalSpacing)); |
| 93 MarginLayoutParams layoutParams = (MarginLayoutParams) child.getLayo
utParams(); |
| 94 layoutParams.setMargins(isRtl ? 0 : childStart, childTop, isRtl ? ch
ildStart : 0, 0); |
| 95 child.setLayoutParams(layoutParams); |
| 96 } |
| 97 |
| 98 int numRows = (childCount + numColumns - 1) / numColumns; |
| 99 int totalHeight = paddingTop + getPaddingBottom() + numRows * childHeigh
t |
| 100 + (numRows - 1) * mVerticalSpacing; |
| 101 |
| 102 setMeasuredDimension(totalWidth, resolveSize(totalHeight, heightMeasureS
pec)); |
| 103 } |
| 104 } |
OLD | NEW |