如何将视图 LayoutParams 宽度设置得更小?

发布于 2025-01-05 04:14:33 字数 1752 浏览 1 评论 0原文

你好,我借用了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心如荒岛 2025-01-12 04:14:33

我认为您混淆了第三个参数(布尔值)的用法。您称其为“打开”,并将其传递为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文