0

I have a Greeting Card activity where one can place Text and Image at desired locations by moving ImageView and TextView using Touch events. After moving to location I want to save its position and use same for other greetings. But neither view.GetX(), view.GetY() nor view.Left, view.Top is working.

I can capture the XY after moving but when using same XY coordinates on SetX & SetY or Left & Top the view is placed at weird locations.

This is my XML layout and using Touch even I move imgV1, txtName and txtSignature.

<RelativeLayout
    android:background="#ffffff"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/layoutControl"
    android:layout_below="@+id/toolbar1"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/pBar"
            android:layout_gravity="center_vertical" />            
    </LinearLayout>
    <FrameLayout
        android:orientation="vertical"
        android:id="@+id/GreetingFrame"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <FrameLayout
            android:id="@+id/layoutPhoto"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <ImageView
                android:layout_gravity="bottom|right|center_vertical|center_horizontal|center"
                android:id="@+id/imgV1"
                android:layout_width="135dp"
                android:layout_height="135dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="65dp"
                android:scaleType="matrix"/>
        </FrameLayout>
        <ImageView
            android:id="@+id/frameImage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:src="@drawable/digicard"
            android:adjustViewBounds="true"
            android:layout_alignParentBottom="true"/>
        <TextView
            android:textSize="18sp"
            android:textColor="#000000"
            android:layout_gravity="bottom|right|center_vertical|center_horizontal|center"
            android:id="@+id/txtName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="35dp"
            android:layout_marginBottom="55dp"
            android:text="Your Name"/>
        <TextView
            android:textSize="22sp"
            android:textStyle="bold"
            android:textColor="#ffffff"
            android:layout_gravity="bottom|right|center_vertical|center_horizontal|center"
            android:id="@+id/txtSignature"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="80dp"
            android:layout_marginBottom="2dp"
            android:text="9999999999"/>
    </FrameLayout>
</RelativeLayout>

TextView touch event

private void TxtName_Touch(object s, TouchEventArgs e)
    {
        TextView v = (TextView)s;

        if (e.Event.Action == MotionEventActions.Move)
        {
            v.SetX(e.Event.RawX - v.Width / 2.0f);
            v.SetY(e.Event.RawY - v.Height / 2.0f);                
        }
    }

Any suggestions ?

seopower
  • 127
  • 4
  • 14

1 Answers1

0

Modify your code as below ,it works fine on my side .

float dX, dY;
private void TextView_Touch(object sender, Android.Views.View.TouchEventArgs e)
{
   TextView view = (TextView)sender;

   int X = (int)e.Event.RawX;
   int Y = (int)e.Event.RawY;
   switch (e.Event.Action)
   {
       case MotionEventActions.Down:
           dX = view.GetX() - X;
           dY = view.GetY() - Y;
           break;

       case MotionEventActions.Move:
           view.Animate().X(X + dX).Y(Y + dY).SetDuration(0).Start();
           break;
    }
}

Refer to https://stackoverflow.com/a/31094315/8187800.

ColeX
  • 14,062
  • 5
  • 43
  • 240