android studio imageview有时不停留在framelayout中

js4nwp54  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(315)

我有一个框架布局,大约是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);

    }

framelayout和imageview的xml代码:

<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>
8nuwlpux

8nuwlpux1#

你必须得到框架布局的高度和宽度,而不是屏幕

//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);

相关问题