如何从Object Animator OfObject(..)方法动画传递视图引用?

时间:2013-10-01 05:38:45

标签: android

我需要沿着圆形路径移动圆圈,因为我正在使用Object Animator和Path评估器。所有信息都是动态的,这意味着当我收到响应时它会发生变化,因此可以在运行时更改圆的总数。这是方法的一部分,其中所有圆圈根据它们在屏幕上的位置定位自己,即如果用户释放触摸,则此方法被调用 - :

public void slideDownSetToCenter() {

    for (int i = 0; i < leftCurrentRunningAnimation.length; i++) {

        Logger.i(TAG, "in slide down animation");
        if (leftReadyToMove[i]) {

            if (leftUpQueue.contains(i)) {
                leftUpQueue.remove(i);
            }
            leftAngle = leftSlice * ++leftAllCirclesAngles[i];
            Logger.i("circle leftAngle points of curve down", String.valueOf(leftAllCirclesAngles[i]));
            if (leftAngle > leftAngleTop) {
                if (i < leftCurrentRunningAnimation.length - 1) {
                    leftReadyToMove[i + 1] = true; // set next circle to
                                                    // move
                }
            }
            Arrays.fill(leftCurrentRunningAnimation, false);
            leftCurrentRunningAnimation[i] = true;

            Logger.i(TAG, "leftAngle" + leftAngle);

            if (leftReadyToMove[i] == true && leftAngle == leftAngleDownOut) {

                leftReadyToMove[i] = false;
                leftDownStack.add(i);
            }

            xPosition = (int) (leftCircleCenterX + leftCircleX * Math.cos(leftAngle));
            yPosition = (int) (leftCircleCenterY + leftCircleY * Math.sin(leftAngle));

            Path = new AnimatorPath();
            Path.moveTo(xPosition, yPosition);
            Path.lineTo(xPosition, yPosition);
            while (true) {
                if (leftAngle == leftAngleDownOut) {
                    break;
                }

                leftAngle = leftSlice * ++leftAllCirclesAngles[i];
                xPosition = (int) (leftCircleCenterX + leftCircleX * Math.cos(leftAngle));
                yPosition = (int) (leftCircleCenterY + leftCircleY * Math.sin(leftAngle));
                Path.lineTo(xPosition, yPosition);
                Logger.i(TAG, "path.........");
            }
            slideCircleAnimator = ObjectAnimator.ofObject(DynamicCircleSwipeAnimation.this, "leftButtonLocationDynamic", new PathEvaluator(), Path.getPoints().toArray());
            slideCircleAnimator.setInterpolator(linearInterpolator);
            slideCircleAnimator.setDuration(500);
            context.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    slideCircleAnimator.start();
                }
            });

        }
        break;
    }
}

这是Object animator的动画方法 - :

  public void setLeftButtonLocationDynamic(final PathPoint newLoc) {

    for (int i = 0; i < leftCurrentRunningAnimation.length; i++) {
        if (leftCurrentRunningAnimation[i] == true) {
            Logger.i("current button id", String.valueOf(i));
            leftArrayOfButtons[i].setTranslationX(newLoc.mX);
            leftArrayOfButtons[i].setTranslationY(newLoc.mY);
            break;
        }
    }
}

这里我的动画没有以正确的方式发生'为什么我需要从Object.OfObject()方法传递我的视图引用,以便我可以在setLeftButtonLocationDynamic(最终的PathPoint newLoc,View v)中获取它。有人有任何想法我怎么能这样做?我搜索了很多并尝试开发自定义类的ObjectAnimator,ValueAnimator和ProperyViewHolder类但是当我从谷歌开源复制它然后我得到错误。任何帮助是可观的?

1 个答案:

答案 0 :(得分:-1)

为了达到这个目的,我改变了移动所有圆圈的方法。之前,我正在使用ObjectAnimator逐个移动圆圈,现在我正在使用Animator设置x位置和y位置。我同时播放两个动画。

以下是示例 - :

public void SlideDownFromRightAnimation() {

    int rightArrayOfButtonsLength = rightArrayOfButtons.length;

    Logger.i(TAG, "queue " + rightUpQueue.size() + "= stack " + rightDownStack.size());
    if (rightUpQueue.size() == rtNumberOfButtons) {
        Arrays.fill(rightReadyToMove, false);
        rightReadyToMove[0] = true;
        rightUpQueue.poll();
    } else if (rightDownStack.size() == rtNumberOfButtons) {
        rightUpQueue.clear();
        for (int i = 0; i < rightArrayOfButtonsLength; i++) {
            rightUpQueue.add(rightDownStack.pop());
            rightAllCirclesAngles[i] = rightCircleUpOutPosition;
        }
        Arrays.fill(rightReadyToMove, false);
        rightReadyToMove[0] = true;
    }

    for (int i = 0; i < rightArrayOfButtonsLength; i++) {

        Logger.i(TAG, "in slide down animation");
        if (rightReadyToMove[i]) {

            if (rightUpQueue.contains(rightArrayOfButtons[i].getTag())) {
                rightUpQueue.remove(i);
            }
            rightAngle = rightSlice * --rightAllCirclesAngles[i];
            Logger.i("circle rightAngle points of curve down", String.valueOf(rightAllCirclesAngles[i]));
            if (rightAngle <= rightAngleTop) {
                if (i < rightArrayOfButtonsLength - 1) {
                    rightReadyToMove[i + 1] = true; // set next circle to
                                                    // move
                }
            }

            Logger.i(TAG, "rightAngle" + rightAngle);

            if (rightAngle <= rightAngleDownOut) {

                rightReadyToMove[i] = false;
                rightDownStack.add((Integer)rightArrayOfButtons[i].getTag());
                rightAllCirclesAngles[i] = rightCircleDownOutPosition;
                rightStartAnimation(rightAngle, null, i);
            } else {
                double rightAngleNext = rightSlice * rightAllCirclesAngles[i];

                if (rightAngleNext <= rightAngleDownOut) {

                    rightReadyToMove[i] = false;
                    rightDownStack.add((Integer)rightArrayOfButtons[i].getTag());
                }

                rightStartAnimation(rightAngle, rightAngleNext, i);
            }

        }

    }

}

这里是右轮动画功能 - :

public void rightStartAnimation(Double rightAngle, Double rightAngleNext, int bttnId) {
    xPosition = (int) (rightCircleCenterX + rightCircleX * Math.cos(rightAngle));
    yPosition = (int) (rightCircleCenterY + rightCircleY * Math.sin(rightAngle));

    if (rightAngleNext != null) {
        xPosition = (int) (rightCircleCenterX + rightCircleX * Math.cos(rightAngleNext));
        yPosition = (int) (rightCircleCenterY + rightCircleY * Math.sin(rightAngleNext));
    }

    final ObjectAnimator animation1 = ObjectAnimator.ofFloat(rightArrayOfButtons[bttnId], "x", rightArrayOfButtons[bttnId].getX(), xPosition);
    animation1.setDuration(0);
    animation1.setInterpolator(linearInterpolator);
    final ObjectAnimator animation2 = ObjectAnimator.ofFloat(rightArrayOfButtons[bttnId], "y", rightArrayOfButtons[bttnId].getY(), yPosition);
    animation2.setDuration(0);
    animation2.setInterpolator(linearInterpolator);
    final AnimatorSet set = new AnimatorSet();
    set.setInterpolator(linearInterpolator);
    context.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            set.playTogether(animation1, animation2);
            set.start();
        }
    });
}