仿抖音点赞效果实现 ——————自定义View

玩过抖音的人应该都知道抖音的点赞效果挺酷炫的,而作为码农我们一定想知道它是怎么实现的。先上效果图:

实现原理非常的简单,直接上代码:

/**
 * Description:   自定义 仿抖音动画类
 * Data:2018/12/7-下午2:21
 * Email:as752497576@gmail.com
 * 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();

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是Android仿抖音评论功能实现的代码示例: 1. 布局文件 ``` <RelativeLayout android:id="@+id/layout_comment" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/bg_comment" android:padding="10dp"> <EditText android:id="@+id/edit_comment" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:hint="请输入评论内容" android:textColor="@color/black" android:textSize="16sp" /> <TextView android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="发送" android:textColor="@color/colorAccent" android:textSize="16sp" /> </RelativeLayout> ``` 2. Activity中的代码 ``` private EditText mEditComment; private TextView mBtnSend; private RelativeLayout mLayoutComment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mEditComment = findViewById(R.id.edit_comment); mBtnSend = findViewById(R.id.btn_send); mLayoutComment = findViewById(R.id.layout_comment); mBtnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String comment = mEditComment.getText().toString(); if (!TextUtils.isEmpty(comment)) { // 发送评论内容到服务器 sendComment(comment); } } }); } private void sendComment(String comment) { // 发送评论内容到服务器的代码 // ... // 接收服务器返回的评论结果 boolean success = true; // 根据服务器返回的结果设置success的值 if (success) { // 添加新评论 TextView tvComment = new TextView(this); tvComment.setText(comment); tvComment.setTextColor(getResources().getColor(R.color.black)); tvComment.setTextSize(16); mLayoutComment.addView(tvComment); // 清空评论输入框 mEditComment.setText(""); } else { Toast.makeText(this, "评论失败,请稍后重试", Toast.LENGTH_SHORT).show(); } } ``` 需要注意的是,上述代码仅为示例代码,具体实现还需要根据自己的需求进行修改和完善。比如需要处理用户登录状态、评论内容的过滤和审核等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值