这是我的布局。它有两个视图,我通过另一个移动一个。
<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;
}
}
我想在它向右时做一个动作,在它向左时做另一个动作。
我怎样才能做到这一点?谢谢!