点赞动画
public class FlyHeartView extends RelativeLayout {
private int defaultWidth = 200;
private long mDuration = 3000;
private int[] color = {0xFFFF34B3, 0xFF9ACD32, 0xFF9400D3, 0xFFEE9A00};
public FlyHeartView(Context context) {
super(context);
initFrameLayout();
}
public FlyHeartView(Context context, AttributeSet attrs) {
super(context, attrs);
initFrameLayout();
}
private void initFrameLayout() {
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(defaultWidth, ViewGroup.LayoutParams.WRAP_CONTENT);
setLayoutParams(params);
}
private ImageView createHeartView() {
ImageView heartIv = new ImageView(getContext());
LayoutParams params = new LayoutParams(defaultWidth / 2, defaultWidth / 2);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
heartIv.setLayoutParams(params);
heartIv.setImageResource(R.mipmap.ic_heart);
heartIv.setImageTintList(ColorStateList.valueOf(color[(int) (color.length * Math.random())]));
return heartIv;
}
public void startFly() {
final ImageView heartIv = createHeartView();
addView(heartIv);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(createTranslationX(heartIv))
.with(createTranslationY(heartIv))
.with(createScale(heartIv))
.with(createRotation(heartIv))
.with(createAlpha(heartIv));
animatorSet.start();
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
removeView(heartIv);
}
});
}
private Animator createTranslationX(View view) {
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", 0, (float) (defaultWidth * Math.random() / 4));
animator.setDuration(mDuration);
animator.setInterpolator(new CycleInterpolator((float) (3 * Math.random())));
return animator;
}
private Animator createTranslationY(View view) {
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationY", 0, -1000);
animator.setDuration(mDuration);
animator.setInterpolator(new AccelerateInterpolator());
return animator;
}
private Animator createScale(View view) {
ObjectAnimator animatorX = ObjectAnimator.ofFloat(view, "scaleX", 1, 1.5f);
ObjectAnimator animatorY = ObjectAnimator.ofFloat(view, "scaleY", 1, 1.5f);
AnimatorSet set = new AnimatorSet();
set.setDuration(mDuration);
set.setInterpolator(new AccelerateInterpolator());
set.play(animatorX).with(animatorY);
return set;
}
private Animator createAlpha(View view) {
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "alpha", 1, 0.1f);
animator.setDuration(mDuration);
animator.setInterpolator(new AccelerateInterpolator());
return animator;
}
private Animator createRotation(View view) {
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0, (float) (25f * Math.random()));
animator.setDuration(mDuration);
animator.setInterpolator(new CycleInterpolator((float) (6 * Math.random())));
return animator;
}
}