开发者

Make FrameLayout expands, and it's childs also

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜