これが私のレイアウトです。2 つのビューがあり、一方を他方に移動します。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/time_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/slide_layout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="20dp"
android:layout_alignParentBottom="true" >
<View
android:id="@+id/slide_to_pause_backgound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#0000FF" />
<View
android:id="@+id/slide_to_pause"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/slide_to_pause_backgound"
android:layout_alignParentBottom="true"
android:background="#00FFFF" />
</RelativeLayout>
これは、移動するビューをアクティビティ クラスに設定する方法です。
slideView = ((View) findViewById(R.id.slide_to_pause));
slideView.setOnTouchListener(this);
そして、これは私がビューを移動する方法です:
@Override
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
viewLeftMargin = X - lParams.leftMargin;
viewTopMargin = lParams.topMargin;
viewBottomMargin = lParams.bottomMargin;
viewRightMargin = X - lParams.rightMargin;
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.rightMargin = X - viewRightMargin;
layoutParams.leftMargin = X - viewLeftMargin;
layoutParams.topMargin = viewTopMargin;
layoutParams.bottomMargin = viewBottomMargin;
view.setLayoutParams(layoutParams);
break;
}
return true;
}
}
右に行ったときと左に行ったときに別のアクションを実行したい。
これどうやってするの?ありがとうございました!