我有一个相对布局包含一个表面视图和另一个相对布局(嵌套)..
1)如果触摸事件超出子相对布局,我想单独为Surface视图接收触摸事件。 ??
2)如果我的触摸事件落在儿童相对布局上,则当前触摸事件不应转到表面视图。
如何实现此切换???
提前致谢..!
以下是我的活动:
public class DragAndDropBasicActivity extends Activity implements OnTouchListener {
private RelativeLayout letterView;
private RelativeLayout mainLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
mainLayout.setOnTouchListener(this);
letterView = (RelativeLayout) findViewById(R.id.mlKnobView);
letterView.setOnTouchListener(this);
}
private boolean dragging = false;
@Override
public boolean onTouch(View v, MotionEvent event) {
boolean eventConsumed = true;
int x = (int)event.getX();
int y = (int)event.getY();
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
if (v == letterView) {
dragging = true;
eventConsumed = false;
}
} else if (action == MotionEvent.ACTION_UP) {
dragging = false;
eventConsumed = false;
} else if (action == MotionEvent.ACTION_MOVE) {
if (v != letterView) {
if (dragging) {
setAbsoluteLocationCentered(letterView, x, y);
}
}
}
return eventConsumed;
}
private void setAbsoluteLocationCentered(View v, int x, int y) {
setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
}
private void setAbsoluteLocation(View v, int x, int y) {
RelativeLayout.LayoutParams alp = (RelativeLayout.LayoutParams) v.getLayoutParams();
alp.leftMargin = x;
alp.topMargin = y;
v.setLayoutParams(alp);
}
}
在我的Xml文件下面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible" />
<RelativeLayout
android:id="@+id/mlKnobView"
android:layout_width="260dip"
android:layout_height="260dip"
android:layout_below="@+id/txtView"
android:background="@drawable/border" >
<Button
android:id="@+id/button_down"
style="?android:attr/buttonStyle"
android:layout_width="140dip"
android:layout_height="60dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dip"
android:background="@drawable/button_down"
android:text="Down" />
<com.example.RotaryKnob
android:id="@+id/jogView"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_above="@id/button_down"
android:layout_below="@+id/button_up"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button_left"
style="?android:attr/buttonStyle"
android:layout_width="60dip"
android:layout_height="80dip"
android:layout_above="@id/button_down"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/jogView"
android:background="@drawable/button_left"
android:text="Left" />
<Button
android:id="@+id/button_right"
style="?android:attr/buttonStyle"
android:layout_width="60dip"
android:layout_height="80dip"
android:layout_above="@id/button_down"
android:layout_alignBaseline="@id/button_left"
android:layout_alignBottom="@id/button_left"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/jogView"
android:background="@drawable/button_right"
android:text="Right" />
<Button
android:id="@+id/button_up"
style="?android:attr/buttonStyleSmall"
android:layout_width="140dip"
android:layout_height="60dip"
android:layout_above="@+id/button_left"
android:layout_alignLeft="@+id/button_down"
android:layout_below="@+id/windowHeader"
android:background="@drawable/button_up"
android:text=" UP " />
</RelativeLayout>
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Hai" />
</RelativeLayout>