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

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/widget/FadingEdgeScrollView.java

Issue 2761583002: Make FadingEdgeScrollView more generic (Closed)
Patch Set: Comments, added hard edge for payments Created 3 years, 9 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/widget/FadingEdgeScrollView.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/widget/FadingEdgeScrollView.java b/chrome/android/java/src/org/chromium/chrome/browser/widget/FadingEdgeScrollView.java
new file mode 100644
index 0000000000000000000000000000000000000000..ef797b3840ef60f5d8f52c617d3127c7b67bce83
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/widget/FadingEdgeScrollView.java
@@ -0,0 +1,103 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.widget;
+
+import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.util.AttributeSet;
+import android.widget.ScrollView;
+
+import org.chromium.base.ApiCompatibilityUtils;
+import org.chromium.chrome.R;
+
+/**
+ * An extension of the ScrollView that supports edge boundaries coming in.
+ */
+public class FadingEdgeScrollView extends ScrollView {
+ public static final int DRAW_NO_EDGE = 0;
Ted C 2017/03/20 19:10:31 I would one-liner javadoc these and IntDef'em.
gone 2017/03/20 20:06:19 Done.
+ public static final int DRAW_FADING_EDGE = 1;
+ public static final int DRAW_HARD_EDGE = 2;
+
+ private static final int POSITION_TOP = 0;
+ private static final int POSITION_BOTTOM = 1;
+
+ private final Paint mSeparatorPaint = new Paint();
+ private final int mSeparatorColor;
+ private final int mSeparatorHeight;
+
+ private int mDrawTopEdge = DRAW_FADING_EDGE;
+ private int mDrawBottomEdge = DRAW_FADING_EDGE;
+
+ public FadingEdgeScrollView(Context context, AttributeSet attrs) {
+ super(context, attrs);
+
+ mSeparatorColor =
+ ApiCompatibilityUtils.getColor(getResources(), R.color.toolbar_shadow_color);
+ mSeparatorHeight = getResources().getDimensionPixelSize(R.dimen.separator_height);
+ }
+
+ @Override
+ protected void dispatchDraw(Canvas canvas) {
+ super.dispatchDraw(canvas);
+ setVerticalFadingEdgeEnabled(true);
+ float topEdgeStrength = getTopFadingEdgeStrength();
+ float bottomEdgeStrength = getBottomFadingEdgeStrength();
+ setVerticalFadingEdgeEnabled(false);
+
+ drawBoundaryLine(canvas, POSITION_TOP, topEdgeStrength, mDrawTopEdge);
+ drawBoundaryLine(canvas, POSITION_BOTTOM, bottomEdgeStrength, mDrawBottomEdge);
+ }
+
+ /**
+ * Sets which edge should be drawn.
+ * @param topEdgeState Whether to draw the edge on the top part of the view.
+ * @param bottomEdgeState Whether to draw the edge on the bottom part of the view.
+ */
+ public void setEdgeVisibility(int topEdgeState, int bottomEdgeState) {
+ mDrawTopEdge = topEdgeState;
+ mDrawBottomEdge = bottomEdgeState;
+ invalidate();
+ }
+
+ /**
+ * Draws a line at the top or bottom of the view. This should be called from dispatchDraw() so
+ * it gets drawn on top of the View's children.
+ *
+ * @param canvas The canvas on which to draw.
+ * @param position Where to draw the line: either POSITION_TOP or POSITION_BOTTOM.
+ * @param edgeStrength A value between 0 and 1 indicating the relative size of the line. 0
+ * means no line at all. 1 means a fully opaque line.
+ * @param edgeState How to draw the line.
+ */
+ private void drawBoundaryLine(Canvas canvas, int position, float edgeStrength, int edgeState) {
+ if (edgeState == DRAW_NO_EDGE) {
+ return;
+ } else if (edgeState == DRAW_FADING_EDGE) {
+ edgeStrength = Math.max(0.0f, Math.min(1.0f, edgeStrength));
+ } else {
+ edgeStrength = 1.0f;
+ }
+ if (edgeStrength <= 0.0f) return;
+
+ int adjustedA = (int) (Color.alpha(mSeparatorColor) * edgeStrength);
+ int adjustedR = (int) (Color.red(mSeparatorColor) * edgeStrength);
+ int adjustedG = (int) (Color.green(mSeparatorColor) * edgeStrength);
+ int adjustedB = (int) (Color.blue(mSeparatorColor) * edgeStrength);
+ mSeparatorPaint.setColor(Color.argb(adjustedA, adjustedR, adjustedG, adjustedB));
+
+ int left = getScrollX();
+ int right = left + getRight();
+
+ if (position == POSITION_BOTTOM) {
+ int bottom = getScrollY() + getBottom() - getTop();
+ canvas.drawRect(left, bottom - mSeparatorHeight, right, bottom, mSeparatorPaint);
+ } else if (position == POSITION_TOP) {
+ int top = getScrollY();
+ canvas.drawRect(left, top, right, top + mSeparatorHeight, mSeparatorPaint);
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698