如何在“启动画面活动”和其他活动之间使相同的资产/抽屉居中?

时间:2018-07-07 07:22:34

标签: android android-layout splash-screen

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

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

但是我也想在下一个后续屏幕中显示相同的应用程序徽标。对于e.q.就我而言,让我们说一下“主要活动”屏幕,其中有一个布局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

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

enter image description here

1 个答案:

答案 0 :(得分:0)

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