使用ValueAnimator逐渐执行Paint.SetColor()会导致奇怪的结果

时间:2017-12-25 13:25:03

标签: java android animation ondraw

我想逐渐设置Paint颜色,但它不会按照预期的方式工作。如果用户选择了项目并且正在移动,则颜色颜色仅会更改。否则,油漆颜色不会改变。造成这种情况的原因是什么?

enter image description here

    @Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {

    if (actionState == ACTION_STATE_SWIPE) {
        if (optionsState == OptionState.UPVOTE) {
            drawUpvoteOption(c, viewHolder.itemView, dX);
        } else if (optionsState == OptionState.DOWNVOTE) {
            if (!isRightSwipeMaxed(dX)) drawDownvoteOption(c, viewHolder.itemView, dX);
            else drawDownvoteOption(c, viewHolder.itemView, MAX_RIGHT_SWIPE);
        }

        setTouchListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

    if (dX == 0) {
        optionsState = OptionState.NONE;
    }

    if (!isRightSwipeMaxed(dX)) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    } else {
        super.onChildDraw(c, recyclerView, viewHolder, MAX_RIGHT_SWIPE, dY, actionState, isCurrentlyActive);
    }
}

private void drawDownvoteOption(Canvas c, View itemView, float dX) {
    rightRect.set(itemView.getRight() + dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
    setRightPaintColorAnimate(Color.RED);
    c.drawRect(rightRect, rightPaint);
    drawText("DOWNVOTE", c, rightRect, rightPaint);
}

private void setRightPaintColorAnimate(int targetColor) {
    ValueAnimator colorAnimator = ValueAnimator.ofObject(new ArgbEvaluator(), rightPaint.getColor(), targetColor);
    colorAnimator.setInterpolator(new LinearInterpolator());
    colorAnimator.setRepeatCount(ValueAnimator.INFINITE);
    colorAnimator.setDuration(300);
    colorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            //This only works if the item is currently moving
            rightPaint.setColor((int) valueAnimator.getAnimatedValue());
        }
    });
    colorAnimator.start();
}

0 个答案:

没有答案