NoobMe
NoobMe

Reputation: 544

Scale Animation behaving like translate animation

i am implementing scale animation for practice in a game.. i am following all guides but something weird is happening always instead of making the object larger. the animation not only makes the image larger but also moves it even though i am not using translate animation...

here the xml file

<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false"    
 xmlns:android="http://schemas.android.com/apk/res/android">
<scale
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="1.0"
    android:toXScale="1.4"
    android:fromYScale="1.0"
    android:toYScale="1.4"
    android:pivotX="50%"
    android:pivotY="50%"
    android:fillAfter="false"
    android:duration="1000" />
 </set>

and here it is called in java

private void startAnimation(final ImageView imageView) {
        final Animation popAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.scalebounce);
        imageView.startAnimation(popAnim);
        popAnim.setAnimationListener(new AnimationListener() {

            public void onAnimationStart(Animation animation) {

            }

            public void onAnimationRepeat(Animation animation) {

            }

            public void onAnimationEnd(Animation animation) {
                imageView.clearAnimation();
                imageView.setVisibility(View.GONE);
            }
        });
    }

its really weird i have tried all the solutions i have found in the net but no luck it still does that translate behavior even though it is only scale... im making the image from small point then it becomes bigger(normal size). but i dont want it to move, just to be in its place. thanks :)

P.S i've been observing the animation and it seems that it takes larger space thats why it looks like it is moving..why does it takes larger space? not the actual image size?

Upvotes: 7

Views: 3826

Answers (2)

Gore
Gore

Reputation: 51

I had the same problem. I dont know if it is almost the same like mine but maby it can help. Ihad buttons in a scrollview with a relativelayout in a relativelayout.When i wanted to animate the scale of my buttons ther was the same problem like yours, so i made it in java code and not xml, but there was no difference between RELATIVE_TO_PARENT and RELATIVE_TO_SELF. I found out that the pivot was everytime taken from the parent of the buttons(scrollview).Dont know why. so i decided to calculate the pivot of the scrollview that it is there where my button is:

ScaleAnimation animation = new ScaleAnimation(1.0f, 1.1f, 1.1f, 1.0f, Animation.RELATIVE_TO_PARENT, (float) (button1.getX()+ 45*ssu) / width, Animation.RELATIVE_TO_PARENT, (float) (button1.getY()+ 45*ssu) / (4 * height) );
                animation.setDuration(1000);
                animation.setRepeatMode(ValueAnimator.REVERSE);
                animation.setRepeatCount(animation.INFINITE);
                button1.startAnimation(animation);

You can see that i divided for the pivotX the part(x position of my button) threw the hole width and for the pivotY the part(y position of my button) thre the hole height of the scrollview(4*height) and added also 45*ssu to the buttons position because its the half of it and when you dont do this the pivot is in the left top corner. PS: ssu is a selfmade scaler because i set the width and height of the button in java code 90*ssu

Hope ii helps you :)

Upvotes: 3

Gil Moshayof
Gil Moshayof

Reputation: 16761

pivotX & pivotY need to be equal to around where your view should scale.

To demonstrate, here's a diagram to illustrate:

enter image description here

Your pivot point is currently outside the view, making it seem like your view is moving as well as scaling.

Set the pivotX / pivotY as 0 if you want the pivot point to be the top left corner of your view.

Set the pivotX = view.getWidth() / 2f & pivotY = view.getHeight() / 2f if you want the scale to be from the center (like the 1st image demonstrates)

Hope this helps :)

Upvotes: 5

Related Questions