Get Center point in my app android -
in app horizontalscrollview present. consist of linearlayout in it, layout have added number of buttons it.viewflipper present in it. flip layout want move horizontalscrollview respective button should center position. layout(s) , number of button(s) same. 1st layout 1st button should @ center location?
thnx help....
hmm, okay, point out, when first or last position (possibly first + 1, last - 1 well, depending on button size), won't able have in center, can't overscroll horizontalscrollview. in mind, general case (you can handle edge cases -- i'd suggest leaving scrolled far can, , giving selected button sort of highlight) should able this:
displaymetrics metrics = new displaymetrics(); getwindowmanager().getdefaultdisplay().getmetrics(metrics); int screenwidth = metrics.widthpixels; //get instance of linearlayout, hsv, , viewflipper linearlayout ll = (linearlayout)findviewbyid(r.id.my_linear_layout); viewflipper vf = (viewflipper)findviewbyid(r.id.my_view_flipper); horizontalscrollview hsv = (horizontalscrollview)findviewbyid(r.id.my_hsv); //as you've said, there should equal number of buttons //views in viewflipper. code assumes true. view v = ll.getchildat(vf.getdisplayedchild()); //assuming smoothscrollto scrolls left side of screen, //which think does, want scroll button's center //point, minus (shifted right) half screen width int scrollto = v.getleft() + (v.getwidth() - screenwidth) / 2); //handle overflow edge cases if (scrollto < 0) scrollto = 0; else if (scrollto > hsv.getwidth()) scrollto = hsv.getwidth(); hsv.smoothscrollto(scrollto);
untested, , i'm prone small syntax errors, general idea may help. :)
Comments
Post a Comment