点赞~~飘爱心~~

代码比较简单、给需要的朋友参考,利用一个对象池、避免爱心实例过多~

`public class LovingHeartView extends FrameLayout {
  private List<TextView> pool;
  private long lastClicktime;

  public LovingHeartView(@NonNull Context context) {
    super(context);
    initView(context);
  }

  public LovingHeartView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    initView(context);
  }

  public LovingHeartView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView(context);
  }
  public void initView(Context context){
    pool = new ArrayList<>();
    setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
          if(System.currentTimeMillis() - lastClicktime > 500){
            lastClicktime = System.currentTimeMillis();
            return false;
          }
          lastClicktime = System.currentTimeMillis();
          TextView textView;
          if(ListUtils.isEmpty(pool)){
            textView = new TextView(context);
            textView.setText("爱心");
            textView.setTextSize(20);
            textView.setTextColor(randomColor());
            textView.setX(event.getX());
            textView.setY(event.getY());
            addView(textView);
            textView.setPivotX(textView.getWidth()/2f);
            textView.setPivotY(textView.getHeight()/2f);
   			
            textView.animate().translationY(event.getY() - 100).alpha(0.2f).scaleX(2).scaleY(2)
                .setDuration(1500);
            textView.animate().setListener(new Animator.AnimatorListener() {
                  @Override
                  public void onAnimationStart(@NonNull Animator animation) {

                  }

                  @Override
                  public void onAnimationEnd(@NonNull Animator animation) {
                    removeView(textView);
                    textView.setAlpha(1f);
                    textView.setScaleX(1);
                    textView.setScaleY(1);
                    pool.add(textView);
                  }

                  @Override
                  public void onAnimationCancel(@NonNull Animator animation) {

                  }

                  @Override
                  public void onAnimationRepeat(@NonNull Animator animation) {

                  }
                });
          }else {
            //来自对象池
            textView = pool.remove(0);
            textView.setX(event.getX());
            textView.setY(event.getY());
            addView(textView);
            textView.animate().translationY(event.getY() - 100).alpha(0.2f).scaleX(2).scaleY(2)
                .setDuration(1500);
          }
          return true;
        }
        return false;
      }
    });
  }
  private int randomColor(){
    Random rnd = new Random();
    return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
  }
}`
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值