1

如何动态更改 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);
    }

}
4

1 回答 1

0

其实,这是正确的答案:

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);
    }

}
于 2011-10-17T14:22:17.233 回答