1

Hi I am borrowing Seth's method found here to animate my viewGroups. It works great but in the wrong direction - This is my first time using/extending the Animation class and I cant figure how to decrease the size of my view -This expands it.

Below is the class and how I use it. Any help will be greatly appreciated.

public class ContainerAnim extends Animation {
    int targetWidth;
    View view;
    boolean opened;

    public ContainerAnim(View v, int targetWidth, boolean opened) {
        this.view = v;
        this.targetWidth = targetWidth;
        this.opened = opened;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        int newWidth;
        if (opened) {
            newWidth = (int) (targetWidth * interpolatedTime);
        } else {
            newWidth = (int) (targetWidth * (1 - interpolatedTime));
        }
        view.getLayoutParams().width = newWidth;
        view.requestLayout();
    }

    @Override
    public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

Usage:

... case R.id.menu_shrink:
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            FrameLayout frame = (FrameLayout) findViewById(R.id.listFragment_container);

            ContainerAnim set = new ContainerAnim(frame, 100, true);
            set.setDuration(200);
            LayoutAnimationController c = new LayoutAnimationController(set,
                    0.25f);
            frame.setLayoutAnimation(c);
            frame.startLayoutAnimation();

            ft.commit();
...
Community
  • 1
  • 1
  • what happens when you change (targetWidth * interpolatedTime) to (targetWidth / interpolatedTime) in apply transformation? – Paul Nikonowicz Feb 13 '12 at 21:35
  • @Paul Nikonowicz The view expands and then returns to the original width –  Feb 13 '12 at 21:40
  • how about making new width -= 10 so hat the width shrinks? – Paul Nikonowicz Feb 13 '12 at 21:44
  • @Paul Nikonowicz I've tried that too. You would think the view would shrink to 10dp but it expands 10 pixels...? –  Feb 13 '12 at 21:47
  • i mean do it like this: view.getLayoutParams().width -= 10 – Paul Nikonowicz Feb 13 '12 at 21:50
  • @Paul Nikonowic Yea thnx; it expands still. I did find if I remove the layoutWeight in the XML file it works with my original class above but the children doesnt replace the lost space. –  Feb 13 '12 at 22:10
  • if all else fails, create a different view with less styling information and see if it works. try to make it work in a simple situation. – Paul Nikonowicz Feb 13 '12 at 22:13

1 Answers1

0

I think you've mixed up the usage of that third parameter (the boolean). You call it "opened", and pass it true, because you want to shrink it. However, this is the opposite of the usage in my original code! In my code, the boolean was true if the view was closed but I wanted it to grow.

"and d = a boolean which specifies the direction (true = expanding, false = collapsing)."

If you swap the condition in the animation class to if(!opened){}, it should work. Or pass it false instead of true. Or pass it true, but change the variable to "closed" instead of "opened".

-Seth

Seth Nelson
  • 2,598
  • 4
  • 22
  • 29