3 回答

TA贡献1808条经验 获得超4个赞
此条件永远不会应用于您提供的代码。它继续超过最大值。
if (bagX < maxBagX) {
bagX = maxBagX;
}
它应该如下所示:
if (bagX >= maxBagX) {
bagX = maxBagX;
}
而maxBagX的值应该是canvasWidth - bag[0].getWidth()为了实现包本身的右边缘。(除非您出于不同的原因使用乘法 3 次,否则这应该是解决方案。)

TA贡献1943条经验 获得超7个赞
我是这样做的 //maintainoffscreen 函数将保持路径不移出屏幕,并返回 true ,否则如果路径没有碰撞到任何一侧,则返回 false,您可以通过设置偏移量来移动路径。
private boolean maintainOffScreen(Path path, float px, float py){
boolean done = true;
RectF rectF = new RectF();
path.computeBounds(rectF,true);
float l = getPathWidth(path) - rectF.right;
float r = getPathWidth(path) + rectF.left;
float t = getPathHeight(path) - rectF.bottom;
float b = getPathHeight(path)+ rectF.top;
if(l < (-1) && r < getWidth() && b < getHeight() && t< (-1)){
//if path does not collide to any side//you can move your path here as well
//by setting Path.offset(px,py)
done = false;
offScreen = OffScreen.NOOFF; //im using these as enum according to my need,
//as this is saving which side of screen collides
}
else if(l >= (-1)){
path.offset(px+(l),py);
offScreen = OffScreen.LEFT;
offscrenn_xl = 1;
done = true;
}
else if(r >= getWidth()){
float s = getWidth() - r;
path.offset(px+(s),py);
offScreen = OffScreen.RIGHT;
offscrenn_xr = s;
done = true;
}
else if(b >= getHeight()){
float s = getHeight() - b;
path.offset(px,py+s);
offScreen = OffScreen.BOTTOM;
offscrenn_yb = s;
done = true;
}
else if(t >= (-1)){
path.offset(px,py+(t));
offScreen = OffScreen.TOP;
offscrenn_yt = t;
done = true;
}
return done;
}
对 px,py 感到困惑?这是你的手指相对于路径的运动
`
final float px = event.getX() - this.startX;
final float py = event.getY() - this.startY;
if(!maintainOffScreen(path,px,py)){path.offset(px,py); }` //you should call
//this function here ,this will move your path to finger if there is no collision.
//and donot forgot to set the startx,and starty yo new positon like this
this.startX = event.getX();
this.startY = event.getY();
添加回答
举报