Android opening animation class complete implementation code

  • 2020-06-03 08:15:32
  • OfStack

The example described in this article is the opening animation class in android, which has been packaged for use by friends who are developing android. In this class, you can: set the image resources for the opening animation, return the next Activity to start, display the opening animation, perform time-consuming actions, create the startup interface Layout, and set the orientation of the screen. The default is portrait, opening animation image resource class. Encapsulates images, playback time, transparency at the beginning, and so on.

The specific implementation code is as follows:


package com.lurencun.cfuture09.androidkit.ui;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import com.lurencun.cfuture09.androidkit.thread.HandlerFactory;
public abstract class IntroActivity extends Activity {
 private static final String FLAG_RESOURCE = "FLAG_RESOURCE";
 /**
 *  A sign that the background task is complete. 
 */
 private static final byte BACKGROUND_FINISH = 0x01;
 /**
 *  A sign that a front desk task has been completed. 
 */
 private static final byte FRONTGROUND_FINISH = 0x10;
 /**
 *  Means to play an opening animation. 
 */
 private static final int INTRO_PLAY = 0;
 /**
 *  Resources for the opening animation. 
 */
 private List<IntroImgResource> mResources;
 /**
 *  Background color of the picture. The default is white. 
 */
 private int mBackgroundColor = 0xFFFFFFFF;
 /**
 * UI Threads. 
 */
 private Handler mUiHandler;
 /**
 *  Used to show animation. 
 */
 private ImageView mIntroImage;
 /**
 *  Screen direction. 
 */
 private int mOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 init();
 runOnMainThread();
 this.requestWindowFeature(Window.FEATURE_NO_TITLE);
 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  WindowManager.LayoutParams.FLAG_FULLSCREEN);
 this.setRequestedOrientation(mOrientation);
 this.setContentView(createLayout());
 setIntroResources(mResources);
 startOnBackground();
 showIntro();
 }
 private void init() {
 mResources = new ArrayList<IntroImgResource>();
 mUiHandler = new UIHandler(this);
 }
 /**
 *  Set the photo resources for the opening animation. 
 *
 * @param resources
 *       Photo resources for the opening animation. 
 */
 protected abstract void setIntroResources(List<IntroImgResource> resources);
 /**
 *  Returns the 1 One to start Activity . 
 *
 * @return  Under the 1 One to start Activity . 
 */
 protected abstract Class<?> nextActivity();
 /**
 *  Show the opening animation. 
 */
 protected void showIntro() {
 int delayTime = 0;
 for (final IntroImgResource resource : mResources) {
  Message msg = new Message();
  msg.what = INTRO_PLAY;
  Bundle data = new Bundle();
  data.putSerializable(FLAG_RESOURCE, resource);
  msg.setData(data);
  mUiHandler.sendMessageDelayed(msg, delayTime);
  delayTime += resource.playerTime;
 }
 mUiHandler.sendEmptyMessageDelayed(FRONTGROUND_FINISH, delayTime);
 }
 /**
 *  Perform time-consuming operations. 
 */
 private void startOnBackground() {
 HandlerFactory.newHandlerInOtherThread("intro_bg").post(
  new Runnable() {
   @Override
   public void run() {
   runOnBackground();
   mUiHandler.sendEmptyMessage(0x1);
   }
  });
 }
 /**
 *  Create a startup interface Layout . 
 *
 * @return  Returns the interface you created Layout.
 */
 private View createLayout() {
 FrameLayout layout = new FrameLayout(this);
 ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
  LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
 layout.setLayoutParams(layoutParams);
 layout.setBackgroundColor(getBackgroundColor());
 mIntroImage = new ImageView(this);
 FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
  FrameLayout.LayoutParams.FILL_PARENT,
  FrameLayout.LayoutParams.FILL_PARENT);
 params.gravity = Gravity.CENTER;
 layout.addView(mIntroImage, params);
 return layout;
 }
 /**
 *  Get the background of the image. 
 *
 * @return
 */
 public int getBackgroundColor() {
 return mBackgroundColor;
 }
 /**
 *  Set the background of the picture. 
 *
 * @param backgroundColor
 */
 public void setBackgroundColor(int backgroundColor) {
 this.mBackgroundColor = backgroundColor;
 }
 /**
 *  Return to the screen direction. 
 *
 * @return
 */
 public int getmOrientation() {
 return mOrientation;
 }
 /**
 *  Set the direction of the screen. The default is portrait. 
 *
 * @param mOrientation
 *       Screen direction. ActivityInfo.SCREEN_ORIENTATION_PORTRAIT Or is it ActivityInfo.
 *      SCREEN_ORIENTATION_LANDSCAPE . 
 */
 public void setmOrientation(int mOrientation) {
 this.mOrientation = mOrientation;
 }
 /**
 *  Code executed in the foreground. If landscape resetting is required for the interface, please do so here setmOrientation() Methods. 
 */
 protected void runOnMainThread() {
 }
 /**
 *  Code executed in the background. This is where the more time-consuming operation takes place. 
 */
 protected void runOnBackground() {
 }
 protected static class UIHandler extends Handler {
 /**
  *  Need to wait. 
  */
 private int isWaiting = 0;
 private WeakReference<IntroActivity> activity;
 public UIHandler(IntroActivity activity) {
  this.activity = new WeakReference<IntroActivity>(activity);
 }
 public void handleMessage(android.os.Message msg) {
  if (msg.what == INTRO_PLAY) {
  IntroImgResource resource = (IntroImgResource) msg.getData()
   .getSerializable(FLAG_RESOURCE);
  AlphaAnimation animation = new AlphaAnimation(
   resource.startAlpha, 1f);
  animation.setDuration(resource.playerTime);
  IntroActivity intro = activity.get();
  if (intro != null) {
   if (resource.isExpand) {
   intro.mIntroImage.setScaleType(ScaleType.FIT_XY);
   } else {
   intro.mIntroImage.setScaleType(ScaleType.CENTER);
   }
   intro.mIntroImage.setImageResource(resource.mResId);
   intro.mIntroImage.startAnimation(animation);
  }
  return;
  }
  if (msg.what == BACKGROUND_FINISH || msg.what == FRONTGROUND_FINISH) {
  isWaiting |= msg.what;
  //  When a background or foreground task is not completed, it is not executed Activity The jump. 
  if (isWaiting == (BACKGROUND_FINISH | FRONTGROUND_FINISH)) {
   IntroActivity intro = activity.get();
   if (intro != null) {
   intro.startActivity(new Intent(intro, intro
    .nextActivity()));
   intro.finish();
   }
  }
  }
 };
 };
 /**
 *  Opening animation of the picture resources class. Encapsulates the image, playback time, and transparency at the beginning. 
 *
 * @author msdx
 *
 */
 protected class IntroImgResource implements Serializable {
 /**
  *  serialization ID . 
  */
 private static final long serialVersionUID = -2257252088641281804L;
 /**
  *  Resource images ID.
  */
 private int mResId;
 /**
  *  Play time in milliseconds. 
  */
 private int playerTime;
 /**
  *  The degree of transparency at the beginning. 0-1 In between. 
  */
 private float startAlpha;
 /**
  *  Whether the image expands. 
  */
 private boolean isExpand;
 /**
  *  The construction method of the opening animation resource. 
  *
  * @param mResId
  *       Image resource ID . 
  * @param playerTime
  *       The e playback time of an image resource in milliseconds. 
  * @param startAlpha
  *       How transparent the image resource is at the beginning. 0-255 In between. 
  */
 public IntroImgResource(int mResId, int playerTime, float startAlpha, boolean isExpand) {
  super();
  this.mResId = mResId;
  this.playerTime = playerTime;
  this.startAlpha = startAlpha;
  this.isExpand = isExpand;
 }
 /**
  *  Get resource images ID . 
  *
  * @return  Resource images ID . 
  */
 public int getmResId() {
  return mResId;
 }
 /**
  *  Set resource image ID.
  *
  * @param mResId
  *       The resource image to set ID.
  */
 public void setmResId(int mResId) {
  this.mResId = mResId;
 }
 /**
  *  Returns the playback time of the resource image. 
  *
  * @return  The playback time of the resource image. 
  */
 public int getPlayerTime() {
  return playerTime;
 }
 /**
  *  Sets the playback time of the resource image. 
  *
  * @param playerTime
  *       The playback time of the resource image. 
  */
 public void setPlayerTime(int playerTime) {
  this.playerTime = playerTime;
 }
 /**
  *  Get the initial transparency of the resource. 
  *
  * @return
  */
 public float getStartAlpha() {
  return startAlpha;
 }
 /**
  *  Sets the level of transparency at the beginning of the resource. 
  *
  * @param startAlpha
  */
 public void setStartAlpha(float startAlpha) {
  this.startAlpha = startAlpha;
 }
 /**
  *  Returns whether the image is set to expand. 
  *
  * @return
  */
 public boolean isExpand() {
  return isExpand;
 }
 /**
  *  Sets whether the image expands. 
  *
  * @param isExpand
  *       If it is true , the picture will be stretched to the full screen size for display, otherwise the original size display. 
  */
 public void setExpand(boolean isExpand) {
  this.isExpand = isExpand;
 }
 }
}

This example is equipped with detailed notes, readers can understand the function of the program code on the basis of personalized modification, create their own unique Android opening animation!


Related articles: