Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1119)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/CardViewHolder.java

Issue 2638353002: Update and unify nine-patch files for content suggestion and omnibox cards (Closed)
Patch Set: sync Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/CardViewHolder.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/CardViewHolder.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/CardViewHolder.java
index 05a102fe16ae14529b25f788ebd1a2575caf5364..4eff2c8841524821a5facb40ce6f55c406fb36ba 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/CardViewHolder.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/CardViewHolder.java
@@ -4,6 +4,7 @@
package org.chromium.chrome.browser.ntp.cards;
+import android.graphics.Rect;
import android.support.annotation.CallSuper;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
@@ -18,6 +19,7 @@ import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
+import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ntp.ContextMenuManager;
import org.chromium.chrome.browser.ntp.UiConfig;
@@ -48,11 +50,16 @@ public abstract class CardViewHolder extends NewTabPageViewHolder {
private final int mMaxPeekPadding;
/**
- * Due to the card background being a 9 patch file - the card border shadow will be part of
- * the card width and height. This value will be used to adjust values to account for the
- * borders.
+ * The card shadow is part of the drawable nine-patch and not drawn via setElevation(),
+ * so it is included in the height and width of the drawable. This member contains the
+ * dimensions of the shadow (from the drawable's padding), so it can be used to offset the
+ * position in calculations.
*/
- private final int mCards9PatchAdjustment;
+ private final Rect mCardShadow = new Rect();
+
+ private final int mCardGap;
+
+ private final int mDefaultLateralMargin;
protected final NewTabPageRecyclerView mRecyclerView;
@@ -78,8 +85,10 @@ public abstract class CardViewHolder extends NewTabPageViewHolder {
UiConfig uiConfig, final ContextMenuManager contextMenuManager) {
super(inflateView(layoutId, recyclerView));
- mCards9PatchAdjustment = recyclerView.getResources().getDimensionPixelSize(
- R.dimen.snippets_card_9_patch_adjustment);
+ ApiCompatibilityUtils.getDrawable(recyclerView.getResources(), R.drawable.card_single)
+ .getPadding(mCardShadow);
+
+ mCardGap = recyclerView.getResources().getDimensionPixelSize(R.dimen.snippets_card_gap);
mMaxPeekPadding = recyclerView.getResources().getDimensionPixelSize(
R.dimen.snippets_padding);
@@ -115,7 +124,11 @@ public abstract class CardViewHolder extends NewTabPageViewHolder {
// Configure the resizer to use negative margins on regular display to balance out the
// lateral shadow of the card 9-patch and avoid a rounded corner effect.
- mMarginResizer.setMargins(-mCards9PatchAdjustment);
+ int cardCornerRadius = recyclerView.getResources().getDimensionPixelSize(
+ R.dimen.snippets_card_corner_radius);
+ assert mCardShadow.left == mCardShadow.right;
+ mDefaultLateralMargin = -(mCardShadow.left + cardCornerRadius);
+ mMarginResizer.setMargins(mDefaultLateralMargin);
}
/**
@@ -176,7 +189,11 @@ public abstract class CardViewHolder extends NewTabPageViewHolder {
hasCardBelow = isCard(belowViewType) && belowViewType != ItemViewType.PROMO;
}
- getParams().bottomMargin = hasCardBelow ? -mCards9PatchAdjustment : 0;
+ // By default the apparent distance between two cards is the sum of the bottom and top
+ // height of their shadows. We want |mCardGap| instead, so we set the bottom margin to
+ // the difference.
+ getParams().bottomMargin =
+ hasCardBelow ? (mCardGap - (mCardShadow.top + mCardShadow.bottom)) : 0;
@DrawableRes
int selectedBackground = selectBackground(hasCardAbove, hasCardBelow);
@@ -247,9 +264,8 @@ public abstract class CardViewHolder extends NewTabPageViewHolder {
}
itemView.setPadding(lateralPadding, mMaxPeekPadding, lateralPadding, mMaxPeekPadding);
- // Adjust the margins by |mCards9PatchAdjustment| so the card width
- // is the actual width not including the elevation shadow, so we can have full bleed.
- mMarginResizer.setMargins(mMaxPeekPadding - (peekPadding + mCards9PatchAdjustment));
+ // Adjust the margins. The shadow width is offset via the default lateral margin.
+ mMarginResizer.setMargins(mDefaultLateralMargin + mMaxPeekPadding - peekPadding);
// Set the opacity of the card content to be 0 when peeking and 1 when full width.
int itemViewChildCount = ((ViewGroup) itemView).getChildCount();
@@ -285,10 +301,10 @@ public abstract class CardViewHolder extends NewTabPageViewHolder {
@DrawableRes
protected int selectBackground(boolean hasCardAbove, boolean hasCardBelow) {
- if (hasCardAbove && hasCardBelow) return R.drawable.ntp_card_middle;
- if (!hasCardAbove && hasCardBelow) return R.drawable.ntp_card_top;
- if (hasCardAbove && !hasCardBelow) return R.drawable.ntp_card_bottom;
- return R.drawable.ntp_card_single;
+ if (hasCardAbove && hasCardBelow) return R.drawable.card_middle;
+ if (!hasCardAbove && hasCardBelow) return R.drawable.card_top;
+ if (hasCardAbove && !hasCardBelow) return R.drawable.card_bottom;
+ return R.drawable.card_single;
}
protected NewTabPageRecyclerView getRecyclerView() {

Powered by Google App Engine
This is Rietveld 408576698