android - Get draw bounds/rect of a view -
i'm developing app lot of views can rotated - it's map of physical objects. have detect when 2 objects (all objects rectangles/squares) overlapping , if user has performed single/double/long tap on object. reason need know drawing bounds of view.
let's @ example image bellow - green rectangle rotated 45 degrees. need coordinates of 4 corners of green rectangle. if use view.gethitrect() returns bounding box (marked in red) of view, of no use me.
do know how coordinates of edges of view?
the solution think of subclass view, manually store initial coordinates of corners , calculate new values on every modification view - translation, scale , rotation wondering if there better method.
p.s. app should working on android 2.3 4.0+ solutions welcomed.
thanks pskink explored again matrix.mappoints method , managed proper coordinates of corners of rectangle.
if running on android 3.0+ can view's matrix calling myview.getmatrix() , map points of interest. had use 0,0 upper left corner , getwidth(),getheight() bottom right corner , map these coordinates matrix. after add view's x , y values real values of corners.
something like:
float points[] = new float[2]; points[0] = myview.getwidth(); points[1] = myview.getheight(); myview.getviewmatrix().mappoints(points); paint p = new paint(); p.setcolor(color.red); //offset point , draw on screen canvas.drawcircle(center.getx() + points[0], center.gety() + points[1], 5f, p);
if have support lower versions of android can use nineoldandroids. i've copied , modified 1 of internal methods view's matrix:
public matrix getviewmatrix() { matrix m = new matrix(); camera mcamera = new camera(); final float w = this.getwidth(); final float h = this.getheight(); final float px = viewhelper.getpivotx(this); final float py = viewhelper.getpivoty(this); final float rx = viewhelper.getrotationx(this);; final float ry = viewhelper.getrotationy(this); final float rz = viewhelper.getrotation(this); if ((rx != 0) || (ry != 0) || (rz != 0)) { final camera camera = mcamera; camera.save(); camera.rotatex(rx); camera.rotatey(ry); camera.rotatez(-rz); camera.getmatrix(m); camera.restore(); m.pretranslate(-px, -py); m.posttranslate(px, py); } final float sx = viewhelper.getscalex(this); final float sy = viewhelper.getscaley(this);; if ((sx != 1.0f) || (sy != 1.0f)) { m.postscale(sx, sy); final float spx = -(px / w) * ((sx * w) - w); final float spy = -(py / h) * ((sy * h) - h); m.posttranslate(spx, spy); } m.posttranslate(viewhelper.gettranslationx(this), viewhelper.gettranslationy(this)); return m; }
i've put method in overloaded class of view (in case - extending textview). there on it's same in android 3.0+ instead of calling myview.getmatrix() call myview.getviewmatrix().
Comments
Post a Comment