我已经创建了一个ImageView,我可以将它拖动到屏幕上。我使用画布将coordonates转换为2个整数的向量,我已经成功编程为immage而不是在上下都得到太多coordonates(y)但我无法在x上做(左和右)我不知道为什么不工作因为是相同的。
我的代码是:
private int offset_x = 0;
private int offset_y = 0;
RelativeLayout canvas;
boolean isselected = false;
View selected_item = null;
Button save,newdoor,newwindow;
static final int SNAP_GRID_INTERVAL = 43;
static final int SNAP_GRID_INTERVAL2 = 25;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
canvas = (RelativeLayout) findViewById(R.id.container2);
ViewGroup vg = (ViewGroup)findViewById(R.id.container3);
save = (Button) findViewById(R.id.savebtn);
newdoor = (Button) findViewById(R.id.btnnewdoor);
newwindow = (Button) findViewById(R.id.btnnewwindow);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final View canvas = ((View) v.getParent());
final int []coord = new int[3];
final int lungime = 66;
final int inaltime = 20;
final int canvasWidth = canvas.getWidth();
final int canvasHeight = canvas.getHeight();
ImageView image = new ImageView(getBaseContext());
RelativeLayout mylayout = (RelativeLayout) findViewById(R.id.container2);
image.setBackgroundResource(R.drawable.element_wall);
mylayout.addView(image);
image.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
selected_item = v;
break;
default:
break;
}
return false;
}
});
mylayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
canvas.getLocationOnScreen(coord);
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
View canvas = ((View) v.getParent());
final int width = v.getWidth() / 2, height = v.getHeight() / 2;
canvas.getLocationOnScreen(coord);
int x = (int)event.getX() - offset_x;
int y = (int)event.getY() - offset_y;
int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
int rawX = (int) event.getRawX(), rawY = (int) event.getRawY();
int x_coord = (int) event.getRawX() - coord[0] - width;
int y_coord = (int) event.getRawY() - coord[1] - height;
Log.i("arry", "x=" + coord[0] + ", y=" + coord[1]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
if (rawY < coord[1] + inaltime / 2)
lp.topMargin = 0;
else
if (rawY >= coord[1] + canvasHeight - inaltime / 2)
lp.topMargin = canvasHeight - inaltime;
else
lp.topMargin = y / SNAP_GRID_INTERVAL * SNAP_GRID_INTERVAL;
lp.leftMargin = x / SNAP_GRID_INTERVAL * SNAP_GRID_INTERVAL;
lp.setMargins(lp.leftMargin, lp.topMargin, 0, 0);
selected_item.setLayoutParams(lp);
break;
default:
break;
}
return true;
}
});
}
});
在SAVE上点击监听器我以编程方式创建imageView。
我在这里设置Y的限制:
if (rawY < coord[1] + inaltime / 2)
lp.topMargin = 0;
else
if (rawY >= coord[1] + canvasHeight - inaltime / 2)
lp.topMargin = canvasHeight - inaltime;
else
lp.topMargin = y / SNAP_GRID_INTERVAL * SNAP_GRID_INTERVAL;
我使用了LogCat上的日志,我得到的coord [0]为0,coord [1]为某个值(162)。 我的问题是,coord [0]每次都应为0?或0是正确的coordonate?