0

I have a custom floorplanView class within my main Activity. This custom view uses up roughly half the screen space. I am trying to simply create a star image and place it on the floor plan onTouch. However, for some reason this appears to place the image below where I am touching (I have tried a few variations and cannot work it out).

 public class FloorplanView extends RelativeLayout {

    public FloorplanView(Context context, AttributeSet attrs) {
        super(context, attrs);

        SCROLL_THRESHOLD = getResources().getDimensionPixelSize(R.dimen.margin5);
        setClipChildren(false);
        setOnTouchListener(new FloorplanTouchListener());
    }

    public class FloorplanTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    posX = event.getRawX() - v.getX();
                    posY = event.getRawY() - v.getY();
                    break;

                case MotionEvent.ACTION_UP:
                    ImageView img = new ImageView(getContext());
                    img.setOnTouchListener(new FlagTouchListener());
                    img.setBackground(getResources().getDrawable(android.R.drawable.btn_star));
                    img.setX(event.getRawX()); //Used posX/Y without success
                    img.setY(event.getRawY());
                    addView(img);
                    break;
            }

            return true;
        }
    }
}

Further to this, is there a way to set bounds to my custom class with extends a relativeLayout so that when I drag and move my image (which is setup and working) it does not go outside of the Floorplanview custom class.

EDIT: The parent Activity class XML

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.fcs_systems.inspector.FloorplanActivity">

    <TextView
        android:id="@+id/txtFloorplanName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Floorplan Name"
        android:visibility="gone"/>

    <com.fcs_systems.inspector.FloorplanView
        android:id="@+id/floorplanView"
        android:background="@color/fcs_red"
        android:layout_width="match_parent"
        android:layout_height="200dp"/>

</RelativeLayout>
SingleWave Games
  • 2,618
  • 9
  • 36
  • 52

1 Answers1

0

According to the documentation,

float getRawX () : Returns the original raw X coordinate of this event. For touch events on the screen, this is the original location of the event on the screen, before it had been adjusted for the containing window and views. (Source : https://developer.android.com/reference/android/view/MotionEvent.html#getRawX() )

This is why getRawX() and getRawY() seem to place the image lower than where you tapped. You should simply use the getX() and getY() methods on the MotionEvent instead.

As for your images going out of the RelativeLayout, this will depends on two things :

  1. Add your images to your RelativeLayout (not sure what your actual addView(img); does)

  2. Make sure your RelativeLayout doesn't have https://developer.android.com/reference/android/view/ViewGroup.html#setClipChildren(boolean) enabled

This will make sure that your images belong to your RelativeLayout container, and will not be drawn when moved outside of it.

NSimon
  • 5,212
  • 2
  • 22
  • 36
  • I have used getX and getY now which works, except it is still slightly out (I am assuming this is getting skewed by some sort of padding/margin). I have added setClipChildren(false); Finally, currently when I drag the star from the floorplan view it disappears into the background (this is possibly due to the way I add the view). Currently my custom view simply uses extend RelativeLayout. – SingleWave Games Jan 09 '18 at 10:00
  • I have added the parent XML to show how I am using my custom view within the main Activity – SingleWave Games Jan 09 '18 at 10:02
  • Thanks for the addition. As for the "still slightly out", this could come from the fact that you're adding the ImageView where you place your finger. But bare in mind it means you're placing the "top left corner of the image" to where you tap. You'll have to add half the width of the image on X and half the height of the image on Y in order to have the ImageView centered to where you tap. As for the view disappearing when you drag it, it can come from a number of reasons that I don't see here. Maybe the error sits in your addView(img). Could you post it too? – NSimon Jan 09 '18 at 10:12
  • addView() is how I am attempting to add to my custom class. Check out my slightly amended code to help show the structure of my code a little better – SingleWave Games Jan 09 '18 at 10:16
  • Could this link https://stackoverflow.com/a/22930854/4232337 help you with the clipping? The inspector can help you as well identifying if there are custom ViewGroup being added by the system which would not have the clipChildren(false). – NSimon Jan 09 '18 at 10:30