问题
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();
...
回答1:
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
来源:https://stackoverflow.com/questions/9268365/how-do-i-set-my-view-layoutparams-width-smaller