Android Open Source - GuiLib View Scroller






From Project

Back to project page GuiLib.

License

The source code is released under:

Apache License

If you think the Android project GuiLib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gandulf.guilib.view;
//w  ww . ja v a2 s.  c o m
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;

/**
 * Scroller class handles scrolling events and updates the
 */
public class ViewScroller {
  /**
   * Scrolling listener interface
   */
  public interface ScrollingListener {
    /**
     * Scrolling callback called when scrolling is performed.
     * 
     * @param distance
     *            the distance to scroll
     */
    void onScroll(int distance);

    /**
     * Starting callback called when scrolling is started
     */
    void onStarted();

    /**
     * Finishing callback called after justifying
     */
    void onFinished();

    /**
     * Justifying callback called to justify a view when scrolling is ended
     */
    void onJustify();
  }

  /** Scrolling duration */
  private static final int SCROLLING_DURATION = 400;

  /** Minimum delta for scrolling */
  public static final int MIN_DELTA_FOR_SCROLLING = 1;

  // Listener
  private ScrollingListener listener;

  // Context
  private Context context;

  // Scrolling

  private Scroller scroller;
  private int lastScrollY;
  private boolean isScrollingPerformed;
  private int orientation;

  /**
   * Constructor
   * 
   * @param context
   *            the current context
   * @param listener
   *            the scrolling listener
   */
  public ViewScroller(Context context, ScrollingListener listener, int orientation) {

    this.orientation = orientation;
    this.scroller = new Scroller(context);
    this.listener = listener;
    this.context = context;
  }

  /**
   * Set the the specified scrolling interpolator
   * 
   * @param interpolator
   *            the interpolator
   */
  public void setInterpolator(Interpolator interpolator) {
    scroller.forceFinished(true);
    scroller = new Scroller(context, interpolator);
  }

  public int getOrientation() {
    return orientation;
  }

  public void setOrientation(int orientation) {
    this.orientation = orientation;
  }

  /**
   * Scroll the wheel
   * 
   * @param distance
   *            the scrolling distance
   * @param time
   *            the scrolling duration
   */
  public void scroll(int distance, int time) {
    scroller.forceFinished(true);

    lastScrollY = 0;

    if (orientation == LinearLayout.VERTICAL) {
      scroller.startScroll(0, 0, 0, distance, time != 0 ? time : SCROLLING_DURATION);
    } else {
      scroller.startScroll(0, 0, distance, 0, time != 0 ? time : SCROLLING_DURATION);
    }
    setNextMessage(MESSAGE_SCROLL);

    startScrolling();
  }

  /**
   * Stops scrolling
   */
  public void stopScrolling() {
    scroller.forceFinished(true);
  }

  // Messages
  private final int MESSAGE_SCROLL = 0;
  private final int MESSAGE_JUSTIFY = 1;

  /**
   * Set next message to queue. Clears queue before.
   * 
   * @param message
   *            the message to set
   */
  private void setNextMessage(int message) {
    clearMessages();
    animationHandler.sendEmptyMessage(message);
  }

  /**
   * Clears messages from queue
   */
  private void clearMessages() {
    animationHandler.removeMessages(MESSAGE_SCROLL);
    animationHandler.removeMessages(MESSAGE_JUSTIFY);
  }

  // animation handler
  private Handler animationHandler = new Handler() {
    public void handleMessage(Message msg) {
      scroller.computeScrollOffset();
      int currY, finalY, delta;
      if (orientation == LinearLayout.VERTICAL) {
        currY = scroller.getCurrY();
        finalY = scroller.getFinalY();
      } else {
        currY = scroller.getCurrX();
        finalY = scroller.getFinalX();
      }

      delta = lastScrollY - currY;
      lastScrollY = currY;
      if (delta != 0) {
        listener.onScroll(delta);
      }

      // scrolling is not finished when it comes to final Y
      // so, finish it manually
      if (Math.abs(currY - finalY) < MIN_DELTA_FOR_SCROLLING) {
        currY = finalY;
        scroller.forceFinished(true);
      }
      if (!scroller.isFinished()) {
        animationHandler.sendEmptyMessage(msg.what);
      } else if (msg.what == MESSAGE_SCROLL) {
        justify();
      } else {
        finishScrolling();
      }
    }
  };

  /**
   * Justifies wheel
   */
  private void justify() {
    listener.onJustify();
    setNextMessage(MESSAGE_JUSTIFY);
  }

  /**
   * Starts scrolling
   */
  private void startScrolling() {
    if (!isScrollingPerformed) {
      isScrollingPerformed = true;
      listener.onStarted();
    }
  }

  /**
   * Finishes scrolling
   */
  void finishScrolling() {
    if (isScrollingPerformed) {
      listener.onFinished();
      isScrollingPerformed = false;
    }
  }

}




Java Source Code List

au.com.bytecode.opencsv.CSVReader.java
com.ecloud.pulltozoomview.PullToZoomScrollView.java
com.gandulf.guilib.data.OpenArrayAdapter.java
com.gandulf.guilib.data.OpenFilter.java
com.gandulf.guilib.download.AbstractDownloader.java
com.gandulf.guilib.download.DownloadBroadcastReceiver.java
com.gandulf.guilib.download.DownloaderGinger.java
com.gandulf.guilib.download.DownloaderWrapper.java
com.gandulf.guilib.download.Downloader.java
com.gandulf.guilib.download.MediaScannerWrapper.java
com.gandulf.guilib.download.UnzipIntentService.java
com.gandulf.guilib.listener.CheckableListenable.java
com.gandulf.guilib.listener.OnCheckedChangeListener.java
com.gandulf.guilib.util.ColorUtil.java
com.gandulf.guilib.util.Debug.java
com.gandulf.guilib.util.DefaultTextWatcher.java
com.gandulf.guilib.util.DirectoryFileFilter.java
com.gandulf.guilib.util.FileFileFilter.java
com.gandulf.guilib.util.ListViewCompat.java
com.gandulf.guilib.util.MathUtil.java
com.gandulf.guilib.util.ResUtil.java
com.gandulf.guilib.view.ColorPickerDialog.java
com.gandulf.guilib.view.DynamicListViewEx.java
com.gandulf.guilib.view.HackeyDrawerLayout.java
com.gandulf.guilib.view.SeekBarEx.java
com.gandulf.guilib.view.VersionInfoDialog.java
com.gandulf.guilib.view.ViewScroller.java
com.gandulf.guilib.view.adapter.MultiFragmentPagerAdapter.java
com.getbase.floatingactionbutton.AddFloatingActionButton.java
com.getbase.floatingactionbutton.FloatingActionButton.java
com.getbase.floatingactionbutton.FloatingActionsMenu.java
com.github.amlcurran.showcaseview.AnimationFactory.java
com.github.amlcurran.showcaseview.AnimatorAnimationFactory.java
com.github.amlcurran.showcaseview.ApiUtils.java
com.github.amlcurran.showcaseview.Calculator.java
com.github.amlcurran.showcaseview.NewShowcaseDrawer.java
com.github.amlcurran.showcaseview.OnShowcaseEventListener.java
com.github.amlcurran.showcaseview.ShotStateStore.java
com.github.amlcurran.showcaseview.ShowcaseAreaCalculator.java
com.github.amlcurran.showcaseview.ShowcaseDrawer.java
com.github.amlcurran.showcaseview.ShowcaseView.java
com.github.amlcurran.showcaseview.StandardShowcaseDrawer.java
com.github.amlcurran.showcaseview.TextDrawer.java
com.github.amlcurran.showcaseview.targets.ActionBarReflector.java
com.github.amlcurran.showcaseview.targets.ActionBarViewWrapper.java
com.github.amlcurran.showcaseview.targets.ActionItemTarget.java
com.github.amlcurran.showcaseview.targets.ActionViewTarget.java
com.github.amlcurran.showcaseview.targets.AppCompatReflector.java
com.github.amlcurran.showcaseview.targets.PointTarget.java
com.github.amlcurran.showcaseview.targets.ReflectorFactory.java
com.github.amlcurran.showcaseview.targets.Reflector.java
com.github.amlcurran.showcaseview.targets.SherlockReflector.java
com.github.amlcurran.showcaseview.targets.Target.java
com.github.amlcurran.showcaseview.targets.ViewTarget.java
com.sothree.slidinguppanel.SlidingUpPanelLayout.java
com.sothree.slidinguppanel.ViewDragHelper.java
com.thebnich.floatinghintedittext.FloatingHintEditText.java
com.thebnich.floatinghintedittext.FloatingHintTextView.java
com.wefika.flowlayout.FlowLayout.java
de.hdodenhof.circleimageview.CircleImageView.java
uk.co.senab.photoview.Compat.java
uk.co.senab.photoview.PhotoViewAttacher.java
uk.co.senab.photoview.PhotoView.java
uk.co.senab.photoview.SDK16.java
uk.co.senab.photoview.ScrollerProxy.java
uk.co.senab.photoview.VersionedGestureDetector.java
uk.me.lewisdeane.ldialogs.BaseDialog.java
uk.me.lewisdeane.ldialogs.CustomDialog.java
uk.me.lewisdeane.ldialogs.CustomListAdapter.java