问题
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 is put into res/drawable folder and shows fine from the splash activity. What is missing?
回答1:
xml for splash screen.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_splash_screen" />
</RelativeLayout>
In MainActivity
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 1000;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(Splash.this,
MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
回答2:
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>
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>
回答3:
Add this line in your onCreate(Bundle savedInstance) method:
mImageView = new ImageView(this);
mImageView.setScaleType(ScaleType.FIT_XY);
mImageView.setImageResource(R.drawable.splash_bg);
LayoutParams imageViewLayoutParams
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mImageView.setLayoutParams(imageViewLayoutParams);
来源:https://stackoverflow.com/questions/36915882/imageview-as-a-splash-screen-not-working