Search code examples
androidandroid-animationscaleanimation

Android ScaleAnimation not centre on screen


I'm running into a bit of a problem when pragmatically setting a Scale animation on an ImageView. Basically I have and Image view set center H/V, but when I apply the animation it off sets it:(...Iv read about pivot x/y being set to 50% but it wont seem to work, any feed back would be great...Screen shot below

    AnimationSet set = new AnimationSet(true);
    Animation animation = new ScaleAnimation(1, 1.2f, 1, 1.2f, Animation.ABSOLUTE, 50,    Animation.ABSOLUTE, 50);
    animation.setDuration(500);
    animation.setRepeatCount(-1);
    animation.setRepeatMode(Animation.RESTART);
    set.addAnimation(animation);

    animation = new ScaleAnimation(1.2f, 1.4f, 1.2f, 1.4f, Animation.ABSOLUTE, 50, Animation.ABSOLUTE, 50);
    animation.setDuration(500);
    animation.setRepeatCount(-1);
    animation.setRepeatMode(Animation.RESTART);
    set.addAnimation(animation);

    animation = new ScaleAnimation(1.4f, 1.0f, 1.4f, 1.0f, Animation.ABSOLUTE, 50, Animation.ABSOLUTE, 50);
    animation.setDuration(500);
    animation.setRepeatCount(-1);
    animation.setRepeatMode(Animation.RESTART);
    set.addAnimation(animation);

    image_view.startAnimation(set);

enter image description here


Solution

  • maybe Animation.RELATIVE_TO_SELF or Animation.RELATIVE_TO_PARENT will do the work? example:

    new ScaleAnimation(1, 1.2f, 1, 1.2f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f));
    

    treat both 0.5f like 50%, counting from left/top