nutella_eater
nutella_eater

Reputation: 3582

How to know when one moving view (overlaps) is on the top of the over

(not sure about the Title. Please make it right)

Hi. I'm trying to create a small game, where player can move objects inside RalativeLayout. What I want is to check whether my moving object is inside FrameLayout or outside of it. For example I want to change the text of this object when it is inside FrameLayout.

So I create objects inside for loop this way:

Random random = new Random();
for (int i = 0; i < 3; i++) {  //any number, not just 3
            TextView textView = new TextView(getActivity());
            textView.setX(random.nextInt(size.x - 200));
            textView.setY(random.nextInt(size.y - 200)); //scattered all over the screen
            textView.setText(i + "");
            textView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            textView.setTextSize(50);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            textView.setLayoutParams(lp);
            textView.setOnTouchListener(touchListener);
            frameFor.addView(textView);
        }

There is also a FrameLayout where eventually all my objects should be:

RelativeLayout.LayoutParams frameLP = new RelativeLayout.LayoutParams(300, 300);
        frameLP.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        frameForLetters.setLayoutParams(frameLP);

As you can see, I also added a OnTouchListener to make objects movable:

View.OnTouchListener touchListener = new View.OnTouchListener() {
        float dX, dY;

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:

                    dX = view.getX() - event.getRawX();
                    dY = view.getY() - event.getRawY();
                    break;

                case MotionEvent.ACTION_MOVE:

                    view.animate()
                            .x(event.getRawX() + dX)
                            .y(event.getRawY() + dY)
                            .setDuration(0)
                            .start();
                    break;
                default:
                    return false;
            }
            return true;
        }
    };

in this pic you can see that one and two are inside FrameLayout and zero is outside. enter image description here

I know it all can be done with huge amount of geometry and calculation the size of each object and FrameLayout itself. Maybe there is another way of doing this? Maybe there is a library or framework which can make my task much easier?

Upvotes: 0

Views: 45

Answers (1)

Allen G
Allen G

Reputation: 1200

I'm not aware of libraries or frameworks, but you can use these two methods to determine if the objects are overlapping (the Rect.intersects method does most of the calculations):

private Rect getScreenBounds(View view)
{
    int[] location = new int[2];
    view.getLocationOnScreen(location);

    return new Rect(location[0], location[1], location[0] + view.getWidth(), location[1] + view.getHeight());
}

private boolean doViewsIntersect(View target, View item)
{
    Rect targetRect = getScreenBounds(target);
    Rect itemRect = getScreenBounds(item);

    return targetRect.intersects(itemRect);
}

If you're interested in knowing if the object is fully inside the other, targetRect becomes the Rect containing the intersection. Hence, you can do the following to see if the item is entirely inside the target bounds:

if(targetRect.intersects(itemRect)) {
    return targetRect.width() == item.getWidth() && targetRect.height() == item.getHeight();
} else {
    return false;
}

Upvotes: 1

Related Questions