Android realizes flash screen effect

  • 2021-09-12 02:24:05
  • OfStack

When you log in to 1 page, you can usually see the effect of "1 flash" and enter the page. Let's take a look at how to achieve this effect

First of all, in the layout (which is no different from usual), the underlined part is the progress bar:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/bg" >
 
  <ImageView
    android:id="@+id/welcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="45dp"
    android:src="@drawable/welcome" />
 
  <ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/welcome"
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="70dp"
    />
 
  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/progressBar"
    android:layout_centerHorizontal="true"
    android:padding="@dimen/padding_medium"
    android:text="@string/welcome"
    android:textAppearance="?android:attr/textAppearanceMedium"
    tools:context=".MainActivity" />
 
</RelativeLayout>

Defined in String:


<resources>
 
  <string name="app_name">ShanP01</string>
  <string name="welcome"> Welcome to join !\n1 Learn happily! </string>// ( \n ) Implement line wrapping 
  <string name="menu_settings">Settings</string>
  <string name="title_activity_main">MainActivity</string>
  <string name="title_study"> Learning </string>
  <string name="title_search"> Search </string>
  <string name="title_game"> Games </string>
  <string name="title_save"> Save </string>
  <string name="title_help"> Help </string>
  <string name="title_activity_welcome">WelcomeActivity</string>
 
</resources>

If you want to run the project without displaying the title bar, just add a sentence to Activity where you want to hide the title bar (in the AndroidManifest. xml file):


android:theme="@android:style/Theme.NoTitleBar" 

The main implementation method:


public class WelcomeActivity extends Activity {
 private ImageView welcomeImage;
    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    welcomeImage=(ImageView) this.findViewById(R.id.welcome);
    AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f, 1.0f);//// Definition 1 Objects with fade-in effect 
    alphaAnimation.setDuration(3000);// Define the flash time (milliseconds) 
    welcomeImage.startAnimation(alphaAnimation);
    alphaAnimation.setAnimationListener(new AnimationListener() {
  
  @Override
  public void onAnimationStart(Animation animation) {
  
  
  }
  
  @Override
  public void onAnimationRepeat(Animation animation) {
  
  
  }
  
  @Override
  public void onAnimationEnd(Animation animation) {
  Intent intent=new Intent();
  intent.setClass(WelcomeActivity.this, MainActivity.class);// Define where the splash effect comes from 1 Where does the interface jump to 1 Page 
  startActivity(intent);
  finish();
  
  }
 });
  }
 
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_welcome, menu);
    return true;
  }
  
}

In fact, there are more than one kind of splash screen effect, but this is one that I think is simple. There is another one:


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_welcome); 
    welcomeImage=(ImageView) this.findViewById(R.id.welcome);   
    AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f);//// Definition 1 Objects with fade-in effect  
    welcomeImage.startAnimation(alphaAnimation); 
    new Handler().postDelayed(new Runnable() {    
      @Override 
      public void run() { 
        Intent intent=new Intent(); 
        intent.setClass(WelcomeActivity.this, MainActivity.class); 
        startActivity(intent); 
        finish(); 
      } 
    },3000);// Be careful not to miss  
  } 

Which one do you think suits you better?


Related articles: