Android 移动双图像视图问题
您好,我正在尝试独立移动两个图像视图。我的问题是,我能够正确移动第一个视图,但第二个视图有问题。我一触到它,它就会移出屏幕。 如果两个图像视图在同一个位置,它工作正常。但我需要它不同 对不起,如果问题很基本 这是代码,有人帮忙吗Android 移动双图像视图问题,android,Android,您好,我正在尝试独立移动两个图像视图。我的问题是,我能够正确移动第一个视图,但第二个视图有问题。我一触到它,它就会移出屏幕。 如果两个图像视图在同一个位置,它工作正常。但我需要它不同 对不起,如果问题很基本 这是代码,有人帮忙吗 int windowwidth; int windowheight; ImageView ima1,ima2; Rect rect=null; private android.widget.RelativeLayout.La
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>
int窗口宽度;
内窗高度;
ImageView ima1、ima2;
Rect Rect=null;
私有android.widget.RelativeLayout.LayoutParams LayoutParams;
//私有android.widget.RelativeLayout.LayoutParams LayoutParams;
//私有android.widget.RelativeLayout.LayoutParams LayoutParams;
@凌驾
创建时的公共void(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(“宽度”+窗口宽度);
System.out.println(“高度”+窗口高度);
ima1=(ImageView)findViewById(R.id.icon);
ima2=(ImageView)findViewById(R.id.icon1);
ima2.setX(250);
ima2.setY(1000);
ima1.setOnTouchListener(新视图.OnTouchListener(){
公共布尔onTouch(视图v,运动事件){
layoutParams=(RelativeLayout.layoutParams)ima1.getLayoutParams();
//layoutParams=(RelativeLayout.layoutParams ima1.getLayoutParams();
开关(event.getAction())
{
case MotionEvent.ACTION\u DOWN:
打破
/*case MotionEvent.ACTION\u取消:
中断*/
case MotionEvent.ACTION\u移动:
int x_cord=(int)event.getRawX();
int y_cord=(int)event.getRawY();
System.out.println(“x”+x_线的值);
System.out.println(“y”值+y_线);
如果(x_线>窗宽){
x_线=窗宽;
}
如果(y_线>窗高){
y_线=窗高;
}
layoutParams.leftMargin=x_-25;
layoutParams.topMargin=y_-25;
//layoutParams.rightMargin=x_-25;
//layoutParams.bottomMargin=y_-25;
ima1.setLayoutParams(layoutParams);
打破
默认:中断;
}
返回true;
}
});
ima2.setOnTouchListener(新视图.OnTouchListener(){
公共布尔onTouch(视图v,运动事件){
layoutParams=(RelativeLayout.layoutParams)ima2.getLayoutParams();
开关(event.getActionMasked())
{
case MotionEvent.ACTION\u DOWN:
//rect=newrect(v.getLeft(),v.getTop(),v.getRight(),v.getBottom());
打破
case MotionEvent.ACTION\u移动:
//如果(!rect.包含((int)event.getX(),(int)event.getY()){
// }
//否则
{
int x_cord=(int)event.getRawX();
int y_cord=(int)event.getRawY();
系统输出打印LN(“x1”+x_线的值);
系统输出打印LN(“y1”+y_线的值);
如果(x_线>窗宽){
x_线=窗宽;
}
如果(y_线>窗高){
y_线=窗高;
}
layoutParams.leftMargin=x_线-25;
layoutParams.topMargin=y_线-75;
ima2.setLayoutParams(layoutParams);
}
打破
默认:中断;
}
返回true;
}
});
}
}
这里是XML
为什么在第二张图像上使用-75的y_线,而在第一张图像上仅使用25的y_线?我只是尝试使用不同的值。即使我将其调整为25,它仍然不起作用