В моей программе картинка почему-то внезапно перестаёт двигаться

Также при повторном нажатии на кнопку, вызывающую передвижение, анимация продолжается. Не понимаю, как это происходит

    public class DeskSelection extends AppCompatActivity {
        private ImageView c2, c3, c4, c5, c6, c7, c8, c9, c10, b2, b3, b4, b5, b6, b7, b8, b9, b10, d2, d3, d4, d5, d6, d7, d8, d9, d10,
                h2, h3, h4, h5, h6, h7, h8, h9, h10, s2, s3, s4, s5, s6, s7, s8, s9, s10, sq, hq, dq, cq, sj, dj, hj, cj, ck, dk, hk, sk,
                sa, da, ha, ca, arrowRight, arrowLeft;
        public List<ImageView> cards;
        volatile static int counter = 0;
        private ViewGroup mMoveLayout;
        private int mX;
        private int mY;
    
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Display display = getWindowManager().getDefaultDisplay();
            Point p = new Point();
            display.getSize(p);
            Log.d("", "display.getSize() = " + (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, p.x, getResources().getDisplayMetrics()));
            setContentView(R.layout.desk_selection);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(100, 100);
            mMoveLayout = (ViewGroup) findViewById(R.id.move);
            c2 = findViewById(R.id.c2); c3 = findViewById(R.id.c3); c4 = findViewById(R.id.c4); c5 = findViewById(R.id.c6);
            c7=  findViewById(R.id.c7); c8 = findViewById(R.id.c9); c9 = findViewById(R.id.c9); h2 = findViewById(R.id.h2);
            s2 = findViewById(R.id.s2); s3 = findViewById(R.id.s3); d3 = findViewById(R.id.d3); h3 = findViewById(R.id.h3);
            d3 = findViewById(R.id.d3); arrowLeft = findViewById(R.id.arrowLeft); arrowRight = findViewById(R.id.arrowRight);
            d4 = findViewById(R.id.d4); h4 = findViewById(R.id.h4);
            cards = new ArrayList<>(Arrays.asList(c3, d3, h3, s3, c4, d4, h4, s4, c5, d5, h5, s5, c6, d6, h6, s6,
                    c7, d7, h7, s7, c8, d8, h8, s8, c9, d9, h9, s9, c10, d10, h10, s10, cj, dj, hj, sj, cq, dq, hq, sq, ck, dk, hk, sk, ca, da, ha, sa, c2, h2, s2));
            arrowRight.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    
                    Thread t0, t1, t2, t3, t_1, t_2, t_3;
                    Display display = getWindowManager().getDefaultDisplay();
                    RelativeLayout.LayoutParams lParams1 = new RelativeLayout.LayoutParams(110, 200),
                    lParams_1 = new RelativeLayout.LayoutParams(110, 200),
                    lParams2 = new RelativeLayout.LayoutParams(110, 200),
                    lParams_2 = new RelativeLayout.LayoutParams(110, 200),
                            lParams3 = new RelativeLayout.LayoutParams(110, 200),
                            lParams_3 = new RelativeLayout.LayoutParams(110, 200);
                    RelativeLayout.LayoutParams lParams0 = (RelativeLayout.LayoutParams) cards.get(counter).getLayoutParams();
                    
                    int cnt = 0;
                    for(int i = counter - 3; cnt < 7;i++)
                    {
                        if (i == 50 ) i = 0;
                        else if (i == -2) i = 50;
                        else if ( i < 0 ) i = 51 + i;
                        switch (cnt)
                        {
                            case 0:
                                lParams_3 = (RelativeLayout.LayoutParams) cards.get(i % cards.size()).getLayoutParams();
                                t_3 = new Thread (new ThreadRight(lParams_3, cards.get(i % cards.size())));
                                t_3.start();
    
                                break;
    
                            case 1:
                                lParams_2 = (RelativeLayout.LayoutParams) cards.get(i % cards.size()).getLayoutParams();
                                t_2 = new Thread (new ThreadRight(lParams_2, cards.get(i % cards.size())));
                                
                                t_2.start();
    
                                break;
                            case 2:
                                lParams_1 = (RelativeLayout.LayoutParams) cards.get(i % cards.size()).getLayoutParams();
                                t_1 = new Thread (new ThreadRight(lParams_1, cards.get(i % cards.size())));
                                Log.d("Пока", "С первым в порядке");
                                t_1.start();
                                break;
                            case 3:
                                lParams0 = (RelativeLayout.LayoutParams) cards.get(i % cards.size()).getLayoutParams();
                                t0 = new Thread (new ThreadRight(lParams0, cards.get(i % cards.size())));
                                t0.start();
                                break;
                            case 4:
                                lParams1 = (RelativeLayout.LayoutParams) cards.get(i % cards.size()).getLayoutParams();
                                t1 = new Thread (new ThreadRight(lParams1, cards.get(i % cards.size())));
                                t1.start();
                                break;
                            case 5:
                                lParams2 = (RelativeLayout.LayoutParams) cards.get(i % cards.size()).getLayoutParams();
                                t2 = new Thread (new ThreadRight(lParams2, cards.get(i % cards.size())));
                                t2.start();
                                break;
                            case 6:
                                lParams3 = (RelativeLayout.LayoutParams) cards.get(i % cards.size()).getLayoutParams();
                                t3 = new Thread (new ThreadRight(lParams3, cards.get(i % cards.size())));
                                t3.start();
                                if (counter == 50) counter = 0;
                                else counter++;
                        }
                        cnt++;
                        Log.d("", "673 = " + (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 673, getResources().getDisplayMetrics())  ) ;
                    }
    
                }
            });
        }
        public void update(ImageView iv, RelativeLayout.LayoutParams params)
        {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run () {
    
                        iv.setLayoutParams(params);
    
                    }
                });
        }
        class ThreadRight implements Runnable
        {
            RelativeLayout.LayoutParams params;
            ImageView iv;
            ThreadRight( RelativeLayout.LayoutParams params, ImageView iv)
            {
                this.iv = iv;
                this.params = params;
                Log.d("", "" + iv + ".leftMargin = " + (int) TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP, params.leftMargin, getResources().getDisplayMetrics())  + ", " + iv + ".rightMargin = " + (int) TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP, params.rightMargin, getResources().getDisplayMetrics())) ;
    
            }
            public void run() {
                Display display = getWindowManager().getDefaultDisplay();
                Point p = new Point();
                display.getSize(p);
                if ((int) TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP, params.leftMargin, getResources().getDisplayMetrics()) == 0 && (int) TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_DIP, params.rightMargin, getResources().getDisplayMetrics()) == 1806) {
                    while ((int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, params.leftMargin, getResources().getDisplayMetrics()) < (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, p.x, getResources().getDisplayMetrics()) * 2) {
                        params.leftMargin += (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
                        params.rightMargin += (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e) {
                        }
                        
                        
                        
                        update(iv, params);
                    }
                   
    
    
                }
    
                else
                {
                    int originParams = (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, params.leftMargin, getResources().getDisplayMetrics());
    
                    int cnt = 0;
    
                    while( originParams + (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, 111, getResources().getDisplayMetrics()) > (int) TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, params.leftMargin, getResources().getDisplayMetrics()))
                    {
                        params.leftMargin += (int) TypedValue.applyDimension(
                                TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
                        params.rightMargin -= (int) TypedValue.applyDimension(
                                TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics());
    
                        try {
                            Thread.sleep(1);
                        } catch (InterruptedException e){}
                        update(iv, params);
                        if (cnt % 10 == 0 ) Log.d("", "" + (int) TypedValue.applyDimension(
                                TypedValue.COMPLEX_UNIT_DIP, params.leftMargin, getResources().getDisplayMetrics()));
                        cnt++;
                    }
                }
    
            }
        }
    }


Код xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/move"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:clickable="true"
    android:focusable="true"
    android:orientation="horizontal"
    >

    <ImageView
        android:id="@+id/arrowLeft"
        android:layout_width="42dp"
        android:layout_height="59dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="1dp"
        android:layout_marginTop="137dp"
        android:contentDescription="@string/arrow1"
        app:srcCompat="@drawable/arrow" />

    <ImageView
        android:id="@+id/arrowRight"
        android:layout_width="42dp"
        android:layout_height="35dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="757dp"
        android:layout_marginTop="139dp"
        android:layout_marginEnd="8dp"
        android:contentDescription="@string/arrow2"
        android:rotation="180"
        app:srcCompat="@drawable/arrow" />

    <ImageView
        android:id="@+id/d6"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="160dp"
        android:layout_marginTop="15dp"
        android:contentDescription="@string/b6"
        android:visibility="invisible"
        app:srcCompat="@drawable/d6" />

    <ImageView
        android:id="@+id/c8"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_centerVertical="true"
        android:layout_marginStart="671dp"
        android:layout_marginTop="60dp"
        android:contentDescription="@string/c8"
        android:visibility="invisible"
        app:srcCompat="@drawable/c8" />

    <ImageView
        android:id="@+id/c9"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="614dp"
        android:layout_marginTop="64dp"
        android:contentDescription="@string/c8"
        android:visibility="invisible"
        app:srcCompat="@drawable/c9" />

    <ImageView
        android:id="@+id/jd"
        android:layout_width="32dp"
        android:layout_height="51dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="501dp"
        android:layout_marginBottom="327dp"
        android:contentDescription="@string/jb"
        android:visibility="invisible"
        app:srcCompat="@drawable/dj" />

    <ImageView
        android:id="@+id/c2"
        android:layout_width="94dp"
        android:layout_height="122dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="231dp"
        android:layout_marginTop="219dp"
        android:layout_marginEnd="489dp"
        android:layout_marginBottom="52dp"
        android:contentDescription="@string/c2"
        app:srcCompat="@drawable/c2" />

    <ImageView
        android:id="@+id/c3"
        android:layout_width="94dp"
        android:layout_height="122dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="338dp"
        android:layout_marginTop="219dp"
        android:layout_marginEnd="374dp"
        android:layout_marginBottom="54dp"
        android:contentDescription="@string/c3"
        app:srcCompat="@drawable/c3" />

    <ImageView
        android:id="@+id/c6"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="54dp"
        android:layout_marginEnd="203dp"
        android:contentDescription="@string/c6"
        android:visibility="invisible"
        app:srcCompat="@drawable/c6" />

    <ImageView
        android:id="@+id/c5"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="159dp"
        android:layout_marginEnd="58dp"
        android:contentDescription="@string/c5"
        android:visibility="invisible"
        app:srcCompat="@drawable/c5" />

    <ImageView
        android:id="@+id/c7"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="38dp"
        android:layout_marginEnd="251dp"
        android:contentDescription="@string/c7"
        android:visibility="invisible"
        app:srcCompat="@drawable/c7" />

    <ImageView
        android:id="@+id/d2"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="176dp"
        android:layout_marginTop="157dp"
        android:contentDescription="@string/d2"
        android:visibility="invisible"
        app:srcCompat="@drawable/d2" />

    <ImageView
        android:id="@+id/h2"
        android:layout_width="94dp"
        android:layout_height="122dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="117dp"
        android:layout_marginTop="219dp"
        android:layout_marginEnd="603dp"
        android:layout_marginBottom="52dp"

        android:contentDescription="@string/s2"
        app:srcCompat="@drawable/h2" />

    <ImageView
        android:id="@+id/s2"
        android:layout_width="94dp"
        android:layout_height="122dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="6dp"
        android:layout_marginTop="219dp"
        android:layout_marginEnd="715dp"
        android:layout_marginBottom="52dp"
        android:contentDescription="@string/s2"
        app:srcCompat="@drawable/s2" />

    <ImageView
        android:id="@+id/d3"
        android:layout_width="94dp"
        android:layout_height="122dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="450dp"

        android:layout_marginTop="219dp"
        android:layout_marginEnd="262dp"
        android:layout_marginBottom="52dp"
        app:srcCompat="@drawable/d3" />

    <ImageView
        android:id="@+id/h3"
        android:layout_width="94dp"
        android:layout_height="122dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="563dp"
        android:layout_marginTop="219dp"
        android:layout_marginEnd="150dp"
        android:layout_marginBottom="52dp"
        app:srcCompat="@drawable/h3" />

    <ImageView
        android:id="@+id/s3"
        android:layout_width="94dp"
        android:layout_height="122dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="673dp"
        android:layout_marginTop="219dp"
        android:layout_marginEnd="38dp"
        android:layout_marginBottom="52dp"
        app:srcCompat="@drawable/s3" />

    <ImageView
        android:id="@+id/c4"
        android:layout_width="32dp"
        android:layout_height="50sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="157dp"
        android:layout_marginEnd="239dp"
        android:visibility="invisible"
        app:srcCompat="@drawable/c4" />

    <ImageView
        android:id="@+id/d4"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="156dp"
        android:layout_marginEnd="192dp"
        android:visibility="invisible"
        app:srcCompat="@drawable/d4" />

    <ImageView
        android:id="@+id/h4"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="159dp"
        android:layout_marginEnd="148dp"
        android:visibility="invisible"
        app:srcCompat="@drawable/h4" />

    <ImageView
        android:id="@+id/s4"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="671dp"
        android:layout_marginTop="160dp"
        android:visibility="invisible"
        app:srcCompat="@drawable/s4" />

    <ImageView
        android:id="@+id/ha"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="97dp"
        android:layout_marginTop="157dp"
        android:visibility="invisible"
        app:srcCompat="@drawable/ha" />

    <ImageView
        android:id="@+id/sa"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="135dp"
        android:layout_marginTop="156dp"
        android:visibility="invisible"
        app:srcCompat="@drawable/sa" />

    <ImageView
        android:id="@+id/ca"
        android:layout_width="32dp"
        android:layout_height="50dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="54dp"
        android:layout_marginTop="157dp"
        android:visibility="invisible"
        app:srcCompat="@drawable/ca" />

</RelativeLayout>

Ответы (0 шт):