001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jppf.android.node.views;
019
020import android.animation.ArgbEvaluator;
021import android.animation.ValueAnimator;
022import android.app.Activity;
023import android.content.Context;
024import android.util.AttributeSet;
025import android.widget.TextView;
026
027/**
028 * A customized {@link TextView} whose text color goes smoothly from a color to another and back while a job is executing.
029 * @author Laurent Cohen
030 */
031public class GlowingTextView extends TextView {
032  /**
033   * The animator used to animate the text color.
034   */
035  private ValueAnimator animator;
036  /**
037   * The initial color of this text, when the view is first displayed.
038   */
039  private int initialColor;
040
041  /**
042   * Initialize with the specified {@link Context}
043   * @param context the cpntext for this view.
044   */
045  public GlowingTextView(final Context context) {
046    super(context);
047  }
048
049  /**
050   * Initialize with the specified {@link Context}
051   * @param context the cpntext for this view.
052   */
053  public GlowingTextView(final Context context, final AttributeSet attrs) {
054    super(context, attrs);
055  }
056
057  /**
058   * Initialize with the specified {@link Context}
059   * @param context the cpntext for this view.
060   */
061  public GlowingTextView(final Context context, final AttributeSet attrs, final int defStyle) {
062    super(context, attrs, defStyle);
063  }
064
065  /**
066   * Start the animation using the specified  duration, the intial text color of this view as start color
067   * and the specified end color.
068   * @param duration the animation duration.
069   * @param endColor the end color.
070   * @return this view.
071   */
072  public GlowingTextView startAnimation(long duration,int endColor) {
073    return startAnimation(duration, getCurrentTextColor(), endColor);
074  }
075
076  /**
077   * Start the animation using the specified  duration, start color and end color.
078   * @param duration the animation duration.
079   * @param startColor the start color.
080   * @param endColor the end color.
081   * @return this view.
082   */
083  public GlowingTextView startAnimation(long duration, int startColor, int endColor) {
084    if (animator != null) {
085      if (animator.isStarted() || animator.isRunning()) animator.end();
086    }
087    this.initialColor = getCurrentTextColor();
088    animator = ValueAnimator.ofInt(startColor, endColor);
089    animator.setDuration(duration);
090    ValueAnimator.setFrameDelay(40L);
091    animator.setRepeatCount(ValueAnimator.INFINITE);
092    animator.setRepeatMode(ValueAnimator.REVERSE);
093    animator.setEvaluator(new ArgbEvaluator());
094    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
095        @Override
096        public void onAnimationUpdate(final ValueAnimator animation) {
097          if (getContext() instanceof Activity) {
098            ((Activity) getContext()).runOnUiThread(new Runnable() {
099              @Override
100              public void run() {
101                setTextColor((Integer) animation.getAnimatedValue() | 0xFF000000);
102              }
103            });
104          }
105        }
106      });
107    animator.start();
108    return this;
109  }
110
111  /**
112   * End the animation.
113   * @return this view.
114   */
115  public GlowingTextView endAnimation() {
116    if (animator != null) {
117      if (animator.isStarted() || animator.isRunning()) animator.end();
118      animator = null;
119    }
120    setTextColor(initialColor);
121    return this;
122  }
123}