在android中,我可以将子视图添加到ImageView吗?

发布于 2024-12-02 19:30:32 字数 434 浏览 1 评论 0原文

我有一个布局,其中包含 ScrollView,其中包含各种组件,包括 ImageView。我发现我可以将 TouchListener 添加到 ImageView,并且为该事件获得的 x,y 值相对于 ImageView 的角(包括填充)有偏移。因此我可以轻松处理触摸事件,而不必担心 ScrollView 的状态。

问题是我没有找到在 ImageView 顶部添加视图的方法,以便在某些位置触摸 ImageView 时可以对 ImageView 进行视觉更改。

具体来说,图像中有各种框,我想在点击它们时突出显示它们。我可以向活动添加一个视图,但我必须考虑 ScrollView 的状态才能在正确的位置显示内容。

解决这个问题的最佳方法是什么?我可以派生 ImageView 的新版本,重载绘制方法,并以某种方式使用它吗? (不确定如何在 xml 中指定它,我可能需要在活动的 onCreate 中设置它)

I have a layout which contains a ScrollView which contains various components including an ImageView. I've found that I can add a TouchListener to the ImageView and the x,y values I get for the event are offset from the corner of the ImageView (including padding). Thus I can easily handle touch events without worrying about the state of the ScrollView.

The problem is that I don't see a way to add a view on top of the ImageView so that I can make a visual change to the ImageView when it's touched in certain locations.

Specifically, there are various boxes in the image which I'd like to highlight when they're tapped. I can add a view to the activity, but I'd have to take the state of the ScrollView into account in order to display things in the right spot.

What's the best way to approach this? Can I derive a new version of ImageView, overload the draw method, and use that somehow? (not sure how I'd specify it in the xml, I'd probably need to set it in the onCreate of the activity)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独陪着我 2024-12-09 19:30:32

您应该能够按照您的建议重写 ImageView 的 onDraw 方法,并在 ImageView 之上绘制内容。 onDraw 方法为您提供了可以在其上绘图的 Canvas。在 XML 中,您可以只使用您创建的新类的名称,而不是“ImageView”(除非您想在 ImageView 提供的参数之外添加参数,否则这应该非常简单)。

<your.new.awesome.class.with.the.full.namespace
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/awesome_image_view"
    />

问题是我没有找到在 ImageView 顶部添加视图的方法,以便在某些位置触摸 ImageView 时可以对 ImageView 进行视觉更改。

您似乎更愿意拥有可以处理的单独视图。您可以通过使用 FrameLayout 封装 ImageView,然后添加任何类型的另一个视图作为该 ImageView 的同级视图来实现此目的。如果在 FrameLayout 中 ImageView 之后提到该视图,则该视图将绘制在 ImageView 之上。此解决方案可能不是最有效的解决方案,但它可能会帮助您入门。重写 ImageView 的 onDraw 方法绝对是最好的选择。

You should be able to override the onDraw method of the ImageView as you suggested and draw stuff on top of the ImageView. The onDraw method gives you the Canvas that you could draw on. In the XML, you could just use the name of the new class you created instead of "ImageView" (unless you want to add parameters in addition to the ones offered by ImageView, this should be pretty straight forward).

<your.new.awesome.class.with.the.full.namespace
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/awesome_image_view"
    />

The problem is that I don't see a way to add a view on top of the ImageView so that I can make a visual change to the ImageView when it's touched in certain locations.

It seems that you are more comfortable with having a separate view that you could work on. You could do that by encapsulating the ImageView with a FrameLayout and then adding another view of any kind as a sibling of that ImageView. This view would be drawn on top of the ImageView if it was mentioned in the FrameLayout after the ImageView. This solution may not be the most efficient one, but it might get you started. Overriding the onDraw method of the ImageView would definitely be the best option.

暮色兮凉城 2024-12-09 19:30:32

您可以制作一个相对布局或带有背景的内容作为您想要的图像,而不仅仅是一个图像视图。那么您可以在该相对布局中拥有子视图。

you can make a relativelayout or something with a background as your desired image instead of having just an imageview. then you can have child views within that relativelayout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文