【问题标题】:Runtime imageview is not showing in Launcher activity运行时图像视图未显示在启动器活动中
【发布时间】:2018-04-14 17:52:25
【问题描述】:

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

提前感谢您的帮助。

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

【问题讨论】:

  • 使用背景彻底的主题或在xml类中添加imageview

标签: java android image oncreate launch-screen


【解决方案1】:

试试这个,在代码中用 cmets 解释每一行:

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


}

【讨论】:

    【解决方案2】:

    是的,这是因为您没有针对不同分辨率的设备管理不同图像大小的 dpi。

    如果您在复制时在可绘制文件夹中只有一张适用于所有设备分辨率的图像(如图所示),请将该图像添加为 nodpi。因此,如果没有特定分辨率的图像,它将自动从 drawable 的 nodpi 中获取图像。

    【讨论】:

      猜你喜欢
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2011-09-12
      • 2016-06-25
      • 1970-01-01
      相关资源
      最近更新 更多