Android Studio ImageView 有时不会留在 FrameLayout 中

Android Studio ImageView does not stay in FrameLayout sometimes

我的框架布局大约是屏幕尺寸的 60%。 在那个 Framelayout 里面我有一个每秒随机改变位置的 ImageView。

除了这个问题外,一切正常: ImageView 有时会消失,因为它会将其位置更改为超出屏幕尺寸的位置。

有什么方法可以让 ImageView 在更改坐标时保持在这个确切的 FrameLayout 内,这样它就不会出现在我的 FrameLayout 之外的某个地方?

位置更改代码:

    private void mueckePopUp() {
        final DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        final Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Random R = new Random();
                        final float dx = R.nextFloat() * displaymetrics.widthPixels;
                        final float dy = R.nextFloat() * displaymetrics.heightPixels;
                        iv_muecke.animate()
                                .x(dx)
                                .y(dy)
                                .setDuration(0)
                                .start();
                    }
                });
            }
        }, 0, 1000);

    }

XML Framelayout 和 ImageView 的代码:

    <FrameLayout
            android:layout_width="0dp"
            android:layout_height="430dp"
            tools:ignore="MissingConstraints"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/tv_points"
            android:layout_marginTop="8dp"
            android:id="@+id/framelayout"
            app:layout_constraintHorizontal_bias="0.0">

        <ImageView
                android:id="@+id/iv_muecke"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:maxHeight="100dp"
                android:minHeight="100dp"
                android:maxWidth="125dp"
                android:minWidth="125dp"
                android:adjustViewBounds="true"
                app:srcCompat="@drawable/muecke"
                android:contentDescription="@string/todo"/>

    </FrameLayout>

您必须获取框架布局的高度和宽度而不是屏幕

        //get image size
        ViewTreeObserver vti = iv_muecke.getViewTreeObserver();
        vti.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                iv_muecke.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                imWidth  = iv_muecke.getMeasuredWidth();
                imHeight = iv_muecke.getMeasuredHeight();

            }
        });
        //get FrameLayout size
        ViewTreeObserver vto = layout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                width  = layout.getMeasuredWidth();
                height = layout.getMeasuredHeight();

            }
        });

然后你要检查你的图片是否超出了FrameLayout。设置在FrameLayout

的边缘
timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Random R = new Random();
                        float dx = R.nextFloat() * width;
                        float dy = R.nextFloat() * height;
                        if (dx > (width - imWidth))
                            dx = width - imWidth;
                        if (dy > (height - imHeight))
                            dy = height - imHeight;
                        iv_muecke.animate()
                                .x(dx)
                                .y(dy)
                                .setDuration(0)
                                .start();
                    }
                });
            }
        }, 0, 1000);