首页 > 解决方案 > 如何在启动画面活动和其他活动之间居中相同的资产/可绘制对象?

问题描述

我已经创建了没有任何布局 xml 文件的启动画面,但这是通过将应用程序徽标居中的矢量可绘制完成的,如下所示:

 <item name="android:windowBackground">@drawable/splash_background</item>

但我也想在下一个后续屏幕中显示相同的应用程序徽标。对于 eq 在我的情况下,让我们说主活动屏幕,其中我有一个布局 xml,我将图标与约束布局居中。

activity_main.xml

    <!-- By adding android:layout_marginTop="26dp" to the ImageView kind of works but not sure why the magic number works
     Not sure where to get the same number for some other devices-->

    <ImageView
        android:id="@+id/imageViewLogo"
        android:layout_width="@dimen/splash_icon"
        android:layout_height="@dimen/splash_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_invest_logo" />


</android.support.constraint.ConstraintLayout>

但是我注意到,当 Splash 和 Main Activity 之间发生相同的资产徽标交换时,我可以看到徽标的跳跃行为。

感谢你的帮助。下面是整个源代码的 github。

https://github.com/nksaroj/InvestApp

您可以在此处看到带有绿色徽标的跳跃问题

在此处输入图像描述

标签: androidandroid-layoutsplash-screen

解决方案


getWindow().getDecorView().getRootView().getViewTreeObserver().addOnGlobalLayoutListener(
        new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // Layout has happened here.

                float statusBarHeight = getStatusBarHeight() * 1f;

                //This is tricks where Samsung and some other devices don't consider the height in splash screen so that you need to adjust the height manually 
                View navigationBarBackground = findViewById(android.R.id.navigationBarBackground);

                if (navigationBarBackground == null) {
                    statusBarHeight = statusBarHeight * -1f;
                }

                imageViewIconY = imageViewReadyIcon.getY() + (statusBarHeight / 2);

                imageViewIconY.setY(imageViewIconY);



                // Don't forget to remove your listener when you are done with it.
                getWindow().getDecorView().getRootView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

推荐阅读