最近需要完成這樣的一個(gè)效果:
sdasd.gif
看上去就是動(dòng)畫(huà)跟貝塞爾曲線的組合驰徊,但是因?yàn)閯?dòng)畫(huà)不熟悉侥袜,所以比較難以實(shí)現(xiàn),這里的代碼是摘取至 hcc大神的分享
LoveLikeLayout:
public class LoveLikeLayout extends RelativeLayout {
//線性插值器
private Interpolator line = new LinearInterpolator();
//加速插值器
private Interpolator acc = new AccelerateInterpolator();
//減速插值器
private Interpolator dce = new DecelerateInterpolator();
//先加速后減速插值器
private Interpolator accdec = new AccelerateDecelerateInterpolator();
//插值器數(shù)組
private Interpolator[] interpolators;
//隨機(jī)數(shù)
private Random mRandom = new Random();
//愛(ài)心圖片數(shù)組
private Drawable[] drawables;
//愛(ài)心圖片的高度
private int drawableHeight;
//愛(ài)心圖片的寬度
private int drawableWidth;
//參數(shù)值
private LayoutParams layoutParams;
//layout高度
private int measuredHeight;
//layout寬度
private int measuredWidth;
public LoveLikeLayout(Context context) {
this(context, null);
}
public LoveLikeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LoveLikeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
/**
* 初始化
*/
private void init() {
//初始化愛(ài)心圖片
drawables = new Drawable[3];
Drawable red = getResources().getDrawable(R.drawable.pl_red);
Drawable yellow = getResources().getDrawable(R.drawable.pl_yellow);
Drawable blue = getResources().getDrawable(R.drawable.pl_blue);
drawables[0] = red;
drawables[1] = yellow;
drawables[2] = blue;
//獲取愛(ài)心的寬高
drawableHeight = red.getIntrinsicHeight();
drawableWidth = red.getIntrinsicWidth();
//設(shè)置愛(ài)心的顯示位置
layoutParams = new LayoutParams(drawableWidth, drawableHeight);
layoutParams.addRule(CENTER_HORIZONTAL, TRUE);
layoutParams.addRule(ALIGN_PARENT_BOTTOM, TRUE);
//初始化動(dòng)畫(huà)插值器
interpolators = new Interpolator[4];
interpolators[0] = line;
interpolators[1] = acc;
interpolators[2] = dce;
interpolators[3] = accdec;
}
/**
* 測(cè)量layout的寬高
* 用于計(jì)算愛(ài)心的顯示位置
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measuredHeight = getMeasuredHeight();
measuredWidth = getMeasuredWidth();
}
/**
* 顯示愛(ài)心
*/
public void addLove() {
final ImageView imageView = new ImageView(getContext());
//隨機(jī)生成愛(ài)心顏色
imageView.setImageDrawable(drawables[mRandom.nextInt(3)]);
imageView.setLayoutParams(layoutParams);
addView(imageView);
Animator animtor = getAnimtor(imageView);
animtor.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
removeView(imageView);
super.onAnimationEnd(animation);
}
});
animtor.start();
}
/**
* 愛(ài)心的顯示和運(yùn)行軌跡動(dòng)畫(huà)組合實(shí)現(xiàn)
*
* @param target
* @return
*/
private Animator getAnimtor(View target) {
AnimatorSet enterAnimtorSet = getEnterAnimtorSet(target);
ValueAnimator bezierAnimtor = getBezierAnimtor(target);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(enterAnimtorSet);
animatorSet.playSequentially(enterAnimtorSet, bezierAnimtor);
animatorSet.setInterpolator(interpolators[mRandom.nextInt(4)]);
animatorSet.setTarget(target);
return animatorSet;
}
/**
* 愛(ài)心的顯示動(dòng)畫(huà)實(shí)現(xiàn)
*
* @param target
* @return
*/
private AnimatorSet getEnterAnimtorSet(View target) {
//愛(ài)心的3中動(dòng)畫(huà)組合 透明度 x,y軸的縮放
ObjectAnimator alpha = ObjectAnimator.ofFloat(target, View.ALPHA, 0.2f, 1f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(target, View.SCALE_X, 0.2f, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(target, View.SCALE_Y, 0.2f, 1f);
//組合動(dòng)畫(huà)
AnimatorSet set = new AnimatorSet();
set.setDuration(500);
set.setInterpolator(new LinearInterpolator());
set.playTogether(alpha, scaleX, scaleY);
set.setTarget(target);
return set;
}
/**
* 愛(ài)心運(yùn)動(dòng)軌跡的動(dòng)畫(huà)實(shí)現(xiàn)
*
* @param target
* @return
*/
private ValueAnimator getBezierAnimtor(final View target) {
BezierEvaluator evaluator = new BezierEvaluator(getPoint(2), getPoint(1));
ValueAnimator animator = ValueAnimator.ofObject(evaluator,
new PointF((measuredWidth - drawableWidth) / 2, measuredHeight - drawableHeight),
new PointF(mRandom.nextInt(getWidth()), 0));
animator.setDuration(3000);
animator.setTarget(target);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
//獲取貝塞爾曲線的運(yùn)動(dòng)軌跡 讓愛(ài)心跟隨著移動(dòng)
PointF animatedValue = (PointF) valueAnimator.getAnimatedValue();
target.setX(animatedValue.x);
target.setY(animatedValue.y);
//增加透明度的變化
target.setAlpha(1 - valueAnimator.getAnimatedFraction());
}
});
return animator;
}
/**
* 獲取中間的兩個(gè)點(diǎn)
*
* @return
*/
private PointF getPoint(int scale) {
PointF pointF = new PointF();
pointF.x = mRandom.nextInt((measuredWidth - 100));
pointF.y = mRandom.nextInt((measuredHeight - 100)) / scale;
return pointF;
}
}
BezierEvaluator:
public class BezierEvaluator implements TypeEvaluator<PointF> {
private PointF pointF1;
private PointF pointF2;
public BezierEvaluator(PointF pointF1, PointF pointF2) {
this.pointF1 = pointF1;
this.pointF2 = pointF2;
}
@Override
public PointF evaluate(float time, PointF startValue, PointF endValue) {
float timeLeft = 1.0f - time;
PointF point = new PointF();//結(jié)果
point.x = timeLeft * timeLeft * timeLeft * (startValue.x)
+ 3 * timeLeft * timeLeft * time * (pointF1.x)
+ 3 * timeLeft * time * time * (pointF2.x)
+ time * time * time * (endValue.x);
point.y = timeLeft * timeLeft * timeLeft * (startValue.y)
+ 3 * timeLeft * timeLeft * time * (pointF1.y)
+ 3 * timeLeft * time * time * (pointF2.y)
+ time * time * time * (endValue.y);
return point;
}
}
用法:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#80000000"
android:layout_height="match_parent"
android:orientation="vertical">
<com.stay4it.widgets.ui.LoveLikeLayout
android:id="@+id/id_LoveLikeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="點(diǎn)擊" />
</LinearLayout>
public class LoveActivity extends AppCompatActivity {
private LoveLikeLayout mLoveLikeLayout;
private Button btn;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_love);
mLoveLikeLayout = (LoveLikeLayout) findViewById(R.id.id_LoveLikeLayout);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mLoveLikeLayout.addLove();
}
});
}
先把代碼摘錄下來(lái)祥诽,后面再研究。瓮恭。