1

If I have a LinearLayout containing ImageViews, how could I write code to tell which, if any, is clipped by the edge of the screen?

<LinearLayout android:id="@+id/imagecontainer"
              android:orientation="horizontal"
              android:layoutHeight="wrap_content"
              android:layoutWidth="fill_parent">

    <ImageView android:id="@+id/image1" .../>
    <ImageView android:id="@+id/image2" .../>

     ...

    <ImageView android:id="@+id/imageN" .../>

</LinearLayout>

I imagine something like, which would return an index or 0 if nobody is clipped. The semantics of the function call aren't really important... I just need some way to tell if there is clipping and if so, who is it?

int whichImageIsClipped(LinearLayout root) { ... }
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • Why do you want to know which image is clipped? What do you plan on doing to the clipped image? – Noel Jun 11 '11 at 20:20
  • 1
    I plan on removing it from the layout. I want to display as many as I can, and then move the rest into overflow. – i_am_jorf Jun 11 '11 at 22:56
  • Are you sure you don't want to use a ListView? http://developer.android.com/reference/android/widget/ListView.html – Noel Jun 11 '11 at 23:46
  • Yes. For one, it's a horizontal thing. Secondly, I don't want it to scroll. Imagine a toolbar with buttons, and then buttons that don't fit in an overflow container... or something along those lines. I am flexible on the root layout view, however. If there is something better than LinearLayout for what I want, great. – i_am_jorf Jun 11 '11 at 23:54

1 Answers1

5

This may be a stretch, but you could try getGlobalVisibleRect(android.graphics.Rect, android.graphics.Point) on each of your children. If it returns false, you know it's completely out of view. If it returns true, you will need to compare the returned Rect with the expected size of your image.

Does that work for what you need?

Here is the code, in case anyone needs it:

public static Boolean isViewClipped(View view) {
  Rect rect = new Rect();
  Boolean completelyObscured = !view.getGlobalVisibleRect(rect);
  return completelyObscured || rect.width() < view.getWidth();
}
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
Noel
  • 7,350
  • 1
  • 36
  • 26