CubicToSample
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
import android.animation.TypeEvaluator; | |
import android.animation.ValueAnimator; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Path; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.view.animation.AccelerateDecelerateInterpolator; | |
/** | |
* Created by amyu on 15/05/20 | |
*/ | |
public class CubicToSample extends View { | |
private Paint mPaint; | |
private int mWidth; | |
private Path mWavePath; | |
private static final float[][] WAVE_CURRENT_POINT_1 = { | |
{0.0f, 0.0f}, | |
{0.125f, 0.1f}, | |
{0.25f, 0.25f}, | |
{0.5f, 0.1f}, | |
{0.75f, 0.25f}, | |
{0.875f, 0.1f}, | |
{1.0f, 0.0f} | |
}; | |
private static final float[][] WAVE_CURRENT_POINT_2 = { | |
{0.0f, 0.0f}, | |
{0.125f, 0.1f}, | |
{0.3f, 0.02f}, | |
{0.5f, 0.2f}, | |
{0.7f, 0.02f}, | |
{0.875f, 0.1f}, | |
{1.0f, 0.0f} | |
}; | |
private static final float[][] WAVE_CONTROL_POINT1_1 = { | |
{0.0602f, 0.0f}, | |
{0.1718f, 0.1771f}, | |
{0.316f, 0.2476f}, | |
{0.5631f, 0.0976f}, | |
{0.8101f, 0.2523f}, | |
{0.9029f, 0.0491f} | |
}; | |
private static final float[][] WAVE_CONTROL_POINT1_2 = { | |
{0.0602f, 0.0f}, | |
{0.1718f, 0.1771f}, | |
{0.3631f, 0.02f}, | |
{0.5691f, 0.204f}, | |
{0.7631f, 0.02f}, | |
{0.9029f, 0.0491f} | |
}; | |
private static final float[][] WAVE_CONTROL_POINT2_1 = { | |
{0.0941f, 0.0491f}, | |
{0.1869f, 0.2523f}, | |
{0.434f, 0.0976f}, | |
{0.681f, 0.2476f}, | |
{0.8252f, 0.1771f}, | |
{0.9368f, 0.0f} | |
}; | |
private static final float[][] WAVE_CONTROL_POINT2_2 = { | |
{0.0941f, 0.0491f}, | |
{0.2369f, 0.02f}, | |
{0.4311f, 0.204f}, | |
{0.6369f, 0.02f}, | |
{0.8252f, 0.1771f}, | |
{0.9368f, 0.0f} | |
}; | |
public CubicToSample(Context context) { | |
this(context, null, 0); | |
} | |
public CubicToSample(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public CubicToSample(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
initView(); | |
} | |
private void initView() { | |
setUpPaint(); | |
setUpPath(); | |
setUpAnimator(); | |
} | |
private void setUpAnimator() { | |
mWaveControl1Animator = ValueAnimator.ofObject(new WaveTypeEvaluator(), WAVE_CONTROL_POINT1_1, WAVE_CONTROL_POINT1_1); | |
mWaveControl1Animator.start(); | |
mWaveControl2Animator = ValueAnimator.ofObject(new WaveTypeEvaluator(), WAVE_CONTROL_POINT2_1, WAVE_CONTROL_POINT2_1); | |
mWaveControl2Animator.start(); | |
mWavePositionAnimator = ValueAnimator.ofObject(new WaveTypeEvaluator(), WAVE_CURRENT_POINT_1, WAVE_CURRENT_POINT_1); | |
mWavePositionAnimator.start(); | |
} | |
private void setUpPaint() { | |
mPaint = new Paint(); | |
mPaint.setColor(Color.RED); | |
mPaint.setAntiAlias(true); | |
mPaint.setStyle(Paint.Style.FILL); | |
} | |
private void setUpPath() { | |
mWavePath = new Path(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
mWavePath.reset(); | |
mWavePath.moveTo(WAVE_CURRENT_POINT_2[0][0], WAVE_CURRENT_POINT_2[0][1]); | |
float[][] controlPoint1 = (float[][]) mWaveControl1Animator.getAnimatedValue(); | |
float[][] controlPoint2 = (float[][]) mWaveControl2Animator.getAnimatedValue(); | |
float[][] endPoint = (float[][]) mWavePositionAnimator.getAnimatedValue(); | |
for (int i = 0; i < 6; i++) { | |
mWavePath.cubicTo( | |
controlPoint1[i][0] * mWidth, controlPoint1[i][1] * 1000, | |
controlPoint2[i][0] * mWidth, controlPoint2[i][1] * 1000, | |
endPoint[i + 1][0] * mWidth, endPoint[i + 1][1] * 1000 | |
); | |
} | |
canvas.drawPath(mWavePath, mPaint); | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
mWidth = w; | |
super.onSizeChanged(w, h, oldw, oldh); | |
} | |
private ValueAnimator mWaveControl1Animator; | |
private ValueAnimator mWaveControl2Animator; | |
private ValueAnimator mWavePositionAnimator; | |
public void startWaveAnimation() { | |
mWaveControl1Animator = ValueAnimator.ofObject(new WaveTypeEvaluator(), WAVE_CONTROL_POINT1_1, WAVE_CONTROL_POINT1_2); | |
mWaveControl1Animator.setDuration(500); | |
mWaveControl1Animator.addUpdateListener(mAnimatorUpdateListener); | |
mWaveControl1Animator.start(); | |
mWaveControl2Animator = ValueAnimator.ofObject(new WaveTypeEvaluator(), WAVE_CONTROL_POINT2_1, WAVE_CONTROL_POINT2_2); | |
mWaveControl2Animator.setDuration(500); | |
mWaveControl2Animator.addUpdateListener(mAnimatorUpdateListener); | |
mWaveControl2Animator.start(); | |
mWavePositionAnimator = ValueAnimator.ofObject(new WaveTypeEvaluator(), WAVE_CURRENT_POINT_1, WAVE_CURRENT_POINT_2); | |
mWavePositionAnimator.setDuration(500); | |
mWavePositionAnimator.addUpdateListener(mAnimatorUpdateListener); | |
mWavePositionAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); | |
mWavePositionAnimator.start(); | |
} | |
private ValueAnimator.AnimatorUpdateListener mAnimatorUpdateListener = new ValueAnimator.AnimatorUpdateListener() { | |
@Override | |
public void onAnimationUpdate(ValueAnimator valueAnimator) { | |
invalidate(); | |
} | |
}; | |
public static class WaveTypeEvaluator implements TypeEvaluator<float[][]> { | |
@Override | |
public float[][] evaluate(float v, float[][] start, float[][] end) { | |
float[][] newValue = new float[start.length][start[0].length]; | |
for (int i = 0; i < start.length; i++) { | |
for (int j = 0; j < start[i].length; j++) { | |
newValue[i][j] = start[i][j] + (end[i][j] - start[i][j]) * v; | |
} | |
} | |
return newValue; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment