如何将视图 LayoutParams 宽度设置得更小?
你好,我借用了Seth的方法此处 为我的 viewGroup 制作动画。它工作得很好,但方向错误 - 这是我第一次使用/扩展动画类,我不知道如何减小视图的大小 - 这扩展了它。
下面是该类以及我如何使用它。任何帮助将不胜感激。
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;
}
}
用法:
... 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();
...
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();
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您混淆了第三个参数(布尔值)的用法。您称其为“打开”,并将其传递为 true,因为您想缩小它。然而,这与我原来代码中的用法相反!在我的代码中,如果视图关闭,则布尔值为 true,但我希望它增长。
“d = 指定方向的布尔值(true = 展开,false = 折叠)。”
如果将动画类中的条件交换为 if(!opened){},它应该可以工作。或者将其传递为 false 而不是 true。或者传递 true,但将变量更改为“关闭”而不是“打开”。
-赛斯
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