Example #1
0
  /**
   * Scroll to child.
   *
   * @param i the i
   */
  void scrollToChild(int i) {

    CarouselItemView view = (CarouselItemView) getAdapter().getView(i, null, null);
    float angle = view.getCurrentAngle();

    if (angle == 0) return;

    if (angle > 180.0f) angle = 360.0f - angle;
    else angle = -angle;

    mFlingRunnable.startUsingDistance(angle);
  }
Example #2
0
  /** Brings an item with nearest to 0 degrees angle to this angle and sets it selected. */
  private void scrollIntoSlots() {

    // Nothing to do
    if (getChildCount() == 0 || mSelectedChild == null) return;

    // get nearest item to the 0 degrees angle
    // Sort itmes and get nearest angle
    float angle;
    int position;

    ArrayList<CarouselItemView> arr = new ArrayList<CarouselItemView>();

    for (int i = 0; i < getAdapter().getCount(); i++)
      arr.add(((CarouselItemView) getAdapter().getView(i, null, null)));

    Collections.sort(
        arr,
        new Comparator<CarouselItemView>() {

          public int compare(CarouselItemView c1, CarouselItemView c2) {
            int a1 = (int) c1.getCurrentAngle();
            if (a1 > 180) a1 = 360 - a1;
            int a2 = (int) c2.getCurrentAngle();
            if (a2 > 180) a2 = 360 - a2;
            return (a1 - a2);
          }
        });

    angle = arr.get(0).getCurrentAngle();

    // Make it minimum to rotate
    if (angle > 180.0f) angle = -(360.0f - angle);

    // Start rotation if needed
    if (angle != 0.0f) {
      mFlingRunnable.startUsingDistance(-angle);
    } else {
      // Set selected position
      position = arr.get(0).getIndex();
      setSelectedPositionInt(position);
      onFinishedMovement();
    }
  }