开发者

Make FrameLayout expands, and it's childs also

开发者 https://www.devze.com 2023-04-13 08:12 出处:网络
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:

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 para开发者_运维问答ms = (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);
    }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消