Android Splash Screen Does Not Display
Solution 1:
When Big sized image you should downsample the image. change the inSampleSize variable value as per your need. increased value, will reduce the image resolution and vise versa
publicstatic Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensionsfinal BitmapFactory.Optionsoptions=newBitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
privatestaticintcalculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of imagefinalintheight= options.outHeight;
finalintwidth= options.outWidth;
intinSampleSize=1;
if (height > reqHeight || width > reqWidth) {
finalinthalfHeight= height / 2;
finalinthalfWidth= width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both// height and width larger than the requested height and width.while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
call above methods like below code:
decodeSampledBitmapFromResource(getResources(), R.drawable.splash, 400, 400);
just, add above methods in your common helper class and call it
Solution 2:
The problem is something i faced when creating splash screens. You start the handler in the onCreate method of the activity.
According to the android developer site:
When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it
So you are creating a handler on the main thread which is also responsible for the UI. This thread prepares your UI (which in this case seems to take a bit more than 2 seconds) and then calls the Runnable inside your handler which immediately moves to another activity thus your splash is not shown.
So what could you do? Start the timer after your view is shown!!!
This is how you can do it (Probably not the only way but good enough):
1- Set an id for the RelativeLayout in the XML:
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/background"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/splash"></RelativeLayout>
2- In your onCreate method do like this:
publicclassSplashextendsAppCompatActivity {
@OverrideprotectedvoidonCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Viewbackground= findViewById(R.id.background); // or use Butterknife or DataBinding for ease of usefinal Handler handler=newHandler();
background.post(newRunnable() {
@Overridepublicvoidrun() {
handler.postDelayed(newRunnable() {
@Overridepublicvoidrun() {
Intent intent=newIntent(Splash.this,MainActivity.class);
startActivity(intent);
Splash.this.finish();
}
},2000);
}
});
}
}
This way you post a message to the view message queue and after the view is put on the screen then it registers a handler as you wish. Hope it helps.
Solution 3:
Here, I tried to solve your problem
splash_screen_activity.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="@drawable/splashscreen"></LinearLayout>
SplashScreenActivity.java
publicclassSplashScreenActivityextendsAppCompatActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen_activity);
Threadbackground=newThread() {
publicvoidrun() {
try {
// Thread will sleep for 5 seconds
sleep(5*1000);
// After 5 seconds redirect to another intent
Intent i=newIntent(getBaseContext(),MainActivity.class);
startActivity(i);
//Remove activity
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.pantrykart"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=".SplashScreenActivity"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".MainActivity"></activity></application></manifest>
Post a Comment for "Android Splash Screen Does Not Display"