2014-01-10 112 views
0

对于原型中的布局,我需要一个按钮,将ImageView的一个部分(一个内部矩形)与放大的图像进行叠加,以保留宽高比。 有没有一些方法来覆盖图像视图上的按钮与定义的边距,然后扩大这种组合保留边距?我试图把两者都放在一个额外的相关布局中,但子元素没有在相关布局中进行缩放(比如在缩放父UIView时可能会缩放2个UIViews)。我也尝试使用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> 
+0

您是否尝试在版面中添加重量? layout_weight = 1等 – Saqib

回答

0

我结束了这个编程方式使用这里的一些答案做: Find the position of a bitmap...

总结方法:

  • 中的onCreate使用globalLayoutListener
  • 在onGlobalLayout回调中,获取比例使用getImageMatrix绘制drawable,然后getValues。由于我保持宽高比,MSCALE_X或MSCALE_Y都可以工作。
  • 得到drawable的边界,由于目标密度的预缩放,这将与资源png的维度有所不同。由于资源png的大小已知,因此计算此“预分频”并乘以矩阵比例得到totalScale
  • 使用LayoutParams将totalScale应用于按钮大小和偏移量。