ImageView作为启动画面无法正常工作 [英] ImageView as a splash screen not working

查看:256
本文介绍了ImageView作为启动画面无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用程序需要一些时间来初始化,并且我想在加载屏幕出现之前显示启动图像并在之后隐藏它。我搜索了stackoverflow并找到了一些解决方案。我尝试按照本教程,解释如何实现一个在启动活动中启动的正确启动画面,但它并没有解决我的问题,因为在启动画面和加载屏幕之间仍然有几秒黑屏(它从一个单独的C ++代码线程渲染,并且在渲染开始之前必须初始化一堆东西,请不要求改变那部分,它是一个跨平台的C ++引擎)。接下来,我尝试了一个ProgressDialog 从这里获取,在主要活动的onCreate中启动它,并在C ++部分开始实际渲染时隐藏,并且除了不是启动图像之外它工作正常。但时间正是我所需要的。然后我用ImageView替换它并且它不起作用(没有显示图像)。

My android app takes some time to initialize, and I'd like to show a splash image before the loading screen appears and hide it afterwards. I searched through stackoverflow and found some solutions. I tried to follow this tutorial, that explains how to implement a proper splash screen that starts within a splash activity, but it didn't solve my problem, because there was still a several seconds black screen between the splash screen and the loading screen (which renders from a separate thread of C++ code, and has to initialize a bunch of things before render starts, please don't ask to change that part, it's a crossplatform C++ engine). Next I experimented with a ProgressDialog taken from here, started it in onCreate of the main activity and hided when C++ part starts actual rendering, and it worked fine except not being a splash image. But the timing was exactly what I need. Then I replaced it with an ImageView and it didn't work (no image is shown).

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    //...
    mImageView = new ImageView(this);
    mImageView.setScaleType(ScaleType.FIT_XY);
    mImageView.setImageResource(R.drawable.splash_bg);
    setContentView(mImageView);
}

splash_bg.png被放入res / drawable文件夹并显示正常活动。
缺少什么?

splash_bg.png is put into res/drawable folder and shows fine from the splash activity. What is missing?

推荐答案

您应该为您的启动活动制作主题,如:

You should made theme for your splash activity like:

<style name="AppTheme.Splash" parent="YOURMAIN_THEME">
        <item name="android:windowBackground">@drawable/splash_bg</item>
</style>

并在drawable目录splash_bg.xml中创建你的启动,如:

And create your splash in drawable directory splash_bg.xml like:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/splash_background"/>
    <item
        android:top="30dp">
        <bitmap
            android:gravity="top"
            android:src="@drawable/demo_logo"
            />
    </item>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/demo_emlogo"/>
    </item>
</layer-list>

这篇关于ImageView作为启动画面无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆