Created
April 10, 2015 05:22
-
-
Save suanmiao/a7c7b37733374bc8db32 to your computer and use it in GitHub Desktop.
WavingView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class WavingView extends View | |
implements | |
ValueAnimator.AnimatorUpdateListener, | |
Animator.AnimatorListener { | |
private int maxWaveRadius, minWaveRadius; | |
private int centerX, centerY; | |
private final long WAVE_SPREAD_DURATION = 2000; | |
private final long WAVE_DELAY_DURATION = 1000; | |
private final int WAVE_AMOUNT = 4; | |
private boolean running; | |
private ValueAnimator mAnimator; | |
private Paint mPaint; | |
private float totalCircle; | |
private float waveDelayValue = 1f; | |
private float waveDurationValue = 4f; | |
private List<Wave> drawingWave; | |
public WavingView(Context context) { | |
super(context); | |
init(); | |
} | |
public WavingView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(); | |
} | |
public WavingView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(); | |
} | |
private void init() { | |
maxWaveRadius = getResources().getDimensionPixelSize(R.dimen.wave_max_radius); | |
minWaveRadius = getResources().getDimensionPixelSize(R.dimen.wave_min_radius); | |
mPaint = new Paint(); | |
// totalCircle = (WAVE_AMOUNT-2) * waveDelayValue + waveDurationValue; | |
totalCircle = waveDurationValue; | |
} | |
private void initAnimator() { | |
mAnimator = ValueAnimator.ofFloat(0, totalCircle); | |
mAnimator.setDuration((WAVE_AMOUNT - 1) * WAVE_DELAY_DURATION + WAVE_SPREAD_DURATION); | |
mAnimator.setInterpolator(new LinearInterpolator()); | |
mAnimator.addUpdateListener(this); | |
mAnimator.addListener(this); | |
} | |
public void startWaving() { | |
if (running) { | |
return; | |
} | |
this.running = true; | |
if (drawingWave == null) { | |
drawingWave = new ArrayList<>(); | |
for (int i = 0; i < WAVE_AMOUNT; i++) { | |
drawingWave.add(new Wave(i * waveDelayValue)); | |
} | |
} | |
initAnimator(); | |
mAnimator.start(); | |
} | |
public void stopWaving() { | |
this.running = false; | |
if (mAnimator != null) { | |
mAnimator.cancel(); | |
} | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
drawContent(canvas); | |
} | |
private void drawContent(Canvas canvas) { | |
if (running && drawingWave != null) { | |
for (Wave wave : drawingWave) { | |
mPaint.setColor(Color.argb(wave.getAlpha(), 255, 104, 55)); | |
canvas.drawCircle(centerX, centerY, wave.getRadius(), mPaint); | |
} | |
} | |
} | |
public void setCenterX(int centerX) { | |
this.centerX = centerX; | |
} | |
public void setCenterY(int centerY) { | |
this.centerY = centerY; | |
} | |
@Override | |
public void onAnimationStart(Animator animation) { | |
} | |
@Override | |
public void onAnimationEnd(Animator animation) { | |
if (running) { | |
initAnimator(); | |
mAnimator.start(); | |
} | |
} | |
@Override | |
public void onAnimationCancel(Animator animation) { | |
} | |
@Override | |
public void onAnimationRepeat(Animator animation) { | |
} | |
@Override | |
public void onAnimationUpdate(ValueAnimator animation) { | |
if (drawingWave != null) { | |
for (int i = 0; i < drawingWave.size(); i++) { | |
Wave wave = drawingWave.get(i); | |
wave.onAnimationUpdate(i, (float) animation.getAnimatedValue()); | |
} | |
invalidate(); | |
} | |
} | |
public class Wave { | |
public float animatingValue = 0; | |
private float delay; | |
public Wave(float delay) { | |
this.delay = delay; | |
init(); | |
} | |
private void init() {} | |
public int getAlpha() { | |
if (animatingValue == -1) { | |
return 0; | |
} else { | |
return (int) ((1f - animatingValue) * 255); | |
} | |
} | |
public int getRadius() { | |
if (animatingValue == -1) { | |
return 0; | |
} else { | |
return minWaveRadius + (int) ((maxWaveRadius - minWaveRadius) * animatingValue); | |
} | |
} | |
private long lastUpdate = 0; | |
public void onAnimationUpdate(int index, float runningValue) { | |
float circleValue = ((runningValue + totalCircle - delay) % totalCircle); | |
if (circleValue >= 0 && circleValue <= waveDurationValue) { | |
animatingValue = | |
circleValue / waveDurationValue; | |
} else { | |
animatingValue = -1; | |
} | |
if (index == 0) { | |
// Log.e("SUAN", "" + (System.currentTimeMillis() - lastUpdate) + "|" + animatingValue); | |
} | |
lastUpdate = System.currentTimeMillis(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment