0

I have a RelativeLayout with a ImageView inside it, it is aligned to the right of the screen with

 android:layout_alignParentRight="true"

I then apply an animation to the layout, that will scale it to twice it size (on X-axis), but for some reason the alignment is broken and the layout (and image within it) stretches outside of the screen to the right. I was expecting it to grow to the left since it is aligned to the parent right.

I guess I could apply a translate to -X at the same time, but there are problems with this as well (1. it´s a bit complicated to compute, 2. the fillAfter never seems to work when using an AnimationSet).

Does anyone know how to solve this problem smoothly? :)

dac2009
  • 3,521
  • 1
  • 22
  • 22

1 Answers1

0

Apparently, Androids scaling is doing all sorts of bad stuff, for example it does not scale a 9-patch correct. Here is a custom animation for scaling that will solve the above and is compatible with 9-patch images. I also got some info here for the basics, this is just a rewrite of his solution: How to implement expandable panels in Android?

public class ExpandAnimation extends Animation
{
    private final int mStartWidth;
    private final int mDeltaWidth;
    private View view;

    public ExpandAnimation(View view, int startWidth, int endWidth)
    {
        mStartWidth = startWidth;
        mDeltaWidth = endWidth - startWidth;
        this.view = view;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t)
    {
        android.view.ViewGroup.LayoutParams lp = view.getLayoutParams();
        lp.width = (int) (mStartWidth + mDeltaWidth * interpolatedTime);
        view.setLayoutParams(lp);
        view.invalidate();
    }

    @Override
    public boolean willChangeBounds()
    {
        return true;
    }
}
Community
  • 1
  • 1
dac2009
  • 3,521
  • 1
  • 22
  • 22