点赞效果初步整理

点赞效果初步整理


整理了下点赞效果,记录一下

  • Android view之点赞容易,取消不易
  • 一步一步教你实现Periscope点赞效果

效果

thumbUp1.gif

画心

贝塞尔曲线关键点:



        path.moveTo((float) (0.5 * realWidth) + rectFlove.left, (float) (startYScale * realHeight) + rectFlove.top);
        path.cubicTo((float) (0.15 * realWidth) + rectFlove.left, (float) (-0.35 * realHeight + rectFlove.top),
                (float) (-0.4 * realWidth) + rectFlove.left, (float) (0.45 * realHeight) + rectFlove.top,
                (float) (0.5 * realWidth) + rectFlove.left, (float) (realHeight * 0.8 + rectFlove.top));

爱心边长为1

起点(0.5,startYScale)

P1(0.15,-0.35)

P2(-0.4,0.45)

终点(0.5,0.8)

点赞效果初步整理_第1张图片
heart1.PNG

长按效果判断


  @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        if (MotionEvent.ACTION_DOWN == event.getAction()) {
            startX = x;
            startY = y;
            mCounter++;
            isReleased = false;
            isMoved = false;
            postDelayed(mLongPressRunnable, 1500);
            return true;
        } else if (MotionEvent.ACTION_MOVE == event.getAction()) {
            if (isMoved)
                return false;
            if (Math.abs(startX - x) > TOUCH_SLOP
                    || Math.abs(startY - y) > TOUCH_SLOP) {
                // 移动超过阈值,则表示移动了
                isMoved = true;
                return false;
            }
        } else if (MotionEvent.ACTION_UP == event.getAction()) {
            isReleased = true;
            if(isLongPressed){
                Log.e(TAG,"长按结束了");
                isLongPressed=false;

                if (getSearchView().getTag(mTagKey) == null || !(Boolean) getSearchView().getTag(mTagKey)) {
                    startLikeAnim(LikeType.like);
                }

                if(heartLayout!=null){
                    heartLayout.stopLoop();
                }

                return  true;
            }

            if (Math.abs(event.getX() - startX) < 5 &&
                    Math.abs(event.getY() - startY) < 5) {
                if (rectFloveBg.contains(event.getX(), event.getY())) {
                    if (getSearchView().getTag(mTagKey) == null || !(Boolean) getSearchView().getTag(mTagKey)) {
                        startLikeAnim(LikeType.like);
                    } else if ((Boolean) getSearchView().getTag(mTagKey)) {
                        if (mUnLikeType == LikeType.broken) {
                            startLikeAnim(LikeType.broken);
                        } else if (mUnLikeType == LikeType.unlike) {
                            startLikeAnim(LikeType.unlike);
                        }
                    }
                }
                return true;
            }
        }
        return false;
    }

接收到action_down 时延时1.5秒 开启线程 ,在线程里判断标志来决定是否是长按事件

 mLongPressRunnable = new Runnable() {
            @Override
            public void run() {

                mCounter--;
                if(mCounter>0||isReleased||isMoved)
                    return;
                isLongPressed=true;
                if(heartLayout!=null){
                    heartLayout.autoLoop();
                }

            }
        };

autoLoop 和stopLoop 则是开启结束轮询

飘心动画的轮询

试一下rxjava吧:


 public void autoLoop(){
        if(subscription==null||subscription.isUnsubscribed()){
            subscription= Observable.interval(0,300, TimeUnit.MILLISECONDS)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1() {
                        @Override
                        public void call(Long aLong) {
                           
                            addHeart();
                        }
                    });
        }
    }

下载

你可能感兴趣的:(点赞效果初步整理)