【问题标题】:Place an Image (overlay) over x,y of another image(background)将图像(叠加)放在另一个图像(背景)的 x,y 上
【发布时间】:2023-03-09 20:13:01
【问题描述】:

我需要在运行时将图像动态放置在另一个图像上,恰好位于背景图像的 x,y 处

我有以下详细信息:

叠加图像属性(红色):x = 58, y =232, h=266, w=431
背景图片(黑色):match_parent, w=1024,h=768

这里 x,y 是叠加层的左上角。

注意:叠加图像的 x,y 是相对于背景图像而不是整个屏幕布局。当 ActionBar 隐藏时,背景图像可能会被拉伸。背景图像的 layout_width 和 layout_height 设置为 match_parent。并且 scaleType 是“中心”。

使用以下代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    RelativeLayout root = (RelativeLayout) findViewById(R.id.rootlayout);

    backIV = (ImageView) findViewById(R.id.imageView1);

    Content = new ImageView(this);
    Content.setImageDrawable(getResources().getDrawable(R.drawable.content_1));
    params = new RelativeLayout.LayoutParams(431, 266);

    rl.addView(Content, params);

    root.addView(rl);


}

从上面的代码

[我想要什么]

从关于 SO 的许多解决方案和建议中,没有一个对我有用。相信我,我不会不尝试就发布这个问题。

【问题讨论】:

  • 在RelativeLayout中为红色矩形设置内边距
  • 你没有设置你想要重叠的图像的布局参数的 x 和 y。你只指定了高度和宽度(图像的大小)而不是它的位置

标签: android android-layout


【解决方案1】:

查看我对这个问题的回答:How to maintain multi layers of ImageViews and keep their aspect ratio based on the largest one?

这是一个扩展的 ImageView,支持多个 Drawable 层

【讨论】:

  • 完全按照我的意愿工作。非常感谢。 :) 我现在只需要修改我的布局以使用 LayeredImageView 而不是 ImageView。
  • 嘿,我需要叠加图像上的 OnCLickListeners。怎么做。请提供意见。
  • 我无法弄清楚如何实现它。您能否给我一些关于如何为覆盖的图像可绘制对象设置 onclickListeners 的代码解释。会有多个叠加层。我需要的是你点击图片 1、图片 2 等等。
  • 我还没试过,但我认为你应该调用 matrix.mapRect 两次,然后检查点是否在里面
  • 我正在使用接触点(event.getX(),event.getY())。使用它正在检查正在触摸哪个图像叠加层。基于此,我正在执行我需要的操作。
猜你喜欢
  • 2021-12-01
  • 2017-03-10
  • 1970-01-01
  • 2012-10-07
  • 1970-01-01
  • 2011-05-25
  • 2018-04-23
  • 1970-01-01
  • 2011-06-26
相关资源
最近更新 更多