Android 在带有背景图像的屏幕上放置和图像

Android placing and image on a screen with a background image

对于这个简单的问题,我深表歉意。我写了一个简单的故事应用程序,使用 Android,但要添加一些交互。我认为添加一些可点击的隐藏图片对于阅读故事的年轻人 reader 来说会很有趣。我能够将隐藏的图像放在屏幕上并且有效。现在我想将它们放在屏幕上的特定位置。例如,当您触摸树枝时,就会出现树上的松鼠。某处是否有说明如何执行此操作的教程。我还需要它与其他屏幕尺寸保持一致,即平板电脑、手机等。

谢谢

尝试覆盖 onTouch 方法并在触摸事件发生的坐标处添加图像。

public boolean onTouch(View v, MotionEvent event) {

  if (event.getAction() == MotionEvent.ACTION_DOWN){
      int x = (int) event.getX() ;
      int y = (int) event.getY();
      RelativeLayout.LayoutParams lp =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);; //Assuming you use a RelativeLayout
      ImageView iv=new ImageView(getApplicationContext());
      lp.setMargins(x,y,0,0);
      iv.setLayoutParams(lp);
      iv.setImageDrawable(getResources().getDrawable(/*id of your image*/));
      ((ViewGroup)v).addView(iv);
  }

并确保您覆盖 onTouch 的布局是 RelativeLayout,填满整个屏幕

你也可以在Github上看看下面这个项目,跟你要找的东西有点关系。 enter link description here