【问题标题】:Placing an ImageView at the specific coordinates of another ImageView将一个 ImageView 放置在另一个 ImageView 的特定坐标处
【发布时间】:2014-07-30 09:06:07
【问题描述】:

您好,我正在寻找解决我遇到的问题的方法,在 Android 中有没有办法将一个 ImageView 放置在另一个 ImageView 上的指定坐标处?
例如,获取 ImageView A 的特定坐标(假设为死点),然后将 ImageView B 放在 ImageView A 的坐标上。导致 ImageView B 被放置在 ImageView B 的中间。然后允许我更改围绕坐标在 ImageView A 中移动 ImageView B。

抱歉,如果我以一种令人困惑的方式编写了此代码

【问题讨论】:

标签: java android imageview coordinates android-imageview


【解决方案1】:

我的想法是使用RelativeLayout 对齐两者的左上角,然后使用padding 偏移顶部。所以将im2 放在im1 x=20, y=15 上:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@id/+im1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@id/+im2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/im1"
        android:layout_alignLeft="@id/im1"
        android:paddingTop="15px"
        android:paddingLeft="20px" />
</RelativeLayout>

您可以通过 ImageView.setPadding 从代码中更新填充。

您可以尝试使用相同的边距,但在代码中弄乱边距很难。

还有一个早已弃用的AbsoluteLayout,但我不建议使用它,创建自己的布局管理器可能更容易,没那么难,可以封装计算代码很好地抵消你。

编辑: 使用上述方法(如果需要负数,则可以使用填充或边距),您可以移动图像并仍然让 x/y 由布局计算。

如果您想使两个图像居中,只需将android:layout_centerInParent="true" 添加到RelativeLayout 中即可。

如果你想移动它,我建议使用画布在上面绘制你的“演员”,然后将画布设置为在单个 ImageView 中显示;或在画布上使用问题 cmets 中的建议库。

【讨论】:

  • 看起来不错的解决方案,但为什么要填充?问题是如何设置完全相同的坐标,但使用填充会使一个视图相对于另一个视图偏移。
  • 这是一个很好的解决方案。我希望有一种以编程方式进行的方法
  • 我想 .setPadding 是一种通过代码设置第二个 imageview 出现位置的方法
  • 更新了,如果要编程,使用Canvas,最灵活。
  • 所以这里最好的解决方案可能是使用画布?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-22
  • 1970-01-01
相关资源
最近更新 更多