简体   繁体   中英

Insert image to screen at touch co-ordinates

I am making an app which requires an image to be placed on the screen at the location the user clicks. The user must be able to insert multiple images on the screen at once.

So far I have set up the image that will be inserted onto the screen as a drawable

@drawable/texture

I have also put in a touch listener in my activity that should get the co-ordinates . I have added this method to the android:onClick on my Layout.

      public boolean onTouch(View v, MotionEvent event) {
      if (event.getAction() == MotionEvent.ACTION_DOWN){
          float x = event.getX() ;
          float y = event.getY();

      }

  } 

This is my first question on stackoverflow so if I have forgotten any information you need please just ask.

Thanks

Try this:

  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);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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