【问题标题】:Find the position of a bitmap in android imageview when container view scaled centerInside当容器视图缩放centerInside时在android imageview中查找位图的位置
【发布时间】:2012-09-04 12:09:12
【问题描述】:

我有一个包含自定义 ImageView、scaleType="centerInside" 的 RelativeLayout,我在位图中加载(通常小于 imageView)。如何获得绘制位图的顶部/左侧位置?我需要能够在相对于位图的位置上添加视图。

   RelativeLayout view = (RelativeLayout) inflater.inflate(R.layout.scroll_scaled, container, false);
ContentImageView image = (ContentImageView) view.findViewById(R.id.base_page);
Bitmap bm = mInterfaceActivity.getPageImage(mPageNumber);
image.setImageBitmap(bm);`

布局文件scrolled_scaled

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
    android:scaleType="centerInside"
    android:id="@+id/base_page"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff00ff00"
    android:contentDescription="@string/product_page"
    android:src="@android:drawable/ic_menu_report_image" >
</ImageView>
</RelativeLayout>

【问题讨论】:

    标签: android bitmap imageview android-relativelayout


    【解决方案1】:

    更新 2:如果您使用未指定的宽度和高度(例如 wrap_content),getX 和 getY 将返回 0。而不是 iv.getX()iv.getY() 替换为这个问题的答案:Getting View's coordinates relative to the root layout 然后将图像的边界添加到这些值。

    您可以通过将 ImageView 的位置添加到内部可绘制对象的左上边界来做到这一点。像这样的:

    ImageView iv = (ImageView)findViewById(R.id.image_view);
    Drawable d = iv.getDrawable();
    Rect bounds = d.getBounds();
    int top = iv.getY() + bounds.top;
    int left = iv.getX() + bounds.left;
    

    更新:对于缩放的图像,您必须将顶部和左侧坐标乘以图像比例才能获得更准确的定位。你可以这样做:

    Matrix m = iv.getImageMatrix();
    float[] values = new float[9];
    m.getValues(values);
    float scaleX = values[Matrix.MSCALE_X];
    float scaleY = values[Matrix.MSCALE_Y];
    

    然后你必须将 top 乘以 scaleY,将 left 乘以 scaleX。

    【讨论】:

    • 没有用。我认为我的 ViewGroup 此时尚未完成膨胀,因此“top”和“left”的值都为零。
    • 查看更新的答案以获得更好的获取视图顶部和左侧坐标的方法。
    • 您的答案假定边界将具有顶部和左侧值以匹配它们相对于View 的绘制位置。不是这种情况。它们总是 0。
    • 在所有情况下它们并不总是 0。我在 ImageView 中使用图像的顶部和左侧边界来限制缩放时的图像转换。可以在此处找到有效的代码:stackoverflow.com/questions/4227451/… 在这种情况下,可能尚未调用 onLayout,因此边界为 0。如果是这种情况,则延迟获取位置,直到调用 onLayout 之后.
    • 有趣,因为我从一个按钮测试了这个问题,所以它已经执行了 onLayout。我想知道为什么会有差异。
    【解决方案2】:

    您需要使用Drawable 的边界自己计算。

    ImageView test = (ImageView) findViewById(R.id.base_page);
    Rect bounds = test.getDrawable().getBounds();
    int x = (test.getWidth() - bounds.right) / 2;
    int y = (test.getHeight() - bounds.bottom) / 2;
    

    首先我们计算View 中未被图像使用的空间。然后,由于它居中,因此额外的空间在图像之前和之后均匀分布,因此它将该长度的一半绘制到 View 中。

    这些数字与视图的位置相关,但如果需要,您可以添加视图 X 和 Y。

    【讨论】:

    • 它不起作用:(我在 test.getdrable().getBounds() 上有空指针异常
    • @ArslanAhmad 在我看来,您可能遇到了超出此问题范围的错误。我建议你开始一个新问题来确定为什么那是空的。在这样做之前,我建议您实际确定该命令的哪一部分为空(test == nulltest.getDrawable() == null
    【解决方案3】:

    根据反馈和一些重试,最终得到了一个两部分的解决方案。

    我创建了子视图并将它们添加到“近似”中的 RelativeLayout 位置,但作为 View.INVISIBLE。

    我对 RelativeLayout ViewGroup 进行了超分类,并在 onLayout 中行走 子视图列表并将它们放在我现在的“适当”位置 让 RelativeLayout 能够自我感知其扩展大小。

    看起来很笨重,但确实有效。

    感谢大家的建议,我的解决方案是听取大家的建议。

    【讨论】:

      【解决方案4】:

      该方法返回imageView内部图像的边界。

      /**
       * Helper method to get the bounds of image inside the imageView.
       *
       * @param imageView the imageView.
       * @return bounding rectangle of the image.
       */
      public static RectF getImageBounds(ImageView imageView) {
          RectF bounds = new RectF();
          Drawable drawable = imageView.getDrawable();
          if (drawable != null) {
              imageView.getImageMatrix().mapRect(bounds, new RectF(drawable.getBounds()));
          }
          return bounds;
      }
      

      【讨论】:

        猜你喜欢
        • 2017-06-29
        • 2014-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 2014-11-08
        相关资源
        最近更新 更多