Android 如何在onDraw中移动矩形

Android 如何在onDraw中移动矩形,android,touch-event,Android,Touch Event,我有一个无法解决的问题。问题是我想用另一个矩形推一个矩形,我这样做: public boolean onTouchEvent(MotionEvent event){ switch(event.getAction()){ case MotionEvent.ACTION_DOWN: touchX = event.getX(); touchY = event.getY(); case MotionEvent.AC

我有一个无法解决的问题。问题是我想用另一个矩形推一个矩形,我这样做:

    public  boolean onTouchEvent(MotionEvent event){


    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
        touchX = event.getX();
        touchY = event.getY();          
    case MotionEvent.ACTION_UP:
        dx = touchX - event.getX();
        dy = touchY - event.getY();            
        if(Math.abs(dy) > Math.abs(dx)){ 

            if(dy > 0){   //Direction UP   



                manFirstY = manFirstY - 50;
                manSecondY = manSecondY - 50;                    
                if(manFirstY < targetSecondY && manFirstX == targetFirstX){                     
                    targetFirstY = targetFirstY - 50;
                    targetSecondY = targetSecondY - 50;

                }

            }

            else if(dy < 0){    //Direction DOWN                                   
                manFirstY = manFirstY + 50;
                manSecondY = manSecondY + 50;                    
                if(manSecondY > targetFirstY && manFirstX == targetFirstX){                 
                    targetFirstY = targetFirstY + 50;
                    targetSecondY = targetSecondY + 50;
                }

            }
        }
        else{
            if(dx > 0){     //Direction LEFT                 
                manFirstX = manFirstX - 50;
                manSecondX = manSecondX - 50;                  
                if(manFirstX < targetSecondX && manFirstY == targetFirstY){
                    targetFirstX = targetFirstX - 50;
                    targetSecondX = targetSecondX - 50;
                }

            }
            else if(dx < 0){     //Direction RIGHT                    
                manFirstX = manFirstX + 50;
                manSecondX = manSecondX + 50;                 
                if(manSecondX > targetFirstX && manFirstY == targetFirstY){
                    targetFirstX = targetFirstX +50;
                    targetSecondX = targetSecondX +50;
                }   
            }
        }          
}

return true;
public boolean onTouchEvent(运动事件){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
touchX=event.getX();
touchY=event.getY();
case MotionEvent.ACTION\u UP:
dx=touchX-event.getX();
dy=touchY-event.getY();
if(Math.abs(dy)>Math.abs(dx)){
如果(dy>0){//向上方向
manFirstY=manFirstY-50;
manSecondY=manSecondY-50;
如果(manFirstYtargetFirstY&&manFirstX==targetFirstX){
targetFirstY=targetFirstY+50;
targetSecondY=targetSecondY+50;
}
}
}
否则{
如果(dx>0){//方向左
manFirstX=manFirstX-50;
manSecondX=manSecondX-50;
if(manFirstXtargetFirstX&&manFirstY==targetFirstY){
targetFirstX=targetFirstX+50;
targetSecondX=targetSecondX+50;
}   
}
}          
}
返回true;
}


但是有一个问题。当我用这种方式时,它工作了,但除了推动它之外,它还带着矩形。我不想让矩形带着另一个,我只想推动它,当我想让矩形离开他推动的矩形时,我只想释放它。

假设manFirstX/manSecondX/manFirstY/manSecondY是左边,右边,第一个矩形的上边缘和下边缘与第二个矩形的目标相同,您应该尝试用一个示例检查代码

如果第一个矩形位于第二个矩形的右侧,
manSecondX
将始终大于
targetSecondX
。如果向右移动,且第一个矩形位于第二个矩形的右侧,则条件
If(manSecondX>targetFirstX&&manFirstY==targetFirstY)
将始终为真

因此,我的建议是简单地添加一个条件,例如

public  boolean onTouchEvent(MotionEvent event){

switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
    touchX = event.getX();
    touchY = event.getY();          
case MotionEvent.ACTION_UP:
    dx = touchX - event.getX();
    dy = touchY - event.getY();            
    if(Math.abs(dy) > Math.abs(dx)){ 

        if(dy > 0){   //Direction UP   



            manFirstY = manFirstY - 50;
            manSecondY = manSecondY - 50;                    
            if(targetFirstY < manSecondY && manFirstY < targetSecondY && manFirstX == targetFirstX){                     
                targetFirstY = targetFirstY - 50;
                targetSecondY = targetSecondY - 50;

            }

        }

        else if(dy < 0){    //Direction DOWN                                   
            manFirstY = manFirstY + 50;
            manSecondY = manSecondY + 50;                    
            if(targetSecondY > manFirstY && manSecondY > targetFirstY && manFirstX == targetFirstX){                 
                targetFirstY = targetFirstY + 50;
                targetSecondY = targetSecondY + 50;
            }

        }
    }
    else{
        if(dx > 0){     //Direction LEFT                 
            manFirstX = manFirstX - 50;
            manSecondX = manSecondX - 50;                  
            if(targetFirstX < manSecondX && manFirstX < targetSecondX && manFirstY == targetFirstY){
                targetFirstX = targetFirstX - 50;
                targetSecondX = targetSecondX - 50;
            }

        }
        else if(dx < 0){     //Direction RIGHT                    
            manFirstX = manFirstX + 50;
            manSecondX = manSecondX + 50;                 
            if(targetSecondX > manFirstX && manSecondX > targetFirstX && manFirstY == targetFirstY){
                targetFirstX = targetFirstX +50;
                targetSecondX = targetSecondX +50;
            }   
        }
    }          
}

return true;
public boolean onTouchEvent(运动事件){
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
touchX=event.getX();
touchY=event.getY();
case MotionEvent.ACTION\u UP:
dx=touchX-event.getX();
dy=touchY-event.getY();
if(Math.abs(dy)>Math.abs(dx)){
如果(dy>0){//向上方向
manFirstY=manFirstY-50;
manSecondY=manSecondY-50;
如果(targetFirstYmanFirstY&&manSecondY>targetFirstY&&manFirstX==targetFirstX){
targetFirstY=targetFirstY+50;
targetSecondY=targetSecondY+50;
}
}
}
否则{
如果(dx>0){//方向左
manFirstX=manFirstX-50;
manSecondX=manSecondX-50;
if(targetFirstXmanFirstX&&manSecondX>targetFirstX&&manFirstY==targetFirstY){
targetFirstX=targetFirstX+50;
targetSecondX=targetSecondX+50;
}   
}
}          
}
返回true;
希望这能奏效