Wednesday, October 19, 2011

[android-developers] Re: obtain rectangle on touch

You'll need to keep up with where you draw the squares, do some simple math to decide which one was touched and then draw that square a different color. You might be better served with...

http://developer.android.com/reference/android/graphics/Canvas.html#drawRect%28float,%20float,%20float,%20float,%20android.graphics.Paint%29

...instead of constantly using a Rect if you draw a ton of squares that can be done with a simple formula like that. When you grab the click, divide the touch X by the square width and the touch Y by the square height to get the quadrant the click landed in. Draw that area a different color and there you go.

If you do decide to use a Rect, would be better to create it outside the loop and just set the top, left, bottom and right via the fields or use the offset() and offsetTo() functions. Making a new Rect constantly inside the loop is super inefficient.

Steven
Studio LFP
http://www.studio-lfp.com


On Wednesday, October 19, 2011 7:35:30 PM UTC-5, JCC wrote:
Hello,
I don't know much about Android and I would like to know if you people
could help.
I draw a board using this:
private Rect selRect = new Rect();
for (int x = 0; x <20; x++) {
            if( x%2!=0 ) paint.setColor(Color.CYAN);
            else paint.setColor(Color.GREEN);
                for ( int y = 0; y < 20; y++) {
                if ((x%2==0 && y%2==0) || (x%2!=0 && y%2 !=0))
paint.setColor(Color.GREEN);
                else paint.setColor(Color.CYAN);
                    selRect = new Rect(x*squareSize, y*squareSize, (x
+1)*squareSize, (y+1)*squareSize);
                    canvas.drawRect(selRect, paint);
                }
            }

so I wanted that using the event onTouch wherever I touch it
"selects" (paints it in a diferent color)  that square that I touched.
so far I only reached the event.getX() and event.getY() but I don't
know how those can be usefull to get the whole square that was
touched.

Thanks in advance, any help would be much appreciated.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment