Координаты изображения не меняются, когда фоновое изображение увеличивается или перемещается - PullRequest
0 голосов
/ 27 сентября 2018

Я пытаюсь создать приложение, в котором я могу построить любое изображение (bg), отметить его и получить его координаты.Поэтому, если я увеличу фоновое изображение, координаты останутся прежними, если я не переместу изображение курсора.И часть метки изображения тоже движется, если фоновое изображение увеличено. введите описание изображения здесь вот снимок экрана приложения и мой код

public class MainActivity extends AppCompatActivity implements View.OnTouchListener {

int windowwidth; // Actually the width of the RelativeLayout.
int windowheight; // Actually the height of the RelativeLayout.
private ImageView mImageView;
private PhotoView bg;
private Button btnMark;
private ImageView marker;

private float centreX;
private float centreY;

private TextView Xc;
private TextView Yc;
private ViewGroup mRrootLayout;
private int _xDelta;
private int _yDelta;

private TextView pickedX;
private TextView pickedY;

String x;
String y;

float canX;
float canY;
// Marker x and Y
private float markerX;
private float markerY;

@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initilizeUI(); // initializing UI

    mImageView.setOnTouchListener(this);

    onClickUI();// call all the onclick buttons, images here


    // Capture the width of the RelativeLayout once it is laid out.
    mRrootLayout.post(new Runnable() {
        @Override
        public void run() {
            windowwidth = mRrootLayout.getWidth();
            windowheight = mRrootLayout.getHeight();
        }
    });
}

private void initilizeUI(){

    mRrootLayout = (ViewGroup) findViewById(R.id.root);

    Xc = (TextView) findViewById( R.id.Xc );
    Yc = (TextView) findViewById( R.id.Yc );
    mImageView = (ImageView)findViewById(R.id.im_move_zoom_rotate);
    bg = (PhotoView) findViewById( R.id.bg );
    btnMark = (Button) findViewById( R.id.btnMarkCordinates );
    pickedX = (TextView) findViewById( R.id.pickX ) ;
    pickedY = (TextView) findViewById( R.id.pickY ) ;
    marker = (ImageView) findViewById( R.id.marker );

}

// Tracks when we have reported that the image view is out of bounds so we
// don't over report.

private boolean  isOut = false;
public boolean onTouch(View view, MotionEvent event) {
    final int X = (int) event.getRawX();
    final int Y = (int) event.getRawY();

    // Check if the image view is out of the parent view and report it if it is.
    // Only report once the image goes out and don't stack toasts.

    switch (event.getAction() & ACTION_MASK) {
        case ACTION_DOWN:
            // _xDelta and _yDelta record how far inside the view we have touched. These
            // values are used to compute new margins when the view is moved.
            _xDelta = X - view.getLeft();
            _yDelta = Y - view.getTop();
            break;
        case ACTION_UP:
        case ACTION_POINTER_DOWN:
        case ACTION_POINTER_UP:
            // Do nothing
            break;
        case ACTION_MOVE:


            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view
                    .getLayoutParams();
            // Image is centered to start, but we need to unhitch it to move it around.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                lp.removeRule(RelativeLayout.CENTER_HORIZONTAL);
                lp.removeRule(RelativeLayout.CENTER_VERTICAL);
            } else {
                lp.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);
                lp.addRule(RelativeLayout.CENTER_VERTICAL, 0);
            }
            lp.leftMargin = X - _xDelta;
            lp.topMargin = Y - _yDelta;
            // Negative margins here ensure that we can move off the screen to the right
            // and on the bottom. Comment these lines out and you will see that
            // the image will be hemmed in on the right and bottom and will actually shrink.
            lp.rightMargin = view.getWidth() -  windowwidth;
            lp.bottomMargin = view.getHeight() - windowheight;

            view.setLayoutParams(lp);

            break;




    }

    getCenterPoint(); // get the center point of the image (cursor)


    return true;
}

private void onClickUI(){

    btnMark.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          //  getCenterPoint();
            pickedX.setVisibility( View.VISIBLE );
            pickedY.setVisibility( View.VISIBLE );
          marker.setVisibility( View.VISIBLE );
            pickedX.setText( "X = " + x );
            pickedY.setText( "Y = "+ y );

              marker.setX( markerX);
            marker.setY( markerY);



        }
    } );
}

private void getCenterPoint(){
    //Get the center point of the image

    centreX=mImageView.getX() + mImageView.getWidth()  / 2;
    centreY=mImageView.getY() + mImageView.getHeight() / 2;

    markerX = centreX - marker.getWidth() / 2;
    markerY = centreY - marker.getHeight() / 2;

    x = String.valueOf( centreX );
    y = String.valueOf( centreY );



    if (isOut) {
        isOut = false;

        Xc.setText( x );
        Yc.setText( y );


    }else{
        isOut = true;
    }
}

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:text="Mark"
    android:textSize="16sp"
    android:id="@+id/btnMarkCordinates"
    android:textStyle="bold" />

<!--<ImageView-->
    <!--android:id="@+id/bg"-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_alignParentStart="true"-->
    <!--android:layout_alignParentTop="true"-->
    <!--android:scaleType="center"-->
    <!--android:src="@drawable/spot_ball" />-->


<com.github.chrisbanes.photoview.PhotoView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/spot_ball"
    android:scaleType="center"
    android:id="@+id/bg"
    />


<ImageView
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/plus"
    android:visibility="invisible"
    android:id="@+id/marker"

    />

<ImageView
    android:id="@+id/im_move_zoom_rotate"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/plus"
    />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="X = "
        android:textSize="20sp"

        />
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello"
    android:textSize="20sp"
    android:id="@+id/Xc"
        /></LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Y = "
        android:textSize="20sp"

        />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Coordinates"
    android:textSize="20sp"
    android:id="@+id/Yc"
    /></LinearLayout>

</LinearLayout>

<TextView
    android:id="@+id/pickY"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignTop="@+id/pickX"
    android:layout_marginEnd="71dp"
    android:text="Coordinates"
    android:visibility="invisible"
    android:textSize="20sp" />

<TextView
    android:id="@+id/pickX"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="121dp"
    android:visibility="invisible"
    android:layout_marginStart="71dp"
    android:text="Coordinates"
    android:textSize="20sp" />

...