android - 如何在启动画面活动和其他活动之间居中相同的资产/可绘制对象?
问题描述
我已经创建了没有任何布局 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
您可以在此处看到带有绿色徽标的跳跃问题
解决方案
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);
}
});
推荐阅读
- bash - Asterisk 创建基于呼叫来源发送电子邮件的脚本
- haskell - 如何使用“optparse-applicative”创建和区分全局选项?
- javascript - 立即显示对 HTML5 画布的更改
- php - 在动态 php 样式表中添加 PHP IF 语句
- r - Emacs ESS 在 %>% 后缩进
- time - 天作为时间序列?
- javascript - 用主题反应导航栏处理程序
- html - Angular 形式的 ngModel
- python - 使用 pyppeteer 与 asyncio 关联来抓取内容
- kubernetes - 我可以运行什么命令来获取最旧的 pod 的名称?