2

I have read many articles about android tranlsation animation. And I know that only one way to use LayoutParams() but I need to animate five buttons. I have create example that can aminate those buttons but my problem is that buttons don't have focus. How I can use LayoutParams() for those buttons? here sample code:

private AnimationSet animate(final Button button, long fromXValue,
        final long toXValue, long fromYDelta, final long toYValue,
        long startOffset){...}

float angle = 222.2f;
            int angle2 = 136;
            long x1 = Math.round((mRadius * Math.cos(angle)));
            long y1 = Math.round((mRadius * Math.sin(angle)));

            long x2 = (int) (mRadius * Math.cos(angle2));
            long y2 = (int) (mRadius * Math.sin(angle2));

            if (isShown && isFinished) {
                animationSet1 = animate(button2, 0, 0, mRadius, 0, 400);
                animationSet2 = animate(button3, x1, 0, y1, 0, 300);
                animationSet3 = animate(button4, -mRadius, 0, 0, 0, 200);
                animationSet4 = animate(button5, x2, 0, y2, 0, 100);
                animationSet5 = animate(button6, 0, 0, -mRadius, 0, 0);
                isShown = false;
            } else if (!isShown && isFinished) {
                animationSet1 = animate(button2, 0, 0, 0, mRadius, 0);
                animationSet2 = animate(button3, 0, x1, 0, y1, 100);
                animationSet3 = animate(button4, 0, -mRadius, 0, 0, 200);
                animationSet4 = animate(button5, 0, x2, 0, y2, 300);
                animationSet5 = animate(button6, 0, 0, 0, -mRadius, 400);
                isShown = true;
            }

Image shows how it should looks like image

EDITED I found solution using LayoutParams() that work almost ok. Here sample:

    private void animate(final Button button, long fromXValue,
        final long toXValue, long fromYDelta, final long toYValue,
        long startOffset) {
    AnimationSet animationSet = new AnimationSet(false);
    TranslateAnimation translateAnimation = new TranslateAnimation(
            fromXValue, toXValue, fromYDelta, toYValue);
    RotateAnimation rotateAnimation = null;
    int dp = getPixelsFromDp(20);
    if (!isShown) {
        rotateAnimation = new RotateAnimation(-180, 0, dp, dp);
    } else {
        rotateAnimation = new RotateAnimation(0, -180, dp, dp);
    }

    animationSet.addAnimation(rotateAnimation);
    animationSet.addAnimation(translateAnimation);
    animationSet.setDuration(100);
    // animationSet.setFillAfter(true);
    // animationSet.setFillEnabled(true);
    animationSet.setStartOffset(startOffset);
    animationSet.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            isFinished = false;
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            isFinished = true;
            Log.i("animation", "animation");
            RelativeLayout.LayoutParams params = (LayoutParams) button
                    .getLayoutParams();
            params.rightMargin -= toXValue;
            params.topMargin += toYValue;

            button.setLayoutParams(params);

        }
    });
    button.startAnimation(animationSet);
}

Buttons have focus. But remained one bad effect: during animation buttons are shown twice. The impression that buttons place on the right placed and after that shows animation again.

4

0 回答 0