Изменение размера ImageView при перетаскивании - PullRequest
0 голосов
/ 04 мая 2020

В настоящее время я пытаюсь настроить перетаскивание с помощью imagesView.

Движение в порядке, но когда изображение приближается к правому и нижнему краям RelativeLayout, оно автоматически изменяется.

Это изменение размера отсутствует на левом и верхнем краях относительного макета.

Может кто-нибудь объяснить мне, почему размер изображения изменяется и как сохранить его в исходном состоянии?

Вот код:

Java

        piece1 = (ImageView) findViewById(R.id.piece1);
        piece2 = (ImageView) findViewById(R.id.piece2);

        piece1.setOnTouchListener(pieceListener);
        piece2.setOnTouchListener(pieceListener);


        ....

   View.OnTouchListener pieceListener = new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        RelativeLayout.LayoutParams relativeLayoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                Log.d("TAG","Start Drag");
                // Where the user started the drag
                pressed_x = (int) event.getRawX();
                pressed_y = (int) event.getRawY();

                // On sauvegarde la position de départ
                dep_x = (int) event.getRawX();
                dep_y = (int) event.getRawY();
                break;

            case MotionEvent.ACTION_MOVE:
                Log.d("TAG","Drag en cours");
                // Where the user's finger is during the drag
                final int x = (int) event.getRawX();
                final int y = (int) event.getRawY();

                // Calculate change in x and change in y
                int dx = x - pressed_x;
                int dy = y - pressed_y;

                // Update the margins
                relativeLayoutParams.leftMargin += dx;
                relativeLayoutParams.topMargin += dy;
                view.setLayoutParams(relativeLayoutParams);

                // Save where the user's finger was for the next ACTION_MOVE
                pressed_x = x;
                pressed_y = y;
                break;

            case MotionEvent.ACTION_UP:
                Log.d("TAG","End Drag");

                //Si la position n'a pas changé par rapport au départ on applique la rotation
                if (pressed_x == dep_x && pressed_y == dep_y)
                {
                    view.setRotation(view.getRotation()+180);
                }
                break;
        }

        return true;
    }
};

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/fond_main">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/tablette">
        </ImageView>
        <ImageView
            android:id="@+id/piece1"
            android:layout_width="@dimen/size_paques_fleches_width"
            android:layout_height="@dimen/size_paques_fleches_height"
            android:src="@drawable/fleche">
        </ImageView>
        <ImageView
            android:id="@+id/piece2"
            android:layout_width="@dimen/size_paques_fleches_width"
            android:layout_height="@dimen/size_paques_fleches_height"
            android:src="@drawable/fleche_selec"
            android:layout_marginTop="100dp">
        </ImageView>
    </RelativeLayout>
    <LinearLayout
        android:orientation= "horizontal"
        android:layout_weight="0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="@dimen/size_chronometer"
            android:layout_height="@dimen/size_chronometer"
            android:layout_gravity="center"
            android:clickable="true"
            android:focusable="true"
            android:src="@drawable/chrono" />
        <Chronometer
            android:id="@+id/simpleChronometervirus"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="@dimen/size_chronometer"
            android:textColor="#f0f6f7"/>
    </LinearLayout>
</LinearLayout>
</RelativeLayout>
...