我想拖動一個視圖。直到現在,我用LinearLayout和邊距以及AbsoluteLayout來試用它。Monodroid - 拖動視圖與觸摸事件?
AbsoluteLayout例如:
button.Touch = (clickedView, motionEvent) =>
{
Button b = (Button)clickedView;
if (motionEvent.Action == MotionEventActions.Move)
{
AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(100, 35, (int)motionEvent.GetX(), (int)motionEvent.GetY());
b.LayoutParameters = layoutParams;
}
return true;
};
在我想我得到了一個古玩行爲每一個案件。這是爲什麼。我拖着的視圖跟隨着我的手指,但總是在兩個位置之間跳躍。其中一個是我的手指,另一個是我的手指左上角。如果我只是將我的當前位置寫入文本視圖(而不移動視圖),則座標的行爲與預期相同。但如果我也在移動視圖,他們又會跳起來。 我該如何避免這種情況?
編輯:我用我的問題的聲音評論,以實施monodroid工作draging(這是在Java/Android SDK在鏈接的網站完成)。也許別人有興趣做一些日子,所以這裏是我的解決方案:
[Activity(Label = "Draging", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
private View selectedItem = null;
private int offset_x = 0;
private int offset_y = 0;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
ViewGroup vg = (ViewGroup)FindViewById(Resource.Id.vg);
vg.Touch = (element, motionEvent) =>
{
switch (motionEvent.Action)
{
case MotionEventActions.Move:
int x = (int)motionEvent.GetX() - offset_x;
int y = (int)motionEvent.GetY() - offset_y;
int w = WindowManager.DefaultDisplay.Width - 100;
int h = WindowManager.DefaultDisplay.Height - 100;
if (x > w)
x = w;
if (y > h)
y = h;
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
LinearLayout.LayoutParams.WrapContent,
LinearLayout.LayoutParams.WrapContent));
lp.SetMargins(x, y, 0, 0);
selectedItem.LayoutParameters = lp;
break;
default:
break;
}
return true;
};
ImageView img = FindViewById<ImageView>(Resource.Id.img);
img.Touch = (element, motionEvent) =>
{
switch (motionEvent.Action)
{
case MotionEventActions.Down:
offset_x = (int)motionEvent.GetX();
offset_y = (int)motionEvent.GetY();
selectedItem = element;
break;
default:
break;
}
return false;
};
}
}
http://www.edumobile.org/android/android-beginner-tutorials/drag-and-drop-ui-element/ – mironych