Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 2.62 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Splash Screen Alpha Animation in Android
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.animation.AccelerateInterpolator;
  6. import android.view.animation.AlphaAnimation;
  7. import android.view.animation.Animation;
  8. import android.widget.ImageView;
  9. public class Splash extends Activity
  10. {
  11.     ImageView img;
  12.     Thread timer;
  13.     public void onCreate(Bundle savedInstanceState)
  14.     {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.splash);
  17.         img = (ImageView) findViewById (R.id.imgSplash);
  18.         img.startAnimation(FadeIn(1000));
  19.         try
  20.         {
  21.             Thread.sleep(1000);
  22.         }
  23.         catch (InterruptedException e1)
  24.         {
  25.             e1.printStackTrace();
  26.         }
  27.  
  28.         img.startAnimation(FadeOut(1000));
  29.  
  30.         try
  31.         {
  32.             Thread.sleep(1000);
  33.         }
  34.         catch (InterruptedException e1)
  35.         {
  36.             e1.printStackTrace();
  37.         }
  38.         timer.start();
  39.         Intent intent = new Intent();
  40.         intent.setClass(Splash.this,MainScreen.class);
  41.         startActivity(intent);
  42.     }
  43.     public void onPause()
  44.     {
  45.         super.onPause();
  46.         finish();
  47.     }
  48.     private Animation FadeIn(int t)
  49.     {
  50.         Animation fade;
  51.         fade = new AlphaAnimation(0.0f,1.0f);
  52.         fade.setDuration(t);
  53.         fade.setInterpolator(new AccelerateInterpolator());
  54.         return fade;
  55.     }
  56.     private Animation FadeOut(int t)
  57.     {
  58.         Animation fade;
  59.         fade = new AlphaAnimation(1.0f,0.0f);
  60.         fade.setDuration(t);
  61.         fade.setInterpolator(new AccelerateInterpolator());
  62.         return fade;
  63.     }
  64. }
  65.        
  66. private final Handler handler = new Handler();
  67.  
  68. private final Runnable startActivityRunnable = new Runnable() {
  69.  
  70.     @Override
  71.     public void run() {
  72.         Intent intent = new Intent();
  73.             intent.setClass(Splash.this,MainScreen.class);
  74.         startActivity(intent);
  75.     }
  76. };
  77.  
  78. public void onCreate(Bundle savedInstanceState)
  79. {
  80.     super.onCreate(savedInstanceState);
  81.     setContentView(R.layout.splash);
  82.     img = (ImageView) findViewById (R.id.imgSplash);
  83.  
  84.     setContentView(img);
  85. }
  86.  
  87. @Override
  88. protected void onResume() {
  89.     super.onResume();
  90.  
  91.     AnimationSet set = new AnimationSet(true);
  92.  
  93.     Animation fadeIn = FadeIn(1000);
  94.     fadeIn.setStartOffset(0);
  95.     set.addAnimation(fadeIn);
  96.  
  97.     Animation fadeOut = FadeOut(1000);
  98.     fadeOut.setStartOffset(2000);
  99.     set.addAnimation(fadeOut);
  100.  
  101.     img.startAnimation(set);
  102.  
  103.     handler.postDelayed(startActivityRunnable, 3000);
  104. }
  105.  
  106. public void onPause()
  107. {
  108.     super.onPause();
  109.     handler.removeCallbacks(startActivityRunnable);
  110. }