嗨,我正在尝试独立移动两个图像视图。我的问题是我能够正确移动第一个,但第二个有问题。我一触摸它,它就会移出屏幕。如果两个 ImageViews 都在同一个位置,它工作正常。但是如果问题非常基本,我需要它是不同的对不起这是任何人帮助的代码吗?
int windowwidth;
int windowheight;
ImageView ima1,ima2;
Rect rect=null;
private android.widget.RelativeLayout.LayoutParams layoutParams ;
// private android.widget.RelativeLayout.LayoutParams layoutParams ;
//private android.widget.RelativeLayout.LayoutParams layoutParams ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.touch);
/* Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();*/
windowwidth = getWindowManager().getDefaultDisplay().getWidth()-130;
windowheight = getWindowManager().getDefaultDisplay().getHeight()-165;
System.out.println("width" +windowwidth);
System.out.println("height" +windowheight);
ima1 = (ImageView)findViewById(R.id.icon);
ima2 = (ImageView)findViewById(R.id.icon1);
ima2.setX(250);
ima2.setY(1000);
ima1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
layoutParams = (RelativeLayout.LayoutParams) ima1.getLayoutParams();
// layoutParams = (RelativeLayout.LayoutParams ima1.getLayoutParams();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;
/*case MotionEvent.ACTION_CANCEL:
break;*/
case MotionEvent.ACTION_MOVE:
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
System.out.println("value of x" +x_cord);
System.out.println("value of y" +y_cord);
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord-25;
layoutParams.topMargin = y_cord-25;
// layoutParams.rightMargin = x_cord-25;
// layoutParams.bottomMargin = y_cord-25;
ima1.setLayoutParams(layoutParams);
break;
default: break;
}
return true;
}
});
ima2.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
layoutParams = (RelativeLayout.LayoutParams) ima2.getLayoutParams();
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
// rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
break;
case MotionEvent.ACTION_MOVE:
// if(!rect.contains((int)event.getX(), (int)event.getY())){
// }
// else
{
int x_cord = (int) event.getRawX();
int y_cord = (int) event.getRawY();
System.out.println("value of x1" +x_cord);
System.out.println("value of y1" +y_cord);
if (x_cord > windowwidth) {
x_cord = windowwidth;
}
if (y_cord > windowheight) {
y_cord = windowheight;
}
layoutParams.leftMargin = x_cord - 25;
layoutParams.topMargin = y_cord - 75;
ima2.setLayoutParams(layoutParams);
}
break;
default: break;
}
return true;
}
});
}
}
XML here
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/icon" />
<ImageView
android:id="@+id/icon1"
android:layout_y="30dip"
android:layout_x="118dip"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/icon1" />
<Button
android:id="@+id/getvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="25dp"
android:text="Reset"
android:textSize="30dp" />
</RelativeLayout>