Question

I am writing a game in which i need to check collisions between two views. when i use the view.getHitRect(outputRect) method from some reason the bottom padding of the view (all the other paddings are set to 0) is added to the top property of the view's hit rectangle. I've tried to fix this by overriding the getHitRect method:

public abstract class GameView extends ImageView {
    @Override
    public void getHitRect(Rect outRect) {
        super.getHitRect(outRect);
        outRect.top+=getPaddingBottom();
    }

This got me a better result, but its still inaccurate, and the bigger the padding gets the less accurate the rectangle becomes, and the collision always occur too low. Before using paddings I was trying to do it without it, which gave me accurate hitRect but i got a different bug.
Thanks in advance

Was it helpful?

Solution

Solved it after some trial and error. Apparently the padding was added to the rectangle, but it was divided equally between the rect.top and rect.bottom, so the solution was:

@Override
public void getHitRect(Rect outRect) {
    super.getHitRect(outRect);
    outRect.top+=getPaddingBottom()/2;
    outRect.bottom-=getPaddingBottom()/2;
}

If one of you Android experts care to explain why I would be grateful, since I don't understand how it works and I just can't see the logic in it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top