android - How to know whether View inside Scroll is Visible completely or no -
there scroll view between header(at top of screen) , tabs(bottom of screen). want know whether imageview inside scrollview visible or not on phone screen window.
i suggest following way (the approach similar 1 in this question).
e.g. have following xml (i'm not sure header , tabs missed):
<scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/scroller"> <imageview android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:id="@+id/image" android:src="@drawable/image001" android:scaletype="fitxy" /> </scrollview>
then activity might following:
public class myactivity extends activity { private static final string tag = "myactivity"; private scrollview mscroll = null; private imageview mimage = null; private viewtreeobserver.ongloballayoutlistener mlayoutlistener = new viewtreeobserver.ongloballayoutlistener() { @override public void ongloballayout() { final rect imagerect = new rect(0, 0, mimage.getwidth(), mimage.getheight()); final rect imagevisiblerect = new rect(imagerect); mscroll.getchildvisiblerect(mimage, imagevisiblerect, null); if (imagevisiblerect.height() < imagerect.height() || imagevisiblerect.width() < imagerect.width()) { log.w(tag, "image not visible"); } else { log.w(tag, "image visible"); } mscroll.getviewtreeobserver().removeongloballayoutlistener(mlayoutlistener); } }; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // show layout test view setcontentview(r.layout.main); mscroll = (scrollview) findviewbyid(r.id.scroller); mimage = (imageview) findviewbyid(r.id.image); mscroll.getviewtreeobserver().addongloballayoutlistener(mlayoutlistener); } }
in case of small image log: image visible.
however, should aware following inconsistency (as per understanding): if have big image, doing scaling (e.g. set android:layout_width="wrap_content"
) when scaled, actual imageview
height full height of image (and scrollview
scrolling), adjustviewbounds might needed. reason behavious framelayout
doesn't care layout_width , layout_height of childs.
Comments
Post a Comment