michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.gfx; michael@0: michael@0: import android.content.Context; michael@0: import android.graphics.Canvas; michael@0: import android.os.Build; michael@0: import android.widget.EdgeEffect; michael@0: import android.view.View; michael@0: michael@0: michael@0: public class OverscrollEdgeEffect implements Overscroll { michael@0: // Used to index particular edges in the edges array michael@0: private static final int TOP = 0; michael@0: private static final int BOTTOM = 1; michael@0: private static final int LEFT = 2; michael@0: private static final int RIGHT = 3; michael@0: michael@0: // All four edges of the screen michael@0: private final EdgeEffect[] mEdges = new EdgeEffect[4]; michael@0: michael@0: // The view we're showing this overscroll on. michael@0: private final View mView; michael@0: michael@0: public OverscrollEdgeEffect(final View v) { michael@0: mView = v; michael@0: Context context = v.getContext(); michael@0: for (int i = 0; i < 4; i++) { michael@0: mEdges[i] = new EdgeEffect(context); michael@0: } michael@0: } michael@0: michael@0: public void setSize(final int width, final int height) { michael@0: mEdges[LEFT].setSize(height, width); michael@0: mEdges[RIGHT].setSize(height, width); michael@0: mEdges[TOP].setSize(width, height); michael@0: mEdges[BOTTOM].setSize(width, height); michael@0: } michael@0: michael@0: private EdgeEffect getEdgeForAxisAndSide(final Axis axis, final float side) { michael@0: if (axis == Axis.Y) { michael@0: if (side < 0) { michael@0: return mEdges[TOP]; michael@0: } else { michael@0: return mEdges[BOTTOM]; michael@0: } michael@0: } else { michael@0: if (side < 0) { michael@0: return mEdges[LEFT]; michael@0: } else { michael@0: return mEdges[RIGHT]; michael@0: } michael@0: } michael@0: } michael@0: michael@0: private void invalidate() { michael@0: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { michael@0: mView.postInvalidateOnAnimation(); michael@0: } else { michael@0: mView.postInvalidateDelayed(10); michael@0: } michael@0: } michael@0: michael@0: public void setVelocity(final float velocity, final Axis axis) { michael@0: final EdgeEffect edge = getEdgeForAxisAndSide(axis, velocity); michael@0: michael@0: // If we're showing overscroll already, start fading it out. michael@0: if (!edge.isFinished()) { michael@0: edge.onRelease(); michael@0: } else { michael@0: // Otherwise, show an absorb effect michael@0: edge.onAbsorb((int)velocity); michael@0: } michael@0: michael@0: invalidate(); michael@0: } michael@0: michael@0: public void setDistance(final float distance, final Axis axis) { michael@0: // The first overscroll event often has zero distance. Throw it out michael@0: if (distance == 0.0f) { michael@0: return; michael@0: } michael@0: michael@0: final EdgeEffect edge = getEdgeForAxisAndSide(axis, (int)distance); michael@0: edge.onPull(distance / (axis == Axis.X ? mView.getWidth() : mView.getHeight())); michael@0: invalidate(); michael@0: } michael@0: michael@0: public void draw(final Canvas canvas, final ImmutableViewportMetrics metrics) { michael@0: if (metrics == null) { michael@0: return; michael@0: } michael@0: michael@0: // If we're pulling an edge, or fading it out, draw! michael@0: boolean invalidate = false; michael@0: if (!mEdges[TOP].isFinished()) { michael@0: invalidate |= draw(mEdges[TOP], canvas, metrics.marginLeft, metrics.marginTop, 0); michael@0: } michael@0: michael@0: if (!mEdges[BOTTOM].isFinished()) { michael@0: invalidate |= draw(mEdges[BOTTOM], canvas, mView.getWidth(), mView.getHeight(), 180); michael@0: } michael@0: michael@0: if (!mEdges[LEFT].isFinished()) { michael@0: invalidate |= draw(mEdges[LEFT], canvas, metrics.marginLeft, mView.getHeight(), 270); michael@0: } michael@0: michael@0: if (!mEdges[RIGHT].isFinished()) { michael@0: invalidate |= draw(mEdges[RIGHT], canvas, mView.getWidth(), metrics.marginTop, 90); michael@0: } michael@0: michael@0: // If the edge effect is animating off screen, invalidate. michael@0: if (invalidate) { michael@0: invalidate(); michael@0: } michael@0: } michael@0: michael@0: public boolean draw(final EdgeEffect edge, final Canvas canvas, final float translateX, final float translateY, final float rotation) { michael@0: final int state = canvas.save(); michael@0: canvas.translate(translateX, translateY); michael@0: canvas.rotate(rotation); michael@0: boolean invalidate = edge.draw(canvas); michael@0: canvas.restoreToCount(state); michael@0: michael@0: return invalidate; michael@0: } michael@0: }