简体   繁体   English

android studio (java) 中图像上的可点击区域

[英]clickable areas over an image in android studio (java)

I've inherited an Android Studio project with a challenging goal.我继承了一个具有挑战性目标的 Android Studio 项目。 Basically, the idea is that the app displays an image which is the map of a building (museum).基本上,这个想法是应用程序显示一个图像,它是建筑物(博物馆)的地图。 When a user clicks on a room, the app displays the name of that room, and then they have some other options to do things.当用户点击一个房间时,应用程序会显示该房间的名称,然后他们有其他一些选项来做事。 This works fine as long as we design the app for use one a single device and don't ever plan to use it on anything else, which of course is silly.只要我们将应用程序设计为在单个设备上使用并且不打算在其他任何设备上使用它,这就会很好地工作,这当然很愚蠢。 I have been able to get the map image to be responsive to different size screens just fine.我已经能够让地图图像对不同尺寸的屏幕做出反应就好了。 The problem I have is with the buttons, which were drawn over portions of the map during design.我遇到的问题是按钮,它们是在设计过程中绘制在地图的一部分上的。

I have used ConstraintLayout, but it isn't doing what I need.我使用了 ConstraintLayout,但它没有做我需要的。 For one, the buttons don't really move the way I would expect.一方面,按钮并没有像我期望的那样移动。 For example, when I run the app on a tablet, the image resizes fine, but the buttons are all clustered in one quadrant;例如,当我在平板电脑上运行应用程序时,图像可以很好地调整大小,但按钮都聚集在一个象限内; they didn't really move with the image resize the way I expected.他们并没有真正按照我预期的方式调整图像大小。

The second issue is the button size.第二个问题是按钮大小。 Even if the buttons did move when on a larger screen (let's just assume they did), the buttons themselves don't resize.即使按钮在较大的屏幕上确实移动了(让我们假设它们确实移动了),按钮本身也不会调整大小。 I have tried WRAP_CONTENT, but then it sizes it larger than I want it even in design.我已经尝试过 WRAP_CONTENT,但是即使在设计中它的大小也比我想要的大。 Setting it to 0dp just makes a 1 pixel button (not sure why).将其设置为 0dp 只会生成一个 1 像素的按钮(不知道为什么)。 Is there some way to get the button size to grow in proportion to the image growth when responding to different screen sizes?在响应不同的屏幕尺寸时,是否有某种方法可以使按钮大小与图像增长成比例增长? In other words, in the layout design, I want to be able to specify the button dimensions so that they cover the exact portion of the map image I want, but then when the image grows, I want the button to grow a proportional amount, so that it mostly stays lined up with the room to which it corresponds.换句话说,在布局设计中,我希望能够指定按钮尺寸,以便它们覆盖我想要的地图图像的确切部分,但是当图像增大时,我希望按钮按比例增大,以便它主要与它对应的房间对齐。

I have read about using an overlay image with colored areas as a workaround, but this won't be practical in some room images where there will be in excess of 20 clickable areas.我已经阅读了有关使用带有彩色区域的叠加图像作为解决方法的内容,但这在某些房间图像中不切实际,因为可点击区域超过 20 个。

I hope that explanation is clear enough.我希望这个解释足够清楚。 Thanks in advance for any help.在此先感谢您的帮助。

In your position, I would advise you to look at libraries that have the functionality you need, for example, AndroidImageMap .在您的职位上,我建议您查看具有您需要的功能的库,例如AndroidImageMap If necessary, you can download them, insert them into your project as a module and change the code.如有必要,您可以下载它们,将它们作为模块插入到您的项目中并更改代码。 Also check these questions, I think it will make it clearer: clickable area of image , How to make certain areas in an image clickable in android还要检查这些问题,我认为它会更清楚: 图像的可点击区域如何使图像中的某些区域在android中可点击

That's not that hard to achieve, you need to override onMeasure in the parent ViewGroup that has the image in the background, and knowing its original size you get the scale to the new dimension you wanna fit then you multiply all coordinates by it: the background image and all buttons l,t,r,b in a similar way AbsoluteLayout works这并不难实现,您需要在背景中具有图像的父 ViewGroup 中覆盖onMeasure ,并且知道其original sizeget the scale to the new dimension您想要适合get the scale to the new dimension ,然后multiply all coordinates它:背景图像和所有按钮l,t,r,b以类似的方式 AbsoluteLayout 工作

I myself also prefer to override onDraw and onTouchEvent and draw/click the buttons in the same component, but you can make it work with the android.Button我自己也更喜欢覆盖onDrawonTouchEvent并在同一组件中绘制/单击按钮,但您可以使其与 android.Button 一起使用

I can provide more help if you add your code如果您添加代码,我可以提供更多帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM