我移动图像时出现问题,当它移动到屏幕外时 我想在屏幕大小的边界内移动 如何解决它 这是该图像的触摸列表代码。
private ImageView nail_cleaner;
RelativeLayout re_layout;
PointF DownPT = null;
PointF StartPT = null;
@Override
public boolean onTouch(View v, MotionEvent event) {
startMoving(nail_cleaner, event);
return true;
}
public void startMoving(ImageView img, MotionEvent event) {
int eid = event.getAction();
switch (eid) {
case MotionEvent.ACTION_MOVE:
PointF mv = new PointF(event.getX() - DownPT.x, event.getY()
- DownPT.y);
img.setX((int) (StartPT.x + mv.x));
img.setY((int) (StartPT.y + mv.y));
StartPT = new PointF(img.getX(), img.getY());
if (event.getX() == event.getX()) {
}
break;
case MotionEvent.ACTION_DOWN:
DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF(img.getX(), img.getY());
Log.e("get points : ", img.getX() + "" + img.getY());
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.clean_wash);
re_layout = (RelativeLayout) findViewById(R.id.layout_clean);
nail_cleaner = (ImageView) findViewById(R.id.id_nail_cleaner);
DownPT = new PointF();
StartPT = new PointF();
nail_cleaner.setOnTouchListener(this);
}
如果我移动图像它会移回另一个图像,我想将这个图像移到另一个图像前面,
答案 0 :(得分:0)
case MotionEvent.ACTION_MOVE:
PointF mv = new PointF(event.getX() - DownPT.x, event.getY()
- DownPT.y);
int imgWidth = img.getWidth()/2;
int imgHeight = img.getHeight()/2;
if(StartPT.x + mv.x + imgWidth > 0 && StartPT.x + mv.x + imgWidth < ScreenWidth){
img.setX((int) (StartPT.x + mv.x));}
if(StartPT.y + mv.y + imgHeight > 0 && StartPT.y + mv.y + imgHeight < ScreenHeight){
img.setY((int) (StartPT.y + mv.y));}
StartPT = new PointF(img.getX(), img.getY());
break;
你必须这样做
答案 1 :(得分:-1)
根据屏幕尺寸设置屏幕边界,然后检查每面墙,例如左墙if(img.getX() <= 0)
img.setX(0);
答案 2 :(得分:-1)
请参阅我只更新您的答案
private ImageView nail_cleaner;
RelativeLayout re_layout;
PointF DownPT = null;
PointF StartPT = null;
@Override
public boolean onTouch(View v, MotionEvent event) {
startMoving(nail_cleaner, event);
return true;
}
public void startMoving(ImageView img, MotionEvent event) {
int eid = event.getAction();
//Get the device height and width
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
switch (eid) {
case MotionEvent.ACTION_MOVE:
/*Check the current x position is less than width of device if get true then move the object */
if(event.getX() < (wwidth-10)){
PointF mv = new PointF(event.getX() - DownPT.x, event.getY()
- DownPT.y);
img.setX((int) (StartPT.x + mv.x));
img.setY((int) (StartPT.y + mv.y));
StartPT = new PointF(img.getX(), img.getY());
}
break;
case MotionEvent.ACTION_DOWN:
DownPT.x = event.getX();
DownPT.y = event.getY();
StartPT = new PointF(img.getX(), img.getY());
Log.e("get points : ", img.getX() + "" + img.getY());
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.clean_wash);
re_layout = (RelativeLayout) findViewById(R.id.layout_clean);
nail_cleaner = (ImageView) findViewById(R.id.id_nail_cleaner);
DownPT = new PointF();
StartPT = new PointF();
nail_cleaner.setOnTouchListener(this);
}