这个版本是正确的,我找到了答案!
我在互联网上找到了这段代码。问题是当我尝试运行它时它崩溃了。很难知道错误..基本是独立移动多个图像。模拟器只是说:“项目已经崩溃”。它甚至没有打开我的主要活动。这是这段代码:
package com.example.disneyfan;
public class Cinderella extends Activity {
ImageView cinderella, dress;
int windowwidth;
int windowheight;
float x = 0;
float y = 0;
private android.widget.RelativeLayout.LayoutParams layoutParams1,
layoutParams2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(com.example.disneyfan.R.id.cinderella);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
System.out.println("width" + windowwidth);
System.out.println("height" + windowheight);
cinderella = (ImageView) findViewById(R.drawable.cinderelladoll);
cinderella.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
layoutParams1 = (RelativeLayout.LayoutParams) cinderella
.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
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;
}
layoutParams1.leftMargin = x_cord - 25;
layoutParams1.topMargin = y_cord - 25;
cinderella.setLayoutParams(layoutParams1);
break;
default:
break;
}
return true;
}
});
dress = (ImageView) findViewById(R.id.dress);
dress.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
layoutParams2 = (RelativeLayout.LayoutParams) dress
.getLayoutParams();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
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;
}
layoutParams2.leftMargin = x_cord - 25;
layoutParams2.topMargin = y_cord - 75;
dress.setLayoutParams(layoutParams2);
break;
default:
break;
}
return true;
}
});
}
}