java - 运行时 ImageView 未显示在启动器 Activity 中

标签 java android image oncreate launch-screen

使用下面的代码,我的背景图像没有显示在我的启动屏幕的 Activity 中。 我还有一个 sleep 三秒的线程,但我只看到白色屏幕作为背景。延迟后,我重定向到另一个 Activity (在 OnStart 函数中)。 如果没有重定向,我会将我的图像视为背景。 在重定向到第二个 Activity 之前如何查看启动屏幕图像?

预先感谢您的帮助。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    mAuth = FirebaseAuth.getInstance();

            RequestOptions options = new RequestOptions();
            options.centerCrop();

            RelativeLayout relativeLayout = new RelativeLayout(mContext);
            RelativeLayout.LayoutParams relLayoutParam = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setBackgroundResource(R.drawable.launch_screen_image);
            relativeLayout.addView(imageView, relLayoutParam);

            setContentView(relativeLayout, relLayoutParam);
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

}

@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser != null && currentUser.getUid() != null
            && currentUser.getUid().equals(mPrefs.getString("UID", "DEFAULT"))) {
        Intent i = new Intent(mContext, SocialPage.class);
        startActivity(i);
    } else {
        Intent i = new Intent(mContext, Login.class);
        startActivity(i);
    }
}

最佳答案

试试这个,用代码中的注释解释每一行:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        //Other stuff
        //...
            //Remove this line ==> imageView.setBackgroundResource(R.drawable.launch_screen_image);
            relativeLayout.addView(imageView, relLayoutParam);

            setContentView(relativeLayout, relLayoutParam);

            //set image resource after setContentView
            imageView.setImageResource(R.drawable.launch_screen_image);

            //Use handler instead of Thread.sleep()
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //it calls continue() after 3 seconds of delay
                    continue();
                }
            },3000);


}

并且continue()方法包括这些:

private void continue(){

    // Check if user is signed in (non-null) and update UI accordingly.
    SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser != null && currentUser.getUid() != null
            && currentUser.getUid().equals(mPrefs.getString("UID", "DEFAULT"))) {
        Intent i = new Intent(mContext, SocialPage.class);
        startActivity(i);
    } else {
        Intent i = new Intent(mContext, Login.class);
        startActivity(i);
    }


}

关于java - 运行时 ImageView 未显示在启动器 Activity 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49834461/

相关文章:

html - 图片链接到另一张图片(图片库)

c# - 在 .NET 的 System.Drawing 命名空间中看不到图像类型

javascript - 如何使用 jQuery 将 jpeg 显示为 IMG 标签的源属性,由 PHP 文件返回

java - 无法设置 Jenkins 的 JAVA_HOME 路径

java - Android 对服务器进行 PATCH/PUT

java - 如何在 Android 中发送点击事件

android - 将数据从类传递到 MainActivity

android - 如何在 Eclipse 中添加 Android 支持 v7 库?

java - Jersey/* servlet 映射导致静态资源出现 404 错误

java - 用任何符号分割字符串