Android - 创建可调整大小的视图

8
我目前正在制作一个带有可移动和可调整大小视图的仪表板。我现在遇到的问题是,我想通过触摸手势来调整视图的大小。因此,我想到了一个点,在选择时添加到视图中,可以拖动以调整所选视图的大小。这类似于Android主屏幕上的调整大小过程。
即使经过长时间的研究,我也无法找到另一个具有自己的DragListener的视图如何覆盖的解决方案。我可以想象将选定的视图和点放入一个ViewGroup中,并使点覆盖视图。有人有这个问题的经验吗?提前感谢您。

Resizable View


1
你找到解决方案了吗? - Alpha-Centauri
2个回答

1
尝试使用以下类。
package mayank.com.bhaktirasamritaswami.ui.components;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import mayank.com.bhaktirasamritaswami.R;
public class ResizableLayout extends RelativeLayout implements View.OnTouchListener{
public static int top_margine;
public ResizableLayout(Context context) {
    super(context);
    init();
}
public ResizableLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public void init(){
    addView(inflate(getContext(), R.layout.ui_component_resizable_layout,null));
    setOnTouchListener(this);
    dragHandle = this.findViewById(R.id.drag_handle);
}

private View dragHandle;
float downRawY;
float dY;
float height;

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    int action = motionEvent.getAction();
    if(action == MotionEvent.ACTION_DOWN){
        downRawY = motionEvent.getRawY();
        height = this.getMeasuredHeight();
    } else {
        View parent = (View) this.getParent();
        if(downRawY<parent.getHeight() - height + 2*dragHandle.getHeight()) {
            float rawY = motionEvent.getRawY()>20*dragHandle.getHeight()?motionEvent.getRawY():20*dragHandle.getHeight();
            MarginLayoutParams p = (MarginLayoutParams) this.getLayoutParams();
            p.topMargin = (int)rawY;
            if(p.topMargin!=0)
                this.top_margine = p.topMargin;
            this.setLayoutParams(p);
        }
    }
    return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent motionEvent){
    onTouch(this, motionEvent);
    return false;
}
}

我设计了这个类仅用于垂直调整大小。您需要更改onTouch方法以适应您的情况。


1
现在问可能有点晚了,但您能否添加或提供有关“ui_component_resizable_layout”的任何想法呢? - Kishan Thakkar

1
将Mayank的答案中的onTouch()更改如下
static final int TOP = 1, RIGHT = 2, BOTTOM = 3, LEFT = 4;
int handleMargin = 10, side = 0;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
if(action == MotionEvent.ACTION_DOWN){
// detect if tap is on handles
    downRawY = motionEvent.getRawY();
    height = this.getMeasuredHeight();
} else {
// change layout margin inside switch
    switch(side){
        case: ....
    }
    View parent = (View) this.getParent();
    if(downRawY<parent.getHeight() - height + 2*dragHandle.getHeight()) {
        float rawY = motionEvent.getRawY()>20*dragHandle.getHeight()?motionEvent.getRawY():20*dragHandle.getHeight();
        MarginLayoutParams p = (MarginLayoutParams) this.getLayoutParams();
        p.topMargin = (int)rawY;
        if(p.topMargin!=0)
            this.top_margine = p.topMargin;
        this.setLayoutParams(p);
    }
}
return false;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接