简体   繁体   中英

Android Studio ImageView does not stay in FrameLayout sometimes

I have a Framelayout which is about 60% of the screen size. Inside that Framelayout I have an ImageView which randomly changes positions every second.

Everything is working perfectly fine except this one problem: The ImageView sometimes just disappears because it changes its position to somewhere that is past the screen size.

Is there any way I can make the ImageView stay inside this exact FrameLayout while changing coordinates so it does not appear somewhere outside of my FrameLayout?

Code for the position change:

    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 code for the Framelayout and 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>

You have to get height and width of Frame layout instead of screen

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

            }
        });

Then you have to check whether your image is out of FrameLayout. Set it at the edge of 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM