我正在尝试使用xamarin安卓进行对角线拖动(非xamarin也会有所帮助)
我希望用户能够使用他的手指拖动绿色部分(见图像),而拖动它需要是对角线(它开始像红色部分一样) image
到目前为止,我已将滑动部分消除,但我不知道如何做对角滑动部分
private float _viewX;
private int _originalLeft;
private int _originalRight;
private View _greenView;
private View _redView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.QuestionsView);
_greenView = FindViewById<View>(Resource.Id.greenTrueView);
_redView = FindViewById<View>(Resource.Id.redFalseView);
_greenView.SetOnTouchListener(this);
_redView.SetOnTouchListener(this);
}
public bool OnTouch(View v, MotionEvent e)
{
if (v.Equals(_greenView))
{
switch (e.Action)
{
case MotionEventActions.Down:
_viewX = e.GetX();
_originalLeft = v.Left;
break;
case MotionEventActions.Move:
var left = (int)(e.RawX - _viewX);
v.Layout(left, v.Top, v.Right, v.Bottom);
break;
}
}
else
{
switch (e.Action)
{
case MotionEventActions.Down:
_viewX = e.GetX();
_originalRight = v.Right;
break;
case MotionEventActions.Move:
var right = (int)(_viewX + e.RawX);
v.Layout(v.Left, v.Top, right, v.Bottom);
break;
}
}
return true;
}
答案 0 :(得分:0)
到目前为止,我已将滑动部分消除,但我不知道如何做对角滑动部分
对角滑动是指每个滑动,垂直偏移与其水平偏移具有固定比率。例如:45度对角线滑动:垂直偏移= 1 *(水平偏移)。因此,您需要做的只是根据_viewX
修改垂直偏移:
private View green_view,red_view;
private float _viewX;
private float _viewY;
private int _originalLeft,_originalRight;
if(v.Equals(green_view))
{
switch (event.Action)
{
case MotionEventActions.Down:
_viewX=event.GetX();
_viewY=event.GetY();
_originalLeft=v.Left;
break;
case MotionEventActions.Move:
int left=(int)(event.RawX()-_viewX);
//if the fixed ratio is 1, you can set it by any number like 1/2
int top=left;
v.Layout(left,top,v.Right,v.Bottom);
break;
}
...