Android Studio popup splash in main

I am currently working on an Android application. Just started, and I was able to implement my screensaver. However, I do not like the transition between this and the main activity. I want the splash screen to fade and the main part disappear. It looks like they are mixing because I have the same background image for both. Could not find any study, but could not find the correct answers. Below I posted my code.

import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.MotionEvent; public class Splash_screen extends Activity { private Thread mSplashThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_layout); final Splash_screen sPlashScreen = this; mSplashThread = new Thread(){ @Override public void run(){ try { synchronized(this){ wait(3000); } } catch(InterruptedException ex){ } finish(); Intent intent = new Intent(); intent.setClass(sPlashScreen, MainActivity.class); startActivity(intent); } }; mSplashThread.start(); } @Override public boolean onTouchEvent(MotionEvent evt) { if(evt.getAction() == MotionEvent.ACTION_DOWN) { synchronized(mSplashThread){ mSplashThread.notifyAll(); } } return true; } } 

Class MainActivity

 import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.app.Activity; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 

Feel free to delete any classes or files that are not needed for this task. Thanks

+5
source share
1 answer

You can use two .xml files to fade out in the new Office and reduce the current activity.

fade_in.xml

 <?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" /> 

fade_out.xml

 <?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.0" android:fillAfter="true" android:duration="500" /> 

Use it in code: (inside your activity)

 Intent intent = new Intent(); intent.setClass(sPlashScreen, MainActivity.class); startActivity(intent); overridePendingTransition(R.anim.fade_in, R.anim.fade_out); 

The code above will quench the current active activity and disappear in the recently launched action.

+14
source

Source: https://habr.com/ru/post/1215046/


All Articles