gpt4 book ai didi

android - 如何在缩放背景图像上定位按钮

转载 作者:行者123 更新时间:2023-11-29 00:25:24 26 4
gpt4 key购买 nike

从 Android 开发开始,我想知道如何使用 XML(使用 Android Studio)针对 4.x Android 设备进行此布局。

如何在位图背景上精确定位 4 个按钮?尽管位图缩放,应该工作。

场景:我的应用程序主屏幕应包含放置在位图上的 4 个按钮。按钮定位应始终与此完全相同,并且整体应按比例放大/缩小(按宽度/高度比例)以匹配屏幕空间。布局草图:

layout sketch

尝试过的解决方案:

  • 带背景的 GridLayout 中的 4 个按钮:

    <GridLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal"
    android:columnCount="2"
    android:rowCount="2">

    <Button android:id="@+id/button1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Button1" />

    <Button android:id="@+id/button2"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Button2" />

    <Button android:id="@+id/button3"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Button3" />

    <Button android:id="@+id/button4"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="Button4" />
    </GridLayout>
  • GridLayout 中的 4 个按钮,位于 RelativeLayout 中的 ImageView 之上。

  • 一个带有 onTouch() 的 ImageView,用于检查颜色编码的位图。 ( source here )

在缩放方面唯一可行的解​​决方案是 onTouch(),但我宁愿继续使用实际按钮,以便键盘导航和按下/悬停效果按预期工作。

如何定位按钮,让它们始终与位图匹配比例?感谢您提供任何见解。

由于标题,这个问题看起来像是一个骗局,但我还没有找到任何可以证明这是如何实现的。

最佳答案

我遇到了这个问题,我用数学方法解决了它。这太可怕了,但是假设您的背景图像是 500x500px,并且在这样的图像上,您的按钮应该位于 (100, 100),相对于该背景图像的大小为 50x50px。当你布置它时,你必须得到当前设备的大小,假设它的大小是 700x800px(这些设备很可能不存在)。在这种情况下,您必须将按钮的宽度设置为 (700/500)*50px,将高度设置为 (800/500)*50px。按钮的位置将位于 (700/500)*100 和 (800/500)*100 处。

您几乎是在使用背景图像缩放按钮。希望我说清楚了:)

关于android - 如何在缩放背景图像上定位按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19998740/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com