0

For a layout in a prototype I need a button that overlays a section (an inner rectangle) of an ImageView with an image that is scaled-up preserving aspect ratio. Is there some way to overlay the button over the imageview with defined margins, then scale up this combination preserving the margins? I tried putting both in an additional relativelayout, but the child elements did not scale within the relativelayout (like 2 UIViews might scale when scaling the parent UIView). I also tried hooking into the imageview using adjustViewBounds to shrink the imageview to the scaled-up image, but this didn't work either.

Here is the current layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp"
    tools:context=".MainActivity" >
        <ImageView
            android:id="@+id/placeholderImage"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/placeholder"
            android:padding="0dp"
            android:adjustViewBounds="true"/>
        <Button
            android:id="@+id/btn"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/darker_gray"
            android:onClick="show"/>
</RelativeLayout>
k k
  • 1,331
  • 2
  • 10
  • 12

1 Answers1

0

I ended up doing this programmatically using some of the answers here: Find the position of a bitmap...

To summarize the approach:

  • in onCreate use a globalLayoutListener
  • in the onGlobalLayout callback, get the scale of the drawable using getImageMatrix, then getValues. Since I'm maintaining aspect ratio either MSCALE_X or MSCALE_Y will work.
  • get the bounds of the drawable, which will vary from the resource png's dimensions due to pre-scaling for target density. Since the resource png's size is known, compute this 'prescale' and multiply by the Matrix scale to get totalScale
  • apply totalScale to the button size and offsets using LayoutParams.
Community
  • 1
  • 1
k k
  • 1,331
  • 2
  • 10
  • 12