Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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.widget; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.graphics.Canvas; | |
| 9 import android.graphics.Color; | |
| 10 import android.graphics.Paint; | |
| 11 import android.util.AttributeSet; | |
| 12 import android.widget.ScrollView; | |
| 13 | |
| 14 import org.chromium.base.ApiCompatibilityUtils; | |
| 15 import org.chromium.chrome.R; | |
| 16 | |
| 17 /** | |
| 18 * An extension of the ScrollView that supports edge boundaries coming in. | |
| 19 */ | |
| 20 public class FadingEdgeScrollView extends ScrollView { | |
| 21 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.
| |
| 22 public static final int DRAW_FADING_EDGE = 1; | |
| 23 public static final int DRAW_HARD_EDGE = 2; | |
| 24 | |
| 25 private static final int POSITION_TOP = 0; | |
| 26 private static final int POSITION_BOTTOM = 1; | |
| 27 | |
| 28 private final Paint mSeparatorPaint = new Paint(); | |
| 29 private final int mSeparatorColor; | |
| 30 private final int mSeparatorHeight; | |
| 31 | |
| 32 private int mDrawTopEdge = DRAW_FADING_EDGE; | |
| 33 private int mDrawBottomEdge = DRAW_FADING_EDGE; | |
| 34 | |
| 35 public FadingEdgeScrollView(Context context, AttributeSet attrs) { | |
| 36 super(context, attrs); | |
| 37 | |
| 38 mSeparatorColor = | |
| 39 ApiCompatibilityUtils.getColor(getResources(), R.color.toolbar_s hadow_color); | |
| 40 mSeparatorHeight = getResources().getDimensionPixelSize(R.dimen.separato r_height); | |
| 41 } | |
| 42 | |
| 43 @Override | |
| 44 protected void dispatchDraw(Canvas canvas) { | |
| 45 super.dispatchDraw(canvas); | |
| 46 setVerticalFadingEdgeEnabled(true); | |
| 47 float topEdgeStrength = getTopFadingEdgeStrength(); | |
| 48 float bottomEdgeStrength = getBottomFadingEdgeStrength(); | |
| 49 setVerticalFadingEdgeEnabled(false); | |
| 50 | |
| 51 drawBoundaryLine(canvas, POSITION_TOP, topEdgeStrength, mDrawTopEdge); | |
| 52 drawBoundaryLine(canvas, POSITION_BOTTOM, bottomEdgeStrength, mDrawBotto mEdge); | |
| 53 } | |
| 54 | |
| 55 /** | |
| 56 * Sets which edge should be drawn. | |
| 57 * @param topEdgeState Whether to draw the edge on the top part of the vi ew. | |
| 58 * @param bottomEdgeState Whether to draw the edge on the bottom part of the view. | |
| 59 */ | |
| 60 public void setEdgeVisibility(int topEdgeState, int bottomEdgeState) { | |
| 61 mDrawTopEdge = topEdgeState; | |
| 62 mDrawBottomEdge = bottomEdgeState; | |
| 63 invalidate(); | |
| 64 } | |
| 65 | |
| 66 /** | |
| 67 * Draws a line at the top or bottom of the view. This should be called from dispatchDraw() so | |
| 68 * it gets drawn on top of the View's children. | |
| 69 * | |
| 70 * @param canvas The canvas on which to draw. | |
| 71 * @param position Where to draw the line: either POSITION_TOP or POSITI ON_BOTTOM. | |
| 72 * @param edgeStrength A value between 0 and 1 indicating the relative size of the line. 0 | |
| 73 * means no line at all. 1 means a fully opaque line. | |
| 74 * @param edgeState How to draw the line. | |
| 75 */ | |
| 76 private void drawBoundaryLine(Canvas canvas, int position, float edgeStrengt h, int edgeState) { | |
| 77 if (edgeState == DRAW_NO_EDGE) { | |
| 78 return; | |
| 79 } else if (edgeState == DRAW_FADING_EDGE) { | |
| 80 edgeStrength = Math.max(0.0f, Math.min(1.0f, edgeStrength)); | |
| 81 } else { | |
| 82 edgeStrength = 1.0f; | |
| 83 } | |
| 84 if (edgeStrength <= 0.0f) return; | |
| 85 | |
| 86 int adjustedA = (int) (Color.alpha(mSeparatorColor) * edgeStrength); | |
| 87 int adjustedR = (int) (Color.red(mSeparatorColor) * edgeStrength); | |
| 88 int adjustedG = (int) (Color.green(mSeparatorColor) * edgeStrength); | |
| 89 int adjustedB = (int) (Color.blue(mSeparatorColor) * edgeStrength); | |
| 90 mSeparatorPaint.setColor(Color.argb(adjustedA, adjustedR, adjustedG, adj ustedB)); | |
| 91 | |
| 92 int left = getScrollX(); | |
| 93 int right = left + getRight(); | |
| 94 | |
| 95 if (position == POSITION_BOTTOM) { | |
| 96 int bottom = getScrollY() + getBottom() - getTop(); | |
| 97 canvas.drawRect(left, bottom - mSeparatorHeight, right, bottom, mSep aratorPaint); | |
| 98 } else if (position == POSITION_TOP) { | |
| 99 int top = getScrollY(); | |
| 100 canvas.drawRect(left, top, right, top + mSeparatorHeight, mSeparator Paint); | |
| 101 } | |
| 102 } | |
| 103 } | |
| OLD | NEW |