让 FrameLayout 展开,它的子元素也展开

发布于 2024-12-10 14:47:47 字数 1115 浏览 0 评论 0原文

如何动态更改 FrameLayout 的大小并且它的子级也得到扩展?

编辑:到目前为止,我得到了这个但不起作用:

public class ResizableArea extends FrameLayout {
    private View child;

    public ResizableArea(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() > 1){
            Vector2D p1 = new Vector2D(event.getX(0), event.getY(0));
            Vector2D p2 = new Vector2D(event.getX(1), event.getY(1));

            float dist = p1.minus(p2).getSize();

            resizeChildBy(dist);
        }
        return true;
    }

    private void resizeChildBy(float size) {
        LayoutParams params = (LayoutParams) child.getLayoutParams();
        params.height -= size;
        params.width -= size;
        child.setLayoutParams(params);
        invalidate();
    }


    @Override
    public void addView(View child) {
        if (getChildCount() != 0)
            throw new IllegalStateException("ResizableArea can only have one child attached!");

        this.child = child;
        super.addView(child);
    }

}

How can I change the size of a FrameLayout dynamically and it's childs get expanded too?

EDIT: So far, I got this but doesn't work:

public class ResizableArea extends FrameLayout {
    private View child;

    public ResizableArea(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() > 1){
            Vector2D p1 = new Vector2D(event.getX(0), event.getY(0));
            Vector2D p2 = new Vector2D(event.getX(1), event.getY(1));

            float dist = p1.minus(p2).getSize();

            resizeChildBy(dist);
        }
        return true;
    }

    private void resizeChildBy(float size) {
        LayoutParams params = (LayoutParams) child.getLayoutParams();
        params.height -= size;
        params.width -= size;
        child.setLayoutParams(params);
        invalidate();
    }


    @Override
    public void addView(View child) {
        if (getChildCount() != 0)
            throw new IllegalStateException("ResizableArea can only have one child attached!");

        this.child = child;
        super.addView(child);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

过度放纵 2024-12-17 14:47:47

事实上,这是正确的答案:

public class ResizableArea extends FrameLayout {
    private View child;

    public ResizableArea(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() > 1){
            Vector2D p1 = new Vector2D(event.getX(0), event.getY(0));
            Vector2D p2 = new Vector2D(event.getX(1), event.getY(1));

            float dist = p1.minus(p2).getSize();

            if (lastDist == 0) {
                lastDist = dist;
                return true;
            }

            resizeChildsBy(lastDist - dist);
            lastDist = dist;
        }
        return true;
    }

    private void resizeChildBy(float size) {
        LayoutParams params = (LayoutParams) child.getLayoutParams();
        params.height -= size;
        params.width -= size;
        child.setLayoutParams(params);
        invalidate();
    }


    @Override
    public void addView(View child) {
        if (getChildCount() != 0)
            throw new IllegalStateException("ResizableArea can only have one child attached!");

        this.child = child;
        super.addView(child);
    }

}

Actually, this is the right answer:

public class ResizableArea extends FrameLayout {
    private View child;

    public ResizableArea(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() > 1){
            Vector2D p1 = new Vector2D(event.getX(0), event.getY(0));
            Vector2D p2 = new Vector2D(event.getX(1), event.getY(1));

            float dist = p1.minus(p2).getSize();

            if (lastDist == 0) {
                lastDist = dist;
                return true;
            }

            resizeChildsBy(lastDist - dist);
            lastDist = dist;
        }
        return true;
    }

    private void resizeChildBy(float size) {
        LayoutParams params = (LayoutParams) child.getLayoutParams();
        params.height -= size;
        params.width -= size;
        child.setLayoutParams(params);
        invalidate();
    }


    @Override
    public void addView(View child) {
        if (getChildCount() != 0)
            throw new IllegalStateException("ResizableArea can only have one child attached!");

        this.child = child;
        super.addView(child);
    }

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