如何阻止对象离开屏幕?

public class MovingBagView extends View {


    private Bitmap bag[] = new Bitmap[2];

    private int bagX;

    private int bagY = 1000;

    private int bagSpeed;

    private Boolean touch = false;

    private int canvasWidth, canvasHeight;

    private Bitmap backgroundImage;

    private Paint scorePaint = new Paint();

    private Bitmap life[] = new Bitmap[2];


    public MovingBagView(Context context) {

        super(context);

        bag[0] = BitmapFactory.decodeResource(getResources(), R.drawable.bag1);

        bag[1] = BitmapFactory.decodeResource(getResources(), R.drawable.bag2);

        backgroundImage = BitmapFactory.decodeResource(getResources(), R.drawable.background);

        scorePaint.setColor(Color.BLACK);

        scorePaint.setTextSize(40);

        scorePaint.setTypeface(Typeface.DEFAULT_BOLD);

        scorePaint.setAntiAlias(true);

        life[0] = BitmapFactory.decodeResource(getResources(), R.drawable.heart);

        life[1] = BitmapFactory.decodeResource(getResources(), R.drawable.heart_grey);

        bagX = 10;

    }


    @Override

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        canvasWidth = canvas.getWidth();

        canvasHeight = canvas.getHeight();

        canvas.drawBitmap(backgroundImage, 0, 0, null);

        int minBagX = bag[0].getWidth();

        int maxBagX = canvasWidth - bag[0].getWidth() * 3;

        bagX = bagX + bagSpeed;

        if (bagX < minBagX) {

            bagX = minBagX;

        }

        if (bagX < maxBagX) {

            bagX = maxBagX;

        }


我可以点击屏幕将对象向左移动,我点击它的次数越多,它应该向左移动得越远。但是,问题是对象向右移动太多,并且离开了屏幕。我希望它停在边缘,所以它仍然在屏幕上可见。此外,对象位于屏幕的中心底部,如何将其定位到左下角?您可以通过查看下面的 GIF 来了解它是如何工作的。

http://img1.mukewang.com/6288e39e0001721a02700480.jpg

繁花如伊
浏览 129回答 3
3回答

炎炎设计

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

杨__羊羊

我是这样做的 //maintainoffscreen 函数将保持路径不移出屏幕,并返回 true ,否则如果路径没有碰撞到任何一侧,则返回 false,您可以通过设置偏移量来移动路径。&nbsp; &nbsp; private boolean maintainOffScreen(Path path, float px, float py){&nbsp; &nbsp; boolean done = true;&nbsp; &nbsp; RectF rectF = new RectF();&nbsp; &nbsp; path.computeBounds(rectF,true);&nbsp; &nbsp; float l = getPathWidth(path) - rectF.right;&nbsp; &nbsp; float r = getPathWidth(path) + rectF.left;&nbsp; &nbsp; float t = getPathHeight(path) - rectF.bottom;&nbsp; &nbsp; float b = getPathHeight(path)+ rectF.top;&nbsp; &nbsp; if(l < (-1) && r < getWidth() && b < getHeight() && t< (-1)){&nbsp; &nbsp; &nbsp; &nbsp; //if path does not collide to any side//you can move your path here as well&nbsp; &nbsp; &nbsp; &nbsp;//by setting Path.offset(px,py)&nbsp; &nbsp; &nbsp; &nbsp; done = false;&nbsp; &nbsp; &nbsp; &nbsp; offScreen = OffScreen.NOOFF; //im using these as enum according to my need,&nbsp;//as this is saving which side of screen collides&nbsp; &nbsp; }&nbsp; &nbsp; else if(l >= (-1)){&nbsp; &nbsp; &nbsp; &nbsp; path.offset(px+(l),py);&nbsp; &nbsp; &nbsp; &nbsp; offScreen = OffScreen.LEFT;&nbsp; &nbsp; &nbsp; &nbsp; offscrenn_xl = 1;&nbsp; &nbsp; &nbsp; &nbsp; done = true;&nbsp; &nbsp; }&nbsp; &nbsp; else if(r >= getWidth()){&nbsp; &nbsp; &nbsp; &nbsp; float s = getWidth() - r;&nbsp; &nbsp; &nbsp; &nbsp; path.offset(px+(s),py);&nbsp; &nbsp; &nbsp; &nbsp; offScreen = OffScreen.RIGHT;&nbsp; &nbsp; &nbsp; &nbsp; offscrenn_xr = s;&nbsp; &nbsp; &nbsp; &nbsp; done = true;&nbsp; &nbsp; }&nbsp; &nbsp; else if(b >= getHeight()){&nbsp; &nbsp; &nbsp; &nbsp; float s = getHeight() - b;&nbsp; &nbsp; &nbsp; &nbsp; path.offset(px,py+s);&nbsp; &nbsp; &nbsp; &nbsp; offScreen = OffScreen.BOTTOM;&nbsp; &nbsp; &nbsp; &nbsp; offscrenn_yb = s;&nbsp; &nbsp; &nbsp; &nbsp; done = true;&nbsp; &nbsp; }&nbsp; &nbsp; else if(t >= (-1)){&nbsp; &nbsp; &nbsp; &nbsp; path.offset(px,py+(t));&nbsp; &nbsp; &nbsp; &nbsp; offScreen = OffScreen.TOP;&nbsp; &nbsp; &nbsp; &nbsp; offscrenn_yt = t;&nbsp; &nbsp; &nbsp; &nbsp; done = true;&nbsp; &nbsp; }&nbsp; &nbsp; 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 thisthis.startX = event.getX();this.startY = event.getY();

忽然笑

创建您使用线性、相对或约束的布局对象,然后使用该对象获取屏幕宽度object.getWidth();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java