blob: b9e627775680d5d7a21fd5218a9d539f3cb66ccc [file] [log] [blame]
Winson Chungb1777442015-06-16 13:35:04 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.launcher3;
17
18import android.animation.Animator;
19import android.animation.ObjectAnimator;
20import android.content.res.Resources;
Peter Schiller1d62b8a2016-06-29 09:46:43 -070021import android.graphics.Bitmap;
Winson Chungb1777442015-06-16 13:35:04 -070022import android.graphics.Canvas;
23import android.graphics.Color;
24import android.graphics.Paint;
25import android.graphics.Rect;
26import android.graphics.drawable.Drawable;
27
28/**
29 * The fast scroller popup that shows the section name the list will jump to.
30 */
31public class BaseRecyclerViewFastScrollPopup {
32
33 private static final float FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR = 1.5f;
34
Peter Schiller62d70232016-07-28 16:58:06 -070035 private static final int SHADOW_INSET = 3;
36 private static final int SHADOW_SHIFT_Y = 2;
37 private static final float SHADOW_ALPHA_MULTIPLIER = 0.67f;
Peter Schiller1d62b8a2016-06-29 09:46:43 -070038
Winson Chungb1777442015-06-16 13:35:04 -070039 private Resources mRes;
40 private BaseRecyclerView mRv;
41
Peter Schiller1d62b8a2016-06-29 09:46:43 -070042 private Bitmap mShadow;
43 private Paint mShadowPaint;
44
Winson Chungb1777442015-06-16 13:35:04 -070045 private Drawable mBg;
46 // The absolute bounds of the fast scroller bg
47 private Rect mBgBounds = new Rect();
48 private int mBgOriginalSize;
49 private Rect mInvalidateRect = new Rect();
50 private Rect mTmpRect = new Rect();
51
52 private String mSectionName;
53 private Paint mTextPaint;
54 private Rect mTextBounds = new Rect();
55 private float mAlpha;
56
57 private Animator mAlphaAnimator;
58 private boolean mVisible;
59
60 public BaseRecyclerViewFastScrollPopup(BaseRecyclerView rv, Resources res) {
61 mRes = res;
62 mRv = rv;
Peter Schiller1d62b8a2016-06-29 09:46:43 -070063
Winson Chungb1777442015-06-16 13:35:04 -070064 mBgOriginalSize = res.getDimensionPixelSize(R.dimen.container_fastscroll_popup_size);
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070065 mBg = rv.getContext().getDrawable(R.drawable.container_fastscroll_popup_bg);
Winson Chungb1777442015-06-16 13:35:04 -070066 mBg.setBounds(0, 0, mBgOriginalSize, mBgOriginalSize);
Peter Schiller1d62b8a2016-06-29 09:46:43 -070067
Winson Chungb1777442015-06-16 13:35:04 -070068 mTextPaint = new Paint();
69 mTextPaint.setColor(Color.WHITE);
70 mTextPaint.setAntiAlias(true);
71 mTextPaint.setTextSize(res.getDimensionPixelSize(R.dimen.container_fastscroll_popup_text_size));
Peter Schiller1d62b8a2016-06-29 09:46:43 -070072
73 mShadowPaint = new Paint();
74 mShadowPaint.setAntiAlias(true);
75 mShadowPaint.setFilterBitmap(true);
76 mShadowPaint.setDither(true);
Winson Chungb1777442015-06-16 13:35:04 -070077 }
78
79 /**
80 * Sets the section name.
81 */
82 public void setSectionName(String sectionName) {
83 if (!sectionName.equals(mSectionName)) {
84 mSectionName = sectionName;
85 mTextPaint.getTextBounds(sectionName, 0, sectionName.length(), mTextBounds);
86 // Update the width to use measureText since that is more accurate
87 mTextBounds.right = (int) (mTextBounds.left + mTextPaint.measureText(sectionName));
88 }
89 }
90
91 /**
92 * Updates the bounds for the fast scroller.
Peter Schiller1d62b8a2016-06-29 09:46:43 -070093 *
Winson Chungb1777442015-06-16 13:35:04 -070094 * @return the invalidation rect for this update.
95 */
Winsonc0880492015-08-21 11:16:27 -070096 public Rect updateFastScrollerBounds(int lastTouchY) {
Winson Chungb1777442015-06-16 13:35:04 -070097 mInvalidateRect.set(mBgBounds);
98
99 if (isVisible()) {
100 // Calculate the dimensions and position of the fast scroller popup
Winsonc0880492015-08-21 11:16:27 -0700101 int edgePadding = mRv.getMaxScrollbarWidth();
Winson Chungb1777442015-06-16 13:35:04 -0700102 int bgPadding = (mBgOriginalSize - mTextBounds.height()) / 2;
103 int bgHeight = mBgOriginalSize;
104 int bgWidth = Math.max(mBgOriginalSize, mTextBounds.width() + (2 * bgPadding));
105 if (Utilities.isRtl(mRes)) {
Winsonc0880492015-08-21 11:16:27 -0700106 mBgBounds.left = mRv.getBackgroundPadding().left + (2 * mRv.getMaxScrollbarWidth());
Winson Chungb1777442015-06-16 13:35:04 -0700107 mBgBounds.right = mBgBounds.left + bgWidth;
108 } else {
Winsonc0880492015-08-21 11:16:27 -0700109 mBgBounds.right = mRv.getWidth() - mRv.getBackgroundPadding().right -
110 (2 * mRv.getMaxScrollbarWidth());
Winson Chungb1777442015-06-16 13:35:04 -0700111 mBgBounds.left = mBgBounds.right - bgWidth;
112 }
113 mBgBounds.top = lastTouchY - (int) (FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR * bgHeight);
114 mBgBounds.top = Math.max(edgePadding,
Hyunyoung Song4ebc3d02016-08-05 10:59:17 -0700115 Math.min(mBgBounds.top, mRv.getVisibleHeight() - edgePadding - bgHeight));
Winson Chungb1777442015-06-16 13:35:04 -0700116 mBgBounds.bottom = mBgBounds.top + bgHeight;
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700117
118 // Generate a bitmap for a shadow matching these bounds
119 mShadow = HolographicOutlineHelper.obtain(
120 mRv.getContext()).createMediumDropShadow(mBg, false /* shouldCache */);
Winson Chungb1777442015-06-16 13:35:04 -0700121 } else {
Peter Schillerf9507122016-06-29 13:23:24 -0700122 mShadow = null;
Winson Chungb1777442015-06-16 13:35:04 -0700123 mBgBounds.setEmpty();
124 }
125
126 // Combine the old and new fast scroller bounds to create the full invalidate rect
127 mInvalidateRect.union(mBgBounds);
128 return mInvalidateRect;
129 }
130
131 /**
132 * Animates the visibility of the fast scroller popup.
133 */
134 public void animateVisibility(boolean visible) {
135 if (mVisible != visible) {
136 mVisible = visible;
137 if (mAlphaAnimator != null) {
138 mAlphaAnimator.cancel();
139 }
140 mAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", visible ? 1f : 0f);
141 mAlphaAnimator.setDuration(visible ? 200 : 150);
142 mAlphaAnimator.start();
143 }
144 }
145
146 // Setter/getter for the popup alpha for animations
147 public void setAlpha(float alpha) {
148 mAlpha = alpha;
149 mRv.invalidate(mBgBounds);
150 }
151
152 public float getAlpha() {
153 return mAlpha;
154 }
155
156 public int getHeight() {
157 return mBgOriginalSize;
158 }
159
160 public void draw(Canvas c) {
161 if (isVisible()) {
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700162 // Determine the alpha and prepare the canvas
163 final int alpha = (int) (mAlpha * 255);
Winson Chungb1777442015-06-16 13:35:04 -0700164 int restoreCount = c.save(Canvas.MATRIX_SAVE_FLAG);
165 c.translate(mBgBounds.left, mBgBounds.top);
166 mTmpRect.set(mBgBounds);
167 mTmpRect.offsetTo(0, 0);
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700168
169 // Expand the rect (with a negative inset), translate it, and draw the shadow
170 if (mShadow != null) {
171 mTmpRect.inset(-SHADOW_INSET * 2, -SHADOW_INSET * 2);
172 mTmpRect.offset(0, SHADOW_SHIFT_Y);
173 mShadowPaint.setAlpha((int) (alpha * SHADOW_ALPHA_MULTIPLIER));
174 c.drawBitmap(mShadow, null, mTmpRect, mShadowPaint);
175 mTmpRect.inset(SHADOW_INSET * 2, SHADOW_INSET * 2);
176 mTmpRect.offset(0, -SHADOW_SHIFT_Y);
177 }
178
179 // Draw the background
Winson Chungb1777442015-06-16 13:35:04 -0700180 mBg.setBounds(mTmpRect);
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700181 mBg.setAlpha(alpha);
Winson Chungb1777442015-06-16 13:35:04 -0700182 mBg.draw(c);
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700183
184 // Draw the text
185 mTextPaint.setAlpha(alpha);
Winson Chungb1777442015-06-16 13:35:04 -0700186 c.drawText(mSectionName, (mBgBounds.width() - mTextBounds.width()) / 2,
Peter Schiller1d62b8a2016-06-29 09:46:43 -0700187 mBgBounds.height() - (mBgBounds.height() / 2) - mTextBounds.exactCenterY(),
Winson Chungb1777442015-06-16 13:35:04 -0700188 mTextPaint);
189 c.restoreToCount(restoreCount);
190 }
191 }
192
193 public boolean isVisible() {
194 return (mAlpha > 0f) && (mSectionName != null);
195 }
196}