将按钮匹配到缩放的imageview

时间:2014-01-10 18:29:50

标签: android android-layout

对于原型中的布局,我需要一个按钮,该按钮覆盖ImageView的一个部分(内部矩形),其图像按比例放大,保留纵横比。 是否有某种方法可以在具有定义边距的图像视图上叠加按钮,然后按比例放大此组合以保留边距?我尝试将两者放在一个额外的relativelayout中,但是子元素没有在relativelayout中缩放(比如2 UIViews可能在缩放父UIView时缩放)。我还尝试使用adjustViewBounds将imageview缩小到放大的图像,但这也不起作用。

这是当前的布局

<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>

1 个答案:

答案 0 :(得分:0)

我最终使用这里的一些答案以编程方式执行此操作: Find the position of a bitmap...

总结方法:

    onCreate中的
  • 使用globalLayoutListener
  • 在onGlobalLayout回调中,使用getImageMatrix获取drawable的比例,然后获取getValues。由于我保持宽高比,MSCALE_X或MSCALE_Y都可以使用。
  • 得到drawable的边界,由于目标密度的预缩放,它将与资源png的尺寸不同。由于资源png的大小已知,请计算此“预定标量”并乘以矩阵比例以获得totalScale
  • 使用LayoutParams将totalScale应用于按钮大小和偏移。
相关问题