玩过抖音的人应该都知道抖音的点赞效果挺酷炫的,而作为码农我们一定想知道它是怎么实现的。先上效果图:
实现原理非常的简单,直接上代码:
/** * Description: 自定义 仿抖音动画类 * Data:2018/12/7-下午2:21 * Email:[email protected] * Author: feipeng */ public class YALikeAnimationView extends RelativeLayout { private Context mContext; float[] num = {-30, -20, 0, 20, 30};//随机心形图片角度 private float dispatchXPosition; private float dispatchYPosition; public YALikeAnimationView(Context context) { super(context); initView(context); } public YALikeAnimationView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context); } public YALikeAnimationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private void initView(Context context) { mContext = context; } @Override public boolean onTouchEvent(MotionEvent ev) { dispatchXPosition = ev.getX(); dispatchYPosition = ev.getY(); return super.onTouchEvent(ev); } public void startAnimation(){ final ImageView imageView = new ImageView(getContext()); LayoutParams params = new LayoutParams(300, 300); params.leftMargin = (int) dispatchXPosition - 150; params.topMargin = (int) dispatchYPosition - 300; imageView.setImageDrawable(getResources().getDrawable(R.mipmap.icon_heart)); imageView.setLayoutParams(params); addView(imageView); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.play(scale(imageView, "scaleX", 2f, 0.9f, 100, 0)) .with(scale(imageView, "scaleY", 2f, 0.9f, 100, 0)) .with(rotation(imageView, 0, 0, num[new Random().nextInt(4)])) .with(alpha(imageView, 0, 1, 100, 0)) .with(scale(imageView, "scaleX", 0.9f, 1, 50, 150)) .with(scale(imageView, "scaleY", 0.9f, 1, 50, 150)) .with(translationY(imageView, 0, -600, 800, 400)) .with(alpha(imageView, 1, 0, 300, 400)) .with(scale(imageView, "scaleX", 1, 3f, 700, 400)) .with(scale(imageView, "scaleY", 1, 3f, 700, 400)); animatorSet.start(); animatorSet.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); removeViewInLayout(imageView); } }); } public static ObjectAnimator scale(View view, String propertyName, float from, float to, long time, long delayTime) { ObjectAnimator translation = ObjectAnimator.ofFloat(view , propertyName , from, to); translation.setInterpolator(new LinearInterpolator()); translation.setStartDelay(delayTime); translation.setDuration(time); return translation; } public static ObjectAnimator translationX(View view, float from, float to, long time, long delayTime) { ObjectAnimator translation = ObjectAnimator.ofFloat(view , "translationX" , from, to); translation.setInterpolator(new LinearInterpolator()); translation.setStartDelay(delayTime); translation.setDuration(time); return translation; } public static ObjectAnimator translationY(View view, float from, float to, long time, long delayTime) { ObjectAnimator translation = ObjectAnimator.ofFloat(view , "translationY" , from, to); translation.setInterpolator(new LinearInterpolator()); translation.setStartDelay(delayTime); translation.setDuration(time); return translation; } public static ObjectAnimator alpha(View view, float from, float to, long time, long delayTime) { ObjectAnimator translation = ObjectAnimator.ofFloat(view , "alpha" , from, to); translation.setInterpolator(new LinearInterpolator()); translation.setStartDelay(delayTime); translation.setDuration(time); return translation; } public static ObjectAnimator rotation(View view, long time, long delayTime, float... values) { ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", values); rotation.setDuration(time); rotation.setStartDelay(delayTime); rotation.setInterpolator(new TimeInterpolator() { @Override public float getInterpolation(float input) { return input; } }); return rotation; } }
OK!完美实现!最后贴上调用方法
YALikeAnimationView ya = findViewById(R.id.YALikeAnimationView); ya.startAnimation();