コード例 #1
0
  /**
   * Animate change of color
   *
   * @param event Event to process
   */
  public void startAnimateColorChange(@NonNull final DecoEvent event) {
    cancelAnimation();
    event.notifyStartListener();
    mVisible = true;

    mDrawMode = event.getEventType();
    mPercentComplete = 0f;

    final boolean changeColors = event.isColorSet();
    if (changeColors) {
      mColorAnimate = new ColorAnimate(mSeriesItem.getColor(), event.getColor());
      mSeriesItem.setColor(event.getColor());
    } else {
      Log.w(TAG, "Must set new color to start CHANGE_COLOR event");
      return;
    }

    final float maxValue = 1.0f;
    mValueAnimator = ValueAnimator.ofFloat(0, maxValue);

    mValueAnimator.setDuration(event.getEffectDuration());
    if (event.getInterpolator() != null) {
      mValueAnimator.setInterpolator(event.getInterpolator());
    } else {
      mValueAnimator.setInterpolator(new LinearInterpolator());
    }

    mValueAnimator.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator valueAnimator) {
            mPercentComplete = Float.valueOf(valueAnimator.getAnimatedValue().toString());

            for (SeriesItem.SeriesItemListener seriesItemListener : mSeriesItem.getListeners()) {
              seriesItemListener.onSeriesItemDisplayProgress(mPercentComplete);
            }
          }
        });

    mValueAnimator.addListener(
        new AnimatorListenerAdapter() {
          @Override
          public void onAnimationEnd(Animator animation) {
            event.notifyEndListener();
          }
        });

    mValueAnimator.start();
  }
コード例 #2
0
  @Override
  public void createAnimation() {
    float startX = getWidth() / 6;
    float startY = getWidth() / 6;
    for (int i = 0; i < 2; i++) {
      final int index = i;
      ValueAnimator translateXAnim =
          ValueAnimator.ofFloat(startX, getWidth() - startX, startX, getWidth() - startX, startX);
      if (i == 1) {
        translateXAnim =
            ValueAnimator.ofFloat(
                getWidth() - startX, startX, getWidth() - startX, startX, getWidth() - startX);
      }
      ValueAnimator translateYAnim =
          ValueAnimator.ofFloat(startY, startY, getHeight() - startY, getHeight() - startY, startY);
      if (i == 1) {
        translateYAnim =
            ValueAnimator.ofFloat(
                getHeight() - startY, getHeight() - startY, startY, startY, getHeight() - startY);
      }

      translateXAnim.setDuration(2000);
      translateXAnim.setInterpolator(new LinearInterpolator());
      translateXAnim.setRepeatCount(-1);
      translateXAnim.addUpdateListener(
          new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
              translateX[index] = (float) animation.getAnimatedValue();
              postInvalidate();
            }
          });
      translateXAnim.start();

      translateYAnim.setDuration(2000);
      translateYAnim.setInterpolator(new LinearInterpolator());
      translateYAnim.setRepeatCount(-1);
      translateYAnim.addUpdateListener(
          new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
              translateY[index] = (float) animation.getAnimatedValue();
              postInvalidate();
            }
          });
      translateYAnim.start();
    }
  }
コード例 #3
0
    public void start() {
      initpaints();
      fadeAndScaleAnimator.setInterpolator(new OvershootInterpolator(1.15f));
      fadeAndScaleAnimator.start();
      fadeAndScaleAnimator.addUpdateListener(
          new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
              invalidate();
              if (animation.getAnimatedFraction() > .8f && !hasfired) {
                hasfired = true;
                rotationAnimator.start();
              }
            }
          });
      rotationAnimator.setInterpolator(new DecelerateInterpolator());

      rotationAnimator.addUpdateListener(
          new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
              invalidate();
            }
          });
    }
コード例 #4
0
  public void duang() {
    mStatus = Status.STATUS_DOWN;
    ValueAnimator valueAnimator = ValueAnimator.ofInt(mMaxArcHeight, 0);
    valueAnimator.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            mArcHeight = (int) animation.getAnimatedValue();
            invalidate();
          }
        });
    valueAnimator.addListener(
        new SimpleAnimationListener() {

          @Override
          public void onAnimationEnd(Animator animation) {
            if (mAnimationListener != null) {
              mAnimationListener.onEnd();
            }
          }
        });
    valueAnimator.setDuration(500);
    valueAnimator.setInterpolator(new OvershootInterpolator(5f));
    valueAnimator.start();
  }
コード例 #5
0
  public void show() {
    mStatus = Status.STATUS_SMOOTH_UP;

    if (mAnimationListener != null) {
      mAnimationListener.onStart();
      this.postDelayed(
          new Runnable() {
            @Override
            public void run() {

              mAnimationListener.onContentShow();
            }
          },
          600);
    }

    ValueAnimator valueAnimator = ValueAnimator.ofInt(0, mMaxArcHeight);
    valueAnimator.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            int value = (int) animation.getAnimatedValue();
            mArcHeight = value;

            if (value == mMaxArcHeight) {
              duang();
            }
            invalidate();
          }
        });
    valueAnimator.setDuration(800);
    valueAnimator.setInterpolator(new AccelerateInterpolator());
    valueAnimator.start();
  }
コード例 #6
0
  public void animateSubjectOut(final SubjectCard materia) {
    final List<View> toBeAnimated = new ArrayList<View>();
    boolean after = false;
    for (SubjectCard mtr : mSubjectCards) {
      if (after) toBeAnimated.add(mtr);
      if (mtr == materia) after = true;
    }
    toBeAnimated.add(mAddButton);
    final int numberToBeAnimated = toBeAnimated.size();

    int maxScroll = mScrollView.getChildAt(0).getHeight() - mScrollView.getHeight(),
        materiaHeight = materia.getHeight();
    final int initScroll = mScrollView.getScrollY(),
        scrollBy =
            ((maxScroll < initScroll + materiaHeight)
                    ? Math.max(maxScroll - materiaHeight, 0)
                    : initScroll)
                - initScroll;
    ValueAnimator listAnimator = ValueAnimator.ofFloat(0, -materiaHeight);
    listAnimator.setInterpolator(new DecelerateInterpolator(3.2f));
    listAnimator.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            float value = (Float) animation.getAnimatedValue();
            for (int i = 0; i < numberToBeAnimated; i++)
              ViewHelper.setTranslationY(toBeAnimated.get(i), value);
            ViewHelper.setScrollY(
                mScrollView, (int) (initScroll + scrollBy * animation.getAnimatedFraction()));
          }
        });
    listAnimator.addListener(
        new AnimatorListenerAdapter() {
          @Override
          public void onAnimationEnd(Animator animation) {
            mSubjectCards.remove(materia);
            mSubjectsLayout.removeView(materia);
            mSqlHelper.remove(materia.getData().getSqlID());
            updateTotal();
            mScrollView.setVerticalScrollBarEnabled(true);
            mScrollView.setOnTouchListener(null);
            for (View mtr : toBeAnimated) ViewHelper.setTranslationY(mtr, 0);
          }
        });
    listAnimator.setDuration(700);
    mScrollView.setVerticalScrollBarEnabled(false);
    mScrollView.setOnTouchListener(
        new View.OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
            return true;
          }
        }); // disable user scrolling during the animation

    if (ViewHelper.getTranslationX(materia) == 0) materia.swipeRight(0, 0);
    listAnimator.setStartDelay(500);
    listAnimator.start();
  }
コード例 #7
0
 /** 开始绘制动画 */
 private void startAnim(int value) {
   ValueAnimator valueAnimator = ObjectAnimator.ofInt(value);
   valueAnimator.setDuration(1000);
   valueAnimator.setInterpolator(new LinearInterpolator());
   valueAnimator.addUpdateListener(
       new ValueAnimator.AnimatorUpdateListener() {
         @Override
         public void onAnimationUpdate(ValueAnimator valueAnimator) {
           mDegress = Integer.valueOf(valueAnimator.getAnimatedValue().toString());
           invalidate();
         }
       });
   valueAnimator.start();
 }
コード例 #8
0
  @Override
  public void createAnimation() {
    long[] delays = new long[] {0, 200, 400};
    for (int i = 0; i < 3; i++) {
      final int index = i;
      ValueAnimator scaleAnim = ValueAnimator.ofFloat(0, 1);
      scaleAnim.setInterpolator(new LinearInterpolator());
      scaleAnim.setDuration(1000);
      scaleAnim.setRepeatCount(-1);
      scaleAnim.addUpdateListener(
          new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
              scaleFloats[index] = (float) animation.getAnimatedValue();
              postInvalidate();
            }
          });
      scaleAnim.setStartDelay(delays[i]);
      scaleAnim.start();

      ValueAnimator alphaAnim = ValueAnimator.ofInt(0, 255);
      scaleAnim.setInterpolator(new LinearInterpolator());
      alphaAnim.setDuration(1000);
      alphaAnim.setRepeatCount(-1);
      alphaAnim.addUpdateListener(
          new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
              alphaInts[index] = (int) animation.getAnimatedValue();
              postInvalidate();
            }
          });
      scaleAnim.setStartDelay(delays[i]);
      alphaAnim.start();
    }
  }
コード例 #9
0
  /**
   * Execute an Animation effect by starting the Value Animator
   *
   * @param event Event to process effect
   * @throws IllegalStateException No effect set in event
   */
  public void startAnimateEffect(@NonNull final DecoEvent event) throws IllegalStateException {
    if (event.getEffectType() == null) {
      throw new IllegalStateException("Unable to execute null effect type");
    }

    // All effects run from 0.0 .. 1.0f in duration
    final float maxValue = 1.0f;

    cancelAnimation();
    event.notifyStartListener();

    mVisible = true;
    mDrawMode = event.getEventType();
    mEffect = new DecoDrawEffect(event.getEffectType(), mPaint, event.getDisplayText());
    mEffect.setRotationCount(event.getEffectRotations());

    mPercentComplete = 0f;

    mValueAnimator = ValueAnimator.ofFloat(0, maxValue);
    mValueAnimator.setDuration(event.getEffectDuration());
    Interpolator interpolator =
        (event.getInterpolator() != null) ? event.getInterpolator() : new LinearInterpolator();
    mValueAnimator.setInterpolator(interpolator);

    mValueAnimator.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator valueAnimator) {
            mPercentComplete = Float.valueOf(valueAnimator.getAnimatedValue().toString());
            for (SeriesItem.SeriesItemListener seriesItemListener : mSeriesItem.getListeners()) {
              seriesItemListener.onSeriesItemDisplayProgress(mPercentComplete);
            }
          }
        });

    mValueAnimator.addListener(
        new AnimatorListenerAdapter() {
          @Override
          public void onAnimationEnd(Animator animation) {
            event.notifyEndListener();
            mDrawMode = DecoEvent.EventType.EVENT_MOVE;
            mVisible = mEffect.postExecuteVisibility();
            mEffect = null;
          }
        });

    mValueAnimator.start();
  }
コード例 #10
0
  /**
   * Creates a new animation whose parameters come from the specified context and attributes set.
   *
   * @param res The resources
   * @param attrs The set of attributes holding the animation parameters
   * @param anim Null if this is a ValueAnimator, otherwise this is an ObjectAnimator
   */
  private static ValueAnimator loadAnimator(
      Context c,
      Resources res,
      Resources.Theme theme,
      AttributeSet attrs,
      ValueAnimator anim,
      float pathErrorScale)
      throws Resources.NotFoundException {

    TypedArray arrayAnimator = null;
    TypedArray arrayObjectAnimator = null;

    if (theme != null) {
      arrayAnimator = theme.obtainStyledAttributes(attrs, R.styleable.Animator, 0, 0);
    } else {
      arrayAnimator = res.obtainAttributes(attrs, R.styleable.Animator);
    }

    // If anim is not null, then it is an object animator.
    if (anim != null) {
      if (theme != null) {
        arrayObjectAnimator =
            theme.obtainStyledAttributes(attrs, R.styleable.PropertyAnimator, 0, 0);
      } else {
        arrayObjectAnimator = res.obtainAttributes(attrs, R.styleable.PropertyAnimator);
      }
    }

    if (anim == null) {
      anim = new ValueAnimator();
    }

    parseAnimatorFromTypeArray(anim, arrayAnimator, arrayObjectAnimator);

    final int resId = arrayAnimator.getResourceId(R.styleable.Animator_android_interpolator, 0);
    if (resId > 0) {
      anim.setInterpolator(AnimationUtils.loadInterpolator(c, resId));
    }

    arrayAnimator.recycle();
    if (arrayObjectAnimator != null) {
      arrayObjectAnimator.recycle();
    }

    return anim;
  }
コード例 #11
0
  private void handleActionUp() {
    if (isOutOfRange) {
      // When user drag it back, we should call onReset().
      if (GeometryUtil.getDistanceBetween2Points(mDragCenter, mInitCenter) < resetDistance) {
        if (mListener != null) mListener.onReset(isOutOfRange);
        return;
      }

      // Otherwise
      disappeared();
    } else {

      // 手指抬起时,弹回动画
      mAnim = ValueAnimator.ofFloat(1.0f);
      mAnim.setInterpolator(new OvershootInterpolator(4.0f));

      final PointF startPoint = new PointF(mDragCenter.x, mDragCenter.y);
      final PointF endPoint = new PointF(mStickCenter.x, mStickCenter.y);
      mAnim.addUpdateListener(
          new AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
              float fraction = animation.getAnimatedFraction();
              PointF pointByPercent =
                  GeometryUtil.getPointByPercent(startPoint, endPoint, fraction);
              updateDragCenter((float) pointByPercent.x, (float) pointByPercent.y);
            }
          });
      mAnim.addListener(
          new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
              if (mListener != null) mListener.onReset(isOutOfRange);
            }
          });
      if (GeometryUtil.getDistanceBetween2Points(startPoint, endPoint) < 10) {
        mAnim.setDuration(10);
      } else {
        mAnim.setDuration(500);
      }
      mAnim.start();
    }
  }
コード例 #12
0
 private void startSendingAnimation(final RevealView.RevealAnimationListener listener) {
   final int width = mFab.getLeft();
   final int height = mFab.getTop();
   ValueAnimator valueAnimator = new ValueAnimator();
   valueAnimator.setDuration(Const.DURATION / 2);
   valueAnimator.setObjectValues(new PointF(0, 0));
   valueAnimator.setInterpolator(new DecelerateInterpolator());
   valueAnimator.setEvaluator(
       new TypeEvaluator<PointF>() {
         @Override
         public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
           PointF point = new PointF();
           point.x = (width) * (1 - fraction / 2);
           point.y = (height) - 0.85f * (height) * (fraction / 2) * (fraction / 2);
           return point;
         }
       });
   valueAnimator.start();
   valueAnimator.addUpdateListener(
       new ValueAnimator.AnimatorUpdateListener() {
         @Override
         public void onAnimationUpdate(ValueAnimator animation) {
           PointF point = (PointF) animation.getAnimatedValue();
           mFab.setX(point.x);
           mFab.setY(point.y);
         }
       });
   valueAnimator.addListener(
       new AnimatorListenerAdapter() {
         @Override
         public void onAnimationEnd(Animator animation) {
           super.onAnimationEnd(animation);
           mRevealView.reveal(
               (int) mFab.getX() + mFab.getWidth() / 2,
               (int) mFab.getY() + mFab.getHeight() / 2,
               getThemeColor(),
               Const.RADIUS,
               Const.DURATION,
               listener);
         }
       });
 }
コード例 #13
0
ファイル: Snackbar.java プロジェクト: quanth29/Carbon
  public void show(final View view) {
    pushedView = view;
    if (this.getParent() != null) return;
    synchronized (Snackbar.class) {
      if (!next.contains(this)) next.add(this);
      if (next.indexOf(this) == 0) {
        View decor = ((Activity) getContext()).getWindow().getDecorView();
        ((ViewGroup) decor.findViewById(android.R.id.content))
            .addView(
                this, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        ViewHelper.setAlpha(content, 0);
        AnimUtils.flyIn(content, null);
        if (view != null) {
          ValueAnimator animator = ValueAnimator.ofFloat(0, -1);
          animator.setDuration(200);
          animator.setInterpolator(new DecelerateInterpolator());
          animator.addUpdateListener(
              new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                  MarginLayoutParams lp = (MarginLayoutParams) content.getLayoutParams();
                  Log.e(
                      "snackbar " + Snackbar.this.hashCode(),
                      ""
                          + ((content.getHeight() + lp.bottomMargin)
                              * (Float) valueAnimator.getAnimatedValue()));
                  ViewHelper.setTranslationY(
                      view,
                      (content.getHeight() + lp.bottomMargin)
                          * (Float) valueAnimator.getAnimatedValue());
                  if (pushedView.getParent() != null)
                    ((View) pushedView.getParent()).postInvalidate();
                }
              });
          animator.start();
        }
        if (duration != INFINITE) handler.postDelayed(hideRunnable, duration);
      }
    }
  }
コード例 #14
0
  /**
   * Kick off an animation to hide or show the arc. This results in an animation where both the
   * width of the line used for the arc and the transparency of the arc is altered over the duration
   * provided
   *
   * @param event Event to process
   * @param showArc True to show the arc, false to hide
   */
  public void startAnimateHideShow(@NonNull final DecoEvent event, final boolean showArc) {
    cancelAnimation();
    event.notifyStartListener();

    mDrawMode = event.getEventType();
    mPercentComplete = showArc ? 1.0f : 0f;
    mVisible = true;

    final float maxValue = 1.0f;
    mValueAnimator = ValueAnimator.ofFloat(0, maxValue);

    mValueAnimator.setDuration(event.getEffectDuration());
    mValueAnimator.setInterpolator(new LinearInterpolator());

    mValueAnimator.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator valueAnimator) {

            float current = Float.valueOf(valueAnimator.getAnimatedValue().toString());
            mPercentComplete = showArc ? (maxValue - current) : current;

            for (SeriesItem.SeriesItemListener seriesItemListener : mSeriesItem.getListeners()) {
              seriesItemListener.onSeriesItemDisplayProgress(mPercentComplete);
            }
          }
        });

    mValueAnimator.addListener(
        new AnimatorListenerAdapter() {
          @Override
          public void onAnimationEnd(Animator animation) {
            if (event.getEventType() != DecoEvent.EventType.EVENT_EFFECT) {
              event.notifyEndListener();
            }
          }
        });

    mValueAnimator.start();
  }
コード例 #15
0
  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_UP) {
      // 需要将ImageView的高度缓慢恢复到最初高度
      ValueAnimator animator = ValueAnimator.ofInt(imageView.getHeight(), orignalHeight);
      animator.addUpdateListener(
          new AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
              // 获取动画的值,设置给imageview
              int animatedValue = (Integer) animator.getAnimatedValue();

              imageView.getLayoutParams().height = animatedValue;
              imageView.requestLayout(); // 使ImageView的布局参数生效
            }
          });
      animator.setInterpolator(new OvershootInterpolator(5)); // 弹性的插值器
      animator.setDuration(350);
      animator.start();
    }
    return super.onTouchEvent(ev);
  }
コード例 #16
0
ファイル: Snackbar.java プロジェクト: quanth29/Carbon
 public void hide() {
   synchronized (Snackbar.class) {
     if (getParent() == null) return;
     handler.removeCallbacks(hideRunnable);
     if (onDismissedListener != null) onDismissedListener.onDismissed();
     AnimUtils.flyOut(
         content,
         new AnimatorListenerAdapter() {
           @Override
           public void onAnimationEnd(Animator animator) {
             hideInternal();
           }
         });
     if (pushedView != null) {
       ValueAnimator animator = ValueAnimator.ofFloat(-1, 0);
       animator.setDuration(200);
       animator.setInterpolator(new DecelerateInterpolator());
       animator.addUpdateListener(
           new ValueAnimator.AnimatorUpdateListener() {
             @Override
             public void onAnimationUpdate(ValueAnimator valueAnimator) {
               MarginLayoutParams lp = (MarginLayoutParams) content.getLayoutParams();
               Log.e(
                   "snackbar " + Snackbar.this.hashCode(),
                   ""
                       + ((content.getHeight() + lp.bottomMargin)
                           * (Float) valueAnimator.getAnimatedValue()));
               ViewHelper.setTranslationY(
                   pushedView,
                   (content.getHeight() + lp.bottomMargin)
                       * (Float) valueAnimator.getAnimatedValue());
               if (pushedView.getParent() != null)
                 ((View) pushedView.getParent()).postInvalidate();
             }
           });
       animator.start();
     }
   }
 }
コード例 #17
0
 /** 回滚状态动画 */
 private void startRollBackAnimation(long duration) {
   ValueAnimator rollBackAnim = ValueAnimator.ofFloat(curRadius, originRadius);
   rollBackAnim.addUpdateListener(
       new ValueAnimator.AnimatorUpdateListener() {
         @Override
         public void onAnimationUpdate(ValueAnimator animation) {
           float value = (float) animation.getAnimatedValue();
           curRadius = (int) value;
           postInvalidate();
         }
       });
   rollBackAnim.setInterpolator(new BounceInterpolator()); // 反弹效果
   rollBackAnim.addListener(
       new AnimatorListenerAdapter() {
         @Override
         public void onAnimationEnd(Animator animation) {
           super.onAnimationEnd(animation);
           DraggableFlagView.this.clearAnimation();
         }
       });
   rollBackAnim.setDuration(duration);
   rollBackAnim.start();
 }
コード例 #18
0
ファイル: RangeSeekBar.java プロジェクト: Sai-Teja/Carbon
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    float v = (value - min) / (max - min);
    float v2 = (value2 - min) / (max - min);

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
      int thumbX =
          (int)
              (v * (getWidth() - getPaddingLeft() - getPaddingRight() - thumbRadius * 2)
                  + getPaddingLeft()
                  + thumbRadius);
      int thumbX2 =
          (int)
              (v2 * (getWidth() - getPaddingLeft() - getPaddingRight() - thumbRadius2 * 2)
                  + getPaddingLeft()
                  + thumbRadius2);
      if (Math.abs(event.getX() - thumbX) < Math.abs(event.getX() - thumbX2)) {
        draggedThumb = 1;
        if (radiusAnimator != null) radiusAnimator.end();
        radiusAnimator = ValueAnimator.ofFloat(thumbRadius, THUMB_RADIUS_DRAGGED);
        radiusAnimator.setDuration(200);
        radiusAnimator.setInterpolator(interpolator);
        radiusAnimator.addUpdateListener(
            new ValueAnimator.AnimatorUpdateListener() {
              @Override
              public void onAnimationUpdate(ValueAnimator animation) {
                thumbRadius = (float) animation.getAnimatedValue();
                postInvalidate();
              }
            });
        radiusAnimator.start();
      } else {
        draggedThumb = 2;
        if (radiusAnimator != null) radiusAnimator.end();
        radiusAnimator = ValueAnimator.ofFloat(thumbRadius2, THUMB_RADIUS_DRAGGED);
        radiusAnimator.setDuration(200);
        radiusAnimator.setInterpolator(interpolator);
        radiusAnimator.addUpdateListener(
            new ValueAnimator.AnimatorUpdateListener() {
              @Override
              public void onAnimationUpdate(ValueAnimator animation) {
                thumbRadius2 = (float) animation.getAnimatedValue();
                postInvalidate();
              }
            });
        radiusAnimator.start();
      }
      ViewParent parent = getParent();
      if (parent != null) parent.requestDisallowInterceptTouchEvent(true);
    } else if (event.getAction() == MotionEvent.ACTION_CANCEL
        || event.getAction() == MotionEvent.ACTION_UP) {
      if (draggedThumb == 1) {
        if (style == Style.Discrete) {
          float val = (float) Math.floor((value - min + step / 2) / step) * step + min;
          if (valueAnimator != null) valueAnimator.cancel();
          valueAnimator = ValueAnimator.ofFloat(value, val);
          valueAnimator.setDuration(200);
          valueAnimator.setInterpolator(interpolator);
          valueAnimator.addUpdateListener(
              new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                  value = (float) animation.getAnimatedValue();
                  int thumbX =
                      (int)
                          ((value - min)
                                  / (max - min)
                                  * (getWidth() - getPaddingLeft() - getPaddingRight())
                              + getPaddingLeft());
                  int thumbY = getHeight() / 2;
                  int radius = rippleDrawable.getRadius();
                  rippleDrawable.setBounds(
                      thumbX - radius, thumbY - radius, thumbX + radius, thumbY + radius);
                  postInvalidate();
                }
              });
          valueAnimator.start();
        }
        if (radiusAnimator != null) radiusAnimator.end();
        radiusAnimator = ValueAnimator.ofFloat(thumbRadius, THUMB_RADIUS);
        radiusAnimator.setDuration(200);
        radiusAnimator.setInterpolator(interpolator);
        radiusAnimator.addUpdateListener(
            new ValueAnimator.AnimatorUpdateListener() {
              @Override
              public void onAnimationUpdate(ValueAnimator animation) {
                thumbRadius = (float) animation.getAnimatedValue();
                postInvalidate();
              }
            });
        radiusAnimator.start();
      } else {
        if (style == Style.Discrete) {
          float val2 = (float) Math.floor((value2 - min + step / 2) / step) * step + min;
          if (valueAnimator != null) valueAnimator.cancel();
          valueAnimator = ValueAnimator.ofFloat(value2, val2);
          valueAnimator.setDuration(200);
          valueAnimator.setInterpolator(interpolator);
          valueAnimator.addUpdateListener(
              new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                  value2 = (float) animation.getAnimatedValue();
                  int thumbX =
                      (int)
                          ((value2 - min)
                                  / (max - min)
                                  * (getWidth() - getPaddingLeft() - getPaddingRight())
                              + getPaddingLeft());
                  int thumbY = getHeight() / 2;
                  int radius = rippleDrawable.getRadius();
                  rippleDrawable.setBounds(
                      thumbX - radius, thumbY - radius, thumbX + radius, thumbY + radius);
                  postInvalidate();
                }
              });
          valueAnimator.start();
        }
        if (radiusAnimator != null) radiusAnimator.end();
        radiusAnimator = ValueAnimator.ofFloat(thumbRadius2, THUMB_RADIUS);
        radiusAnimator.setDuration(200);
        radiusAnimator.setInterpolator(interpolator);
        radiusAnimator.addUpdateListener(
            new ValueAnimator.AnimatorUpdateListener() {
              @Override
              public void onAnimationUpdate(ValueAnimator animation) {
                thumbRadius2 = (float) animation.getAnimatedValue();
                postInvalidate();
              }
            });
        radiusAnimator.start();
      }
      draggedThumb = -1;
      ViewParent parent = getParent();
      if (parent != null) parent.requestDisallowInterceptTouchEvent(false);
    }

    if (draggedThumb == 1) {
      v = (event.getX() - getPaddingLeft()) / (getWidth() - getPaddingLeft() - getPaddingRight());
      v = Math.max(0, Math.min(v, 1));
    } else if (draggedThumb == 2) {
      v2 = (event.getX() - getPaddingLeft()) / (getWidth() - getPaddingLeft() - getPaddingRight());
      v2 = Math.max(0, Math.min(v2, 1));
    }

    if (v > v2) {
      draggedThumb = 3 - draggedThumb;
      float t = v;
      v = v2;
      v2 = t;
    }
    float newValue = v * (max - min) + min;
    float newValue2 = v2 * (max - min) + min;

    if (rippleDrawable != null) {
      rippleDrawable.setHotspot(event.getX(), event.getY());
      int thumbY = getHeight() / 2;
      int radius = rippleDrawable.getRadius();
      if (draggedThumb == 1) {
        int thumbX =
            (int) (v * (getWidth() - getPaddingLeft() - getPaddingRight()) + getPaddingLeft());
        rippleDrawable.setBounds(
            thumbX - radius, thumbY - radius, thumbX + radius, thumbY + radius);
      } else if (draggedThumb == 2) {
        int thumbX2 =
            (int) (v2 * (getWidth() - getPaddingLeft() - getPaddingRight()) + getPaddingLeft());
        rippleDrawable.setBounds(
            thumbX2 - radius, thumbY - radius, thumbX2 + radius, thumbY + radius);
      }
    }

    postInvalidate();
    if (newValue != value && onValueChangedListener != null) {
      if (style == Style.Discrete) {
        int sv = stepValue(newValue);
        int sv2 = stepValue(newValue2);
        if (stepValue(value) != sv || stepValue(value2) != sv2)
          onValueChangedListener.onValueChanged(this, sv, sv2);
      } else {
        onValueChangedListener.onValueChanged(this, newValue, newValue2);
      }
      value = newValue;
    }
    super.onTouchEvent(event);
    return true;
  }
コード例 #19
0
  /** Create the animation of filling the chart by using a valueAnimator to adjust values */
  public void startAnimateMove(@NonNull final DecoEvent event) {
    mIsPaused = false;
    mDrawMode = event.getEventType();
    mVisible = true;

    cancelAnimation();
    mEventCurrent = event;

    final boolean changeColors = event.isColorSet();
    if (changeColors) {
      mColorAnimate = new ColorAnimate(mSeriesItem.getColor(), event.getColor());
      mSeriesItem.setColor(event.getColor());
    }
    float position = event.getEndPosition();

    event.notifyStartListener();

    mPositionStart = mPositionCurrentEnd;
    mPositionEnd = position;

    long animationDuration = event.getEffectDuration();

    if ((animationDuration == 0) || (Math.abs(mPositionEnd - mPositionStart) < 0.01)) {
      cancelAnimation();
      mPositionCurrentEnd = mPositionEnd;
      mEventCurrent = null;
      mPercentComplete = 1.0f;
      for (SeriesItem.SeriesItemListener seriesItemListener : mSeriesItem.getListeners()) {
        seriesItemListener.onSeriesItemAnimationProgress(1.0f, mPositionEnd);
      }
      event.notifyEndListener();
      return;
    }

    if (animationDuration < 0) {
      /**
       * If an animation duration is not set we calculate it using a formula of the proportion of a
       * revolution multiplied by the default time for a full revolution. This ensures that the
       * speed of the move is consistent for all ranges
       */
      animationDuration =
          (Math.abs(
              (int)
                  (mSeriesItem.getSpinDuration()
                      * ((mPositionStart - mPositionEnd) / mSeriesItem.getMaxValue()))));
    }

    mValueAnimator = ValueAnimator.ofFloat(mPositionStart, position);
    mValueAnimator.setDuration(animationDuration);

    /**
     * Note: When setting the Interpolator the default is {@link
     * android.view.animation.AccelerateDecelerateInterpolator}
     *
     * <p>However, if you call setInterpolator(null) then the default is not used, rather: {@link
     * LinearInterpolator}
     */
    if (event.getInterpolator() != null) {
      mValueAnimator.setInterpolator(event.getInterpolator());
    } else {
      if (mSeriesItem.getInterpolator() != null) {
        mValueAnimator.setInterpolator(mSeriesItem.getInterpolator());
      }
    }

    mValueAnimator.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator valueAnimator) {
            float current = Float.valueOf(valueAnimator.getAnimatedValue().toString());
            mPercentComplete = (current - mPositionStart) / (mPositionEnd - mPositionStart);
            mPositionCurrentEnd = current;

            /**
             * Notify the listeners of position update. This will be the OrbView itself and possibly
             * the user who is using a listener to update the progress in an alternative manner, ie.
             * displaying text progress %
             */
            for (SeriesItem.SeriesItemListener seriesItemListener : mSeriesItem.getListeners()) {
              seriesItemListener.onSeriesItemAnimationProgress(
                  mPercentComplete, mPositionCurrentEnd);
            }
          }
        });

    mValueAnimator.addListener(
        new AnimatorListenerAdapter() {
          @Override
          public void onAnimationEnd(Animator animation) {
            if (changeColors) {
              mColorAnimate = null;
            }

            event.notifyEndListener();
          }
        });

    mValueAnimator.start();
  }
コード例 #20
0
  @Override
  public List<Animator> createAnimation() {
    List<Animator> animators = new ArrayList<>();
    float startT = getWidth() / 11;
    ValueAnimator translationAnim = ValueAnimator.ofFloat(getWidth() - startT, getWidth() / 2);
    translationAnim.setDuration(650);
    translationAnim.setInterpolator(new LinearInterpolator());
    translationAnim.setRepeatCount(-1);
    translationAnim.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            translateX = (float) animation.getAnimatedValue();
            postInvalidate();
          }
        });
    translationAnim.start();

    ValueAnimator alphaAnim = ValueAnimator.ofInt(255, 122);
    alphaAnim.setDuration(650);
    alphaAnim.setRepeatCount(-1);
    alphaAnim.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            alpha = (int) animation.getAnimatedValue();
            postInvalidate();
          }
        });
    alphaAnim.start();

    ValueAnimator rotateAnim1 = ValueAnimator.ofFloat(0, 45, 0);
    rotateAnim1.setDuration(650);
    rotateAnim1.setRepeatCount(-1);
    rotateAnim1.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            degrees1 = (float) animation.getAnimatedValue();
            postInvalidate();
          }
        });
    rotateAnim1.start();

    ValueAnimator rotateAnim2 = ValueAnimator.ofFloat(0, -45, 0);
    rotateAnim2.setDuration(650);
    rotateAnim2.setRepeatCount(-1);
    rotateAnim2.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            degrees2 = (float) animation.getAnimatedValue();
            postInvalidate();
          }
        });
    rotateAnim2.start();

    animators.add(translationAnim);
    animators.add(alphaAnim);
    animators.add(rotateAnim1);
    animators.add(rotateAnim2);
    return animators;
  }
コード例 #21
0
ファイル: Snackbar.java プロジェクト: quanth29/Carbon
  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    boolean hit = e2.getY() > content.getTop() && getParent() != null;

    if (hit && swipeToDismiss && animator == null) {
      swipe = e2.getX() - e1.getX();
      ViewHelper.setTranslationX(content, swipe);
      ViewHelper.setAlpha(content, Math.max(0, 1 - 2 * Math.abs(swipe) / content.getWidth()));
      postInvalidate();
      if (Math.abs(swipe) > content.getWidth() / 4) {
        handler.removeCallbacks(hideRunnable);
        animator = ObjectAnimator.ofFloat(swipe, content.getWidth() / 2.0f * Math.signum(swipe));
        animator.setDuration(200);
        animator.addUpdateListener(
            new ValueAnimator.AnimatorUpdateListener() {
              @Override
              public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float s = (Float) valueAnimator.getAnimatedValue();
                ViewHelper.setTranslationX(content, s);
                float alpha =
                    Math.max(
                        0,
                        1
                            - 2
                                * Math.abs((Float) valueAnimator.getAnimatedValue())
                                / content.getWidth());
                ViewHelper.setAlpha(content, alpha);
                postInvalidate();
              }
            });
        animator.start();
        animator.addListener(
            new AnimatorListenerAdapter() {
              @Override
              public void onAnimationEnd(Animator animation) {
                hideInternal();
                animator = null;
              }
            });
        if (pushedView != null) {
          ValueAnimator animator = ValueAnimator.ofFloat(-1, 0);
          animator.setDuration(200);
          animator.setInterpolator(new DecelerateInterpolator());
          animator.addUpdateListener(
              new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                  MarginLayoutParams lp = (MarginLayoutParams) content.getLayoutParams();
                  Log.e(
                      "snackbar " + Snackbar.this.hashCode(),
                      ""
                          + ((content.getHeight() + lp.bottomMargin)
                              * (Float) valueAnimator.getAnimatedValue()));
                  ViewHelper.setTranslationY(
                      pushedView,
                      (content.getHeight() + lp.bottomMargin)
                          * (Float) valueAnimator.getAnimatedValue());
                  if (pushedView.getParent() != null)
                    ((View) pushedView.getParent()).postInvalidate();
                }
              });
          animator.start();
        }
      }
      return true;
    }
    return false;
  }