mobile/android/base/ScrollAnimator.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko;
michael@0 7
michael@0 8 import java.util.Timer;
michael@0 9 import java.util.TimerTask;
michael@0 10
michael@0 11 import org.mozilla.gecko.util.GamepadUtils;
michael@0 12
michael@0 13 import android.view.InputDevice;
michael@0 14 import android.view.MotionEvent;
michael@0 15 import android.view.View;
michael@0 16
michael@0 17 public class ScrollAnimator implements View.OnGenericMotionListener {
michael@0 18 private Timer mScrollTimer;
michael@0 19 private int mX;
michael@0 20 private int mY;
michael@0 21
michael@0 22 // Assuming 60fps, this will make the view scroll once per frame
michael@0 23 static final long MS_PER_FRAME = 1000 / 60;
michael@0 24
michael@0 25 // Maximum number of pixels that can be scrolled per frame
michael@0 26 static final float MAX_SCROLL = 0.075f * GeckoAppShell.getDpi();
michael@0 27
michael@0 28 private class ScrollRunnable extends TimerTask {
michael@0 29 private View mView;
michael@0 30
michael@0 31 public ScrollRunnable(View view) {
michael@0 32 mView = view;
michael@0 33 }
michael@0 34
michael@0 35 @Override
michael@0 36 public final void run() {
michael@0 37 mView.scrollBy(mX, mY);
michael@0 38 }
michael@0 39 }
michael@0 40
michael@0 41 @Override
michael@0 42 public boolean onGenericMotion(View view, MotionEvent event) {
michael@0 43 if ((event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
michael@0 44 switch (event.getAction()) {
michael@0 45 case MotionEvent.ACTION_MOVE:
michael@0 46 // Cancel the animation if the joystick is in a neutral position
michael@0 47 if (GamepadUtils.isValueInDeadZone(event, MotionEvent.AXIS_X) &&
michael@0 48 GamepadUtils.isValueInDeadZone(event, MotionEvent.AXIS_Y)) {
michael@0 49 if (mScrollTimer != null) {
michael@0 50 mScrollTimer.cancel();
michael@0 51 mScrollTimer = null;
michael@0 52 }
michael@0 53 return true;
michael@0 54 }
michael@0 55
michael@0 56 // Scroll with a velocity relative to the screen DPI
michael@0 57 mX = (int) (event.getAxisValue(MotionEvent.AXIS_X) * MAX_SCROLL);
michael@0 58 mY = (int) (event.getAxisValue(MotionEvent.AXIS_Y) * MAX_SCROLL);
michael@0 59
michael@0 60 // Start the timer; the view will continue to scroll as long as
michael@0 61 // the joystick is not in the deadzone.
michael@0 62 if (mScrollTimer == null) {
michael@0 63 mScrollTimer = new Timer();
michael@0 64 ScrollRunnable task = new ScrollRunnable(view);
michael@0 65 mScrollTimer.scheduleAtFixedRate(task, 0, MS_PER_FRAME);
michael@0 66 }
michael@0 67
michael@0 68 return true;
michael@0 69 }
michael@0 70 }
michael@0 71
michael@0 72 return false;
michael@0 73 }
michael@0 74
michael@0 75 /**
michael@0 76 * Cancels the running scroll animation if it is in progress.
michael@0 77 */
michael@0 78 public void cancel() {
michael@0 79 if (mScrollTimer != null) {
michael@0 80 mScrollTimer.cancel();
michael@0 81 mScrollTimer = null;
michael@0 82 }
michael@0 83 }
michael@0 84 }

mercurial