Friday, June 25, 2010

Creating animated SplashScreen activity

1:54 AM by biplob · 0 comments

>> here is sample code to create a splash screen .

//  SplashScreen  activity
_____________________________________
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.BounceInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class SplashScreen extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 3000;
    AnimationSet rootSet;
   
 
    GraphicsView graphics;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
       
        setContentView(R.layout.splash);
  
        graphics=(GraphicsView)findViewById(R.id.graphics);
       
        rootSet=getAnimation();
    
        // thread for displaying the SplashScreen
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                   graphics.startAnimation(rootSet);
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(100);
                        if(_active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    _active=false;
                   
                   
                    finish();
                
                    startActivity(new Intent("myactivity"));// next activity .......
                
                   
                }
            }
        };
        splashTread.start();
    }
   
   
    private AnimationSet getAnimation() {
        rootSet = new AnimationSet(true);
        rootSet.setInterpolator(new BounceInterpolator());

     
   
   
        // Create and add first child, a motion animation.
        TranslateAnimation trans1 = new TranslateAnimation(0, 0, -400, 0);
        trans1.setStartOffset(0);
        trans1.setDuration(800);
        trans1.setFillAfter(true);

        rootSet.addAnimation(trans1);

        ScaleAnimation scale = new ScaleAnimation(0,1, 0,
                1, // From x, to x, from y, to y
                ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
                ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
       
        scale.setDuration(800);
        scale.setFillAfter(true);

        // Add rotate and size animations to a new set,
        // then add the set to the root set.
        AnimationSet childSet = new AnimationSet(true);

        // childSet.addAnimation(rotate);
        childSet.addAnimation(scale);

       
        childSet.setInterpolator(new BounceInterpolator());
        rootSet.addAnimation(childSet);
       
        Animation outtoRight = new TranslateAnimation(
                  Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,  +1.0f,
                  Animation.RELATIVE_TO_PARENT,  0.0f, Animation.RELATIVE_TO_PARENT,   0.0f
                );
                outtoRight.setStartOffset(3000);
                outtoRight.setDuration(400);
                outtoRight.setInterpolator(new AccelerateInterpolator());
           
                rootSet.addAnimation(outtoRight);
       
        return rootSet;
    }
  
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            _active = false;
        }
        return true;
    }
}


// splash.xml
______________________________________________________________________________



    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

   
   
 
   
    
      android:id="@+id/graphics"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
    
       />
   


 

// GraphicsView class

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.graphics.Path.Direction;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;


public class GraphicsView extends View {
  
  
 
          
  
   private Path circle;
   private Paint cPaint;
 
   private Paint mPaint=null;
   int x=180;
   int y=150;
   Bitmap p;
    Display display;

private Paint cenPaint;
  
   public GraphicsView(Context context) {
        super(context);
        display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        init();
    }



    public GraphicsView(Context context, AttributeSet attrs) {
        super(context, attrs);
        display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        init();
    }
  
 
    private void init() {
   
          x=display.getWidth()/2;
          y=display.getHeight()/2;
         
       
          int color = Color.BLUE; // solid blue
          p=BitmapFactory.decodeResource(getResources(),R.drawable.levelthreeflamingshot);
         
      
     
         
         
          color = getResources().getColor(R.color.mycolor);
         

         
          circle = new Path();
       
          circle.addCircle(x, y, 50, Direction.CW);
          circle.addCircle(x, y, 70, Direction.CW);
          circle.addCircle(x, y, 90, Direction.CW);
          circle.addCircle(x, y, 110, Direction.CW);
          circle.addCircle(x, y, 130, Direction.CW);
          circle.addCircle(x, y, 150, Direction.CW);
          circle.addCircle(x, y, 170, Direction.CW);
         

          cPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
          cPaint.setStyle(Paint.Style.STROKE);
          cPaint.setStrokeWidth(3);
          cPaint.setColor(Color.LTGRAY);
         
          mPaint = new Paint();
          mPaint.setTextSize(20);
          mPaint.setTypeface(Typeface.SANS_SERIF);
          mPaint.setColor(Color.RED);
        
         
   
      

          // setBackgroundColor(Color.WHITE);
         
          setBackgroundResource(R.drawable.background);
    }   // Color examples
     
   
     
     

  

  
   @Override
   protected void onDraw(Canvas canvas) {
      // Drawing commands go here
     
     
      canvas.drawPath(circle, cPaint);
     
     
    canvas.drawText(" Welcome ", x-50, y-50,mPaint);
   
    canvas.drawBitmap(p,x-(p.getWidth()/2),y-(p.getHeight()/2),null );
    canvas.drawText(" My Game ", x-50, y+80,mPaint);
     
     
     
   }
  
 
}
 

 
Advertisement

comments

0 Responses to "Creating animated SplashScreen activity"

Post a Comment