1.ValueAnimator通过AnimatorSet去play
ValueAnimator valueAnimator = getBesselAnimator(tempImageView, rankWidth, rankHeight);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(valueAnimator).with(riseAlphaAnimator).with(riseScaleXAnimator).with(riseScaleYAnimator);
animatorSet.start();
2.getBesselAnimator生成ValueAnimator,rankWidth, rankHeight为整个容器的宽高。
private ValueAnimator getBesselAnimator(final ImageView imageView, int rankWidth, int rankHeight){
float point0[] = new float[2];
point0[0] = rankWidth/2;
point0[1] = rankHeight;
float point1[] = new float[2];
point1[0] = (float) ((rankWidth) * (0.10)) + (float) (Math.random() * (rankWidth) * (0.8));
point1[1] = (float) (rankHeight - Math.random() * rankHeight * (0.5));
float point2[] = new float[2];
point2[0] = (float) (Math.random() * rankWidth);
point2[1] = (float) (Math.random() * (rankHeight - point1[1]));
float point3[] = new float[2];
point3[0] = (float) (Math.random() * rankWidth);
point3[1] = 0;
//point1,point2曲线中间点 四个点顺序上到下,p3,p2,p1,p0
BesselEvaluator besselEvaluator = new BesselEvaluator(point1, point2);
//point0, point3,这两个参数最终会传到BesselEvaluator中的evaluate方法中的point0, point3
ValueAnimator valueAnimator = ValueAnimator.ofObject(besselEvaluator, point0, point3);
valueAnimator.setDuration(riseDuration);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float[] currentPosition = new float[2];
currentPosition = (float[]) animation.getAnimatedValue();
imageView.setTranslationX(currentPosition[0]);
imageView.setTranslationY(currentPosition[1]);
}
});
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
removeView(imageView);
imageView.setImageDrawable(null);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
return valueAnimator;
}
3.BesselEvaluator具体计算,实现了TypeEvaluator接口,泛型float数组
public class BesselEvaluator implements TypeEvaluator<float[]> {
private float point1[] = new float[2], point2[] = new float[2];
public BesselEvaluator(float[] point1, float[] point2){
this.point1 = point1;
this.point2 = point2;
}
/**
*
* @param fraction 变量
* @param point0 贝塞尔曲线起点
* @param point3 贝塞尔曲线终点
* @return 因为需要的点是从下到上....所以p0,p1,p2,p3的点是从下打上的
*/
@Override
public float[] evaluate(float fraction, float[] point0, float[] point3) {
float[] currentPosition = new float[2];
//贝塞尔公式计算X点
currentPosition[0] = point0[0] * (1 - fraction) * (1 - fraction) * (1 - fraction)
+ point1[0] * 3 * fraction * (1 - fraction) * (1 - fraction)
+ point2[0] * 3 * (1 - fraction) * fraction * fraction
+ point3[0] * fraction * fraction * fraction;
//贝塞尔公式计算Y点
currentPosition[1] = point0[1] * (1 - fraction) * (1 - fraction) * (1 - fraction)
+ point1[1] * 3 * fraction * (1 - fraction) * (1 - fraction)
+ point2[1] * 3 * (1 - fraction) * fraction * fraction
+ point3[1] * fraction * fraction * fraction;
return currentPosition;
}
}
最后,该文章仅仅是个人阅读开源控件:https://github.com/Yasic/QQBubbleView 阅读记录。