使用下面的代码,我的启动画面活动中没有显示我的背景图片。 我还有一个睡眠时间为3秒的线程,但我看到只有白色屏幕作为背景。延迟之后,我重定向到另一个活动(在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);
}
}
答案 0 :(得分:1)
试试这个,解释代码中带注释的每一行:
@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);
}
}
答案 1 :(得分:0)