2015-06-16 13:35:04 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package com.android.launcher3;
|
|
|
|
|
|
|
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.animation.ObjectAnimator;
|
|
|
|
|
import android.content.res.Resources;
|
2016-06-29 09:46:43 -07:00
|
|
|
import android.graphics.Bitmap;
|
2015-06-16 13:35:04 -07:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The fast scroller popup that shows the section name the list will jump to.
|
|
|
|
|
*/
|
|
|
|
|
public class BaseRecyclerViewFastScrollPopup {
|
|
|
|
|
|
|
|
|
|
private static final float FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR = 1.5f;
|
|
|
|
|
|
2016-06-30 13:52:36 -07:00
|
|
|
private static final int SHADOW_INSET = 5;
|
2016-06-29 09:46:43 -07:00
|
|
|
private static final int SHADOW_SHIFT_Y = 4;
|
2016-06-30 13:52:36 -07:00
|
|
|
private static final float SHADOW_ALPHA_MULTIPLIER = 0.5f;
|
2016-06-29 09:46:43 -07:00
|
|
|
|
2015-06-16 13:35:04 -07:00
|
|
|
private Resources mRes;
|
|
|
|
|
private BaseRecyclerView mRv;
|
|
|
|
|
|
2016-06-29 09:46:43 -07:00
|
|
|
private Bitmap mShadow;
|
|
|
|
|
private Paint mShadowPaint;
|
|
|
|
|
|
2015-06-16 13:35:04 -07:00
|
|
|
private Drawable mBg;
|
|
|
|
|
// The absolute bounds of the fast scroller bg
|
|
|
|
|
private Rect mBgBounds = new Rect();
|
|
|
|
|
private int mBgOriginalSize;
|
|
|
|
|
private Rect mInvalidateRect = new Rect();
|
|
|
|
|
private Rect mTmpRect = new Rect();
|
|
|
|
|
|
|
|
|
|
private String mSectionName;
|
|
|
|
|
private Paint mTextPaint;
|
|
|
|
|
private Rect mTextBounds = new Rect();
|
|
|
|
|
private float mAlpha;
|
|
|
|
|
|
|
|
|
|
private Animator mAlphaAnimator;
|
|
|
|
|
private boolean mVisible;
|
|
|
|
|
|
|
|
|
|
public BaseRecyclerViewFastScrollPopup(BaseRecyclerView rv, Resources res) {
|
|
|
|
|
mRes = res;
|
|
|
|
|
mRv = rv;
|
2016-06-29 09:46:43 -07:00
|
|
|
|
2015-06-16 13:35:04 -07:00
|
|
|
mBgOriginalSize = res.getDimensionPixelSize(R.dimen.container_fastscroll_popup_size);
|
2016-06-19 12:49:00 -07:00
|
|
|
mBg = rv.getContext().getDrawable(R.drawable.container_fastscroll_popup_bg);
|
2015-06-16 13:35:04 -07:00
|
|
|
mBg.setBounds(0, 0, mBgOriginalSize, mBgOriginalSize);
|
2016-06-29 09:46:43 -07:00
|
|
|
|
2015-06-16 13:35:04 -07:00
|
|
|
mTextPaint = new Paint();
|
|
|
|
|
mTextPaint.setColor(Color.WHITE);
|
|
|
|
|
mTextPaint.setAntiAlias(true);
|
|
|
|
|
mTextPaint.setTextSize(res.getDimensionPixelSize(R.dimen.container_fastscroll_popup_text_size));
|
2016-06-29 09:46:43 -07:00
|
|
|
|
|
|
|
|
mShadowPaint = new Paint();
|
|
|
|
|
mShadowPaint.setAntiAlias(true);
|
|
|
|
|
mShadowPaint.setFilterBitmap(true);
|
|
|
|
|
mShadowPaint.setDither(true);
|
2015-06-16 13:35:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the section name.
|
|
|
|
|
*/
|
|
|
|
|
public void setSectionName(String sectionName) {
|
|
|
|
|
if (!sectionName.equals(mSectionName)) {
|
|
|
|
|
mSectionName = sectionName;
|
|
|
|
|
mTextPaint.getTextBounds(sectionName, 0, sectionName.length(), mTextBounds);
|
|
|
|
|
// Update the width to use measureText since that is more accurate
|
|
|
|
|
mTextBounds.right = (int) (mTextBounds.left + mTextPaint.measureText(sectionName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the bounds for the fast scroller.
|
2016-06-29 09:46:43 -07:00
|
|
|
*
|
2015-06-16 13:35:04 -07:00
|
|
|
* @return the invalidation rect for this update.
|
|
|
|
|
*/
|
2015-08-21 11:16:27 -07:00
|
|
|
public Rect updateFastScrollerBounds(int lastTouchY) {
|
2015-06-16 13:35:04 -07:00
|
|
|
mInvalidateRect.set(mBgBounds);
|
|
|
|
|
|
|
|
|
|
if (isVisible()) {
|
|
|
|
|
// Calculate the dimensions and position of the fast scroller popup
|
2015-08-21 11:16:27 -07:00
|
|
|
int edgePadding = mRv.getMaxScrollbarWidth();
|
2015-06-16 13:35:04 -07:00
|
|
|
int bgPadding = (mBgOriginalSize - mTextBounds.height()) / 2;
|
|
|
|
|
int bgHeight = mBgOriginalSize;
|
|
|
|
|
int bgWidth = Math.max(mBgOriginalSize, mTextBounds.width() + (2 * bgPadding));
|
|
|
|
|
if (Utilities.isRtl(mRes)) {
|
2015-08-21 11:16:27 -07:00
|
|
|
mBgBounds.left = mRv.getBackgroundPadding().left + (2 * mRv.getMaxScrollbarWidth());
|
2015-06-16 13:35:04 -07:00
|
|
|
mBgBounds.right = mBgBounds.left + bgWidth;
|
|
|
|
|
} else {
|
2015-08-21 11:16:27 -07:00
|
|
|
mBgBounds.right = mRv.getWidth() - mRv.getBackgroundPadding().right -
|
|
|
|
|
(2 * mRv.getMaxScrollbarWidth());
|
2015-06-16 13:35:04 -07:00
|
|
|
mBgBounds.left = mBgBounds.right - bgWidth;
|
|
|
|
|
}
|
|
|
|
|
mBgBounds.top = lastTouchY - (int) (FAST_SCROLL_OVERLAY_Y_OFFSET_FACTOR * bgHeight);
|
|
|
|
|
mBgBounds.top = Math.max(edgePadding,
|
2015-08-21 11:16:27 -07:00
|
|
|
Math.min(mBgBounds.top, mRv.getHeight() - edgePadding - bgHeight));
|
2015-06-16 13:35:04 -07:00
|
|
|
mBgBounds.bottom = mBgBounds.top + bgHeight;
|
2016-06-29 09:46:43 -07:00
|
|
|
|
|
|
|
|
// Generate a bitmap for a shadow matching these bounds
|
|
|
|
|
mShadow = HolographicOutlineHelper.obtain(
|
|
|
|
|
mRv.getContext()).createMediumDropShadow(mBg, false /* shouldCache */);
|
2015-06-16 13:35:04 -07:00
|
|
|
} else {
|
2016-06-29 13:23:24 -07:00
|
|
|
mShadow = null;
|
2015-06-16 13:35:04 -07:00
|
|
|
mBgBounds.setEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Combine the old and new fast scroller bounds to create the full invalidate rect
|
|
|
|
|
mInvalidateRect.union(mBgBounds);
|
|
|
|
|
return mInvalidateRect;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Animates the visibility of the fast scroller popup.
|
|
|
|
|
*/
|
|
|
|
|
public void animateVisibility(boolean visible) {
|
|
|
|
|
if (mVisible != visible) {
|
|
|
|
|
mVisible = visible;
|
|
|
|
|
if (mAlphaAnimator != null) {
|
|
|
|
|
mAlphaAnimator.cancel();
|
|
|
|
|
}
|
|
|
|
|
mAlphaAnimator = ObjectAnimator.ofFloat(this, "alpha", visible ? 1f : 0f);
|
|
|
|
|
mAlphaAnimator.setDuration(visible ? 200 : 150);
|
|
|
|
|
mAlphaAnimator.start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Setter/getter for the popup alpha for animations
|
|
|
|
|
public void setAlpha(float alpha) {
|
|
|
|
|
mAlpha = alpha;
|
|
|
|
|
mRv.invalidate(mBgBounds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getAlpha() {
|
|
|
|
|
return mAlpha;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getHeight() {
|
|
|
|
|
return mBgOriginalSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void draw(Canvas c) {
|
|
|
|
|
if (isVisible()) {
|
2016-06-29 09:46:43 -07:00
|
|
|
// Determine the alpha and prepare the canvas
|
|
|
|
|
final int alpha = (int) (mAlpha * 255);
|
2015-06-16 13:35:04 -07:00
|
|
|
int restoreCount = c.save(Canvas.MATRIX_SAVE_FLAG);
|
|
|
|
|
c.translate(mBgBounds.left, mBgBounds.top);
|
|
|
|
|
mTmpRect.set(mBgBounds);
|
|
|
|
|
mTmpRect.offsetTo(0, 0);
|
2016-06-29 09:46:43 -07:00
|
|
|
|
|
|
|
|
// Expand the rect (with a negative inset), translate it, and draw the shadow
|
|
|
|
|
if (mShadow != null) {
|
|
|
|
|
mTmpRect.inset(-SHADOW_INSET * 2, -SHADOW_INSET * 2);
|
|
|
|
|
mTmpRect.offset(0, SHADOW_SHIFT_Y);
|
|
|
|
|
mShadowPaint.setAlpha((int) (alpha * SHADOW_ALPHA_MULTIPLIER));
|
|
|
|
|
c.drawBitmap(mShadow, null, mTmpRect, mShadowPaint);
|
|
|
|
|
mTmpRect.inset(SHADOW_INSET * 2, SHADOW_INSET * 2);
|
|
|
|
|
mTmpRect.offset(0, -SHADOW_SHIFT_Y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Draw the background
|
2015-06-16 13:35:04 -07:00
|
|
|
mBg.setBounds(mTmpRect);
|
2016-06-29 09:46:43 -07:00
|
|
|
mBg.setAlpha(alpha);
|
2015-06-16 13:35:04 -07:00
|
|
|
mBg.draw(c);
|
2016-06-29 09:46:43 -07:00
|
|
|
|
|
|
|
|
// Draw the text
|
|
|
|
|
mTextPaint.setAlpha(alpha);
|
2015-06-16 13:35:04 -07:00
|
|
|
c.drawText(mSectionName, (mBgBounds.width() - mTextBounds.width()) / 2,
|
2016-06-29 09:46:43 -07:00
|
|
|
mBgBounds.height() - (mBgBounds.height() / 2) - mTextBounds.exactCenterY(),
|
2015-06-16 13:35:04 -07:00
|
|
|
mTextPaint);
|
|
|
|
|
c.restoreToCount(restoreCount);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isVisible() {
|
|
|
|
|
return (mAlpha > 0f) && (mSectionName != null);
|
|
|
|
|
}
|
|
|
|
|
}
|