android程序开启动画封装


本文及代码原创,转载请注明出处:http://maosidiaoxian.iteye.com/blog/1682616 

许多程序在开启的时候都会有一个LOGO的展示,然后才进入。参考一个师兄的写法,我自己也对这样的行为进行了封装。代码如下: 
Java代码   收藏代码
  1. /* 
  2.  * @(#)IntroActivity.java              Project:com.sinaapp.msdxblog.androidkit 
  3.  * Date:2012-9-10 
  4.  * 
  5.  * Copyright (c) 2011 CFuture09, Institute of Software,  
  6.  * Guangdong Ocean University, Zhanjiang, GuangDong, China. 
  7.  * All rights reserved. 
  8.  * 
  9.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  10.  *  you may not use this file except in compliance with the License. 
  11.  * You may obtain a copy of the License at 
  12.  * 
  13.  *     http://www.apache.org/licenses/LICENSE-2.0 
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing, software 
  16.  * distributed under the License is distributed on an "AS IS" BASIS, 
  17.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  18.  * See the License for the specific language governing permissions and 
  19.  * limitations under the License. 
  20.  */  
  21. package com.sinaapp.msdxblog.androidkit.ui;  
  22.   
  23. import java.io.Serializable;  
  24. import java.lang.ref.WeakReference;  
  25. import java.util.ArrayList;  
  26. import java.util.List;  
  27.   
  28. import android.app.Activity;  
  29. import android.content.Intent;  
  30. import android.content.pm.ActivityInfo;  
  31. import android.os.Bundle;  
  32. import android.os.Handler;  
  33. import android.os.Message;  
  34. import android.view.Gravity;  
  35. import android.view.View;  
  36. import android.view.ViewGroup;  
  37. import android.view.ViewGroup.LayoutParams;  
  38. import android.view.Window;  
  39. import android.view.WindowManager;  
  40. import android.view.animation.AlphaAnimation;  
  41. import android.widget.FrameLayout;  
  42. import android.widget.ImageView;  
  43.   
  44. import com.sinaapp.msdxblog.androidkit.thread.HandlerFactory;  
  45.   
  46. /** 
  47.  * @author Geek_Soledad (66704238@51uc.com) 
  48.  */  
  49. public abstract class IntroActivity extends Activity {  
  50.     private static final String FLAG_RESOURCE = "FLAG_RESOURCE";  
  51.     /** 
  52.      * 后台任务完成的标志。 
  53.      */  
  54.     private static final int BACKGROUND_FINISH = 0x01;  
  55.     /** 
  56.      * 前台任务完成的标志。 
  57.      */  
  58.     private static final int FRONTGROUND_FINISH = 0x10;  
  59.     /** 
  60.      * 表示要播放开场动画。 
  61.      */  
  62.     private static final int INTRO_PLAY = 0;  
  63.     /** 
  64.      * 开场动画的资源。 
  65.      */  
  66.     private List<IntroImgResource> mResources = new ArrayList<IntroImgResource>();  
  67.     /** 
  68.      * 图片背景颜色。默认为白色。 
  69.      */  
  70.     private int mBackgroundColor = 0xFFFFFFFF;  
  71.     /** 
  72.      * UI线程。 
  73.      */  
  74.     private Handler mUiHandler;  
  75.     /** 
  76.      * 用来显示动画。 
  77.      */  
  78.     private ImageView mIntroImage;  
  79.     /** 
  80.      * 屏幕方向。 
  81.      */  
  82.     private int mOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;  
  83.   
  84.     @Override  
  85.     protected void onCreate(Bundle savedInstanceState) {  
  86.         super.onCreate(savedInstanceState);  
  87.         init();  
  88.         runOnMainThread();  
  89.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);  
  90.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  91.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  92.         this.setRequestedOrientation(mOrientation);  
  93.         this.setContentView(createLayout());  
  94.         setIntroResources(mResources);  
  95.         startOnBackground();  
  96.         showIntro();  
  97.     }  
  98.   
  99.     private void init() {  
  100.         mUiHandler = new UIHandler(this);  
  101.     }  
  102.   
  103.     /** 
  104.      * 设置开场动画的图片资源。 
  105.      *  
  106.      * @param resources 
  107.      *            开场动画的图片资源。 
  108.      */  
  109.     protected abstract void setIntroResources(List<IntroImgResource> resources);  
  110.   
  111.     /** 
  112.      * 返回下一个要启动的Activity。 
  113.      *  
  114.      * @return 下一个要启动的Activity。 
  115.      */  
  116.     protected abstract Class<?> nextActivity();  
  117.   
  118.     /** 
  119.      * 显示开场动画。 
  120.      */  
  121.     protected void showIntro() {  
  122.         int delayTime = 0;  
  123.         for (final IntroImgResource resource : mResources) {  
  124.             Message msg = new Message();  
  125.             msg.what = INTRO_PLAY;  
  126.             Bundle data = new Bundle();  
  127.             data.putSerializable(FLAG_RESOURCE, resource);  
  128.             msg.setData(data);  
  129.             mUiHandler.sendMessageDelayed(msg, delayTime);  
  130.             delayTime += resource.playerTime;  
  131.         }  
  132.         mUiHandler.sendEmptyMessageDelayed(FRONTGROUND_FINISH, delayTime);  
  133.   
  134.     }  
  135.   
  136.     /** 
  137.      * 执行耗时的操作。 
  138.      */  
  139.     private void startOnBackground() {  
  140.         HandlerFactory.getNewHandlerInOtherThread("intro_bg").post(  
  141.                 new Runnable() {  
  142.                     @Override  
  143.                     public void run() {  
  144.                         runOnBackground();  
  145.                         mUiHandler.sendEmptyMessage(0x1);  
  146.                     }  
  147.                 });  
  148.     }  
  149.   
  150.     /** 
  151.      * 创建启动时的界面Layout。 
  152.      *  
  153.      * @return 返回创建的界面Layout. 
  154.      */  
  155.     private View createLayout() {  
  156.         FrameLayout layout = new FrameLayout(this);  
  157.         ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(  
  158.                 LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
  159.         layout.setLayoutParams(layoutParams);  
  160.         layout.setBackgroundColor(getBackgroundColor());  
  161.         mIntroImage = new ImageView(this);  
  162.         FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(  
  163.                 FrameLayout.LayoutParams.WRAP_CONTENT,  
  164.                 FrameLayout.LayoutParams.WRAP_CONTENT);  
  165.         params.gravity = Gravity.CENTER;  
  166.         layout.addView(mIntroImage, params);  
  167.   
  168.         return layout;  
  169.     }  
  170.   
  171.     /** 
  172.      * 获取图片背景。 
  173.      *  
  174.      * @return 
  175.      */  
  176.     public int getBackgroundColor() {  
  177.         return mBackgroundColor;  
  178.     }  
  179.   
  180.     /** 
  181.      * 设置图片背景。 
  182.      *  
  183.      * @param backgroundColor 
  184.      */  
  185.     public void setBackgroundColor(int backgroundColor) {  
  186.         this.mBackgroundColor = backgroundColor;  
  187.     }  
  188.   
  189.     /** 
  190.      * 返回屏幕方向。 
  191.      *  
  192.      * @return 
  193.      */  
  194.     public int getmOrientation() {  
  195.         return mOrientation;  
  196.     }  
  197.   
  198.     /** 
  199.      * 设置屏幕的方向。默认是竖屏。 
  200.      *  
  201.      * @param mOrientation 
  202.      *            屏幕方向。ActivityInfo.SCREEN_ORIENTATION_PORTRAIT或者是ActivityInfo. 
  203.      *            SCREEN_ORIENTATION_LANDSCAPE。 
  204.      */  
  205.     public void setmOrientation(int mOrientation) {  
  206.         this.mOrientation = mOrientation;  
  207.     }  
  208.   
  209.     /** 
  210.      * 在前台中执行的代码。如需对界面进行横屏的重新设置,请此在执行setmOrientation()方法。 
  211.      */  
  212.     protected void runOnMainThread() {  
  213.     }  
  214.   
  215.     /** 
  216.      * 在后台中执行的代码。在此进行比较耗时的操作。 
  217.      */  
  218.     protected void runOnBackground() {  
  219.     }  
  220.   
  221.     protected static class UIHandler extends Handler {  
  222.         /** 
  223.          * 是否需要等待。 
  224.          */  
  225.         private int isWaiting = 0;  
  226.         private WeakReference<IntroActivity> activity;  
  227.           
  228.         public UIHandler(IntroActivity activity ){  
  229.             this.activity = new WeakReference<IntroActivity>(activity);  
  230.         }  
  231.   
  232.         public void handleMessage(android.os.Message msg) {  
  233.             if (msg.what == INTRO_PLAY) {  
  234.                 IntroImgResource resource = (IntroImgResource) msg.getData()  
  235.                         .getSerializable(FLAG_RESOURCE);  
  236.                 AlphaAnimation animation = new AlphaAnimation(  
  237.                         resource.startAlpha, 1f);  
  238.                 animation.setDuration(resource.playerTime);  
  239.                 activity.get().mIntroImage.setImageResource(resource.mResId);  
  240.                 activity.get().mIntroImage.startAnimation(animation);  
  241.                 return;  
  242.             }  
  243.               
  244.             if (msg.what == BACKGROUND_FINISH || msg.what == FRONTGROUND_FINISH) {  
  245.   
  246.                 isWaiting |= msg.what;  
  247.                 // 当后台或前台的任务未完成时,不执行Activity的跳转。  
  248.                 if (isWaiting == (BACKGROUND_FINISH | FRONTGROUND_FINISH)) {  
  249.                     activity.get().startActivity(new Intent(activity.get(), activity.get().nextActivity()));  
  250.                     activity.get().finish();  
  251.                     return;  
  252.                 }  
  253.             }  
  254.         };  
  255.     };  
  256.   
  257.     /** 
  258.      * 开场动画的图片资源类。封装了图片、播放时间、开始时的透明程度。 
  259.      *  
  260.      * @author msdx 
  261.      *  
  262.      */  
  263.     protected class IntroImgResource implements Serializable {  
  264.         /** 
  265.          * 序列化ID。 
  266.          */  
  267.         private static final long serialVersionUID = -2257252088641281804L;  
  268.         /** 
  269.          * 资源图片ID. 
  270.          */  
  271.         private int mResId;  
  272.         /** 
  273.          * 播放时间,单位为毫秒。 
  274.          */  
  275.         private int playerTime;  
  276.         /** 
  277.          * 开始时的透明程度。0-1之间。 
  278.          */  
  279.         private float startAlpha;  
  280.   
  281.         /** 
  282.          * 开场动画资源的构造方法。 
  283.          *  
  284.          * @param mResId 
  285.          *            图片资源的ID。 
  286.          * @param playerTime 
  287.          *            图片资源的播放时间,单位为毫秒。。 
  288.          * @param startAlpha 
  289.          *            图片资源开始时的透明程度。0-255之间。 
  290.          */  
  291.         public IntroImgResource(int mResId, int playerTime, float startAlpha) {  
  292.             super();  
  293.             this.mResId = mResId;  
  294.             this.playerTime = playerTime;  
  295.             this.startAlpha = startAlpha;  
  296.         }  
  297.   
  298.         /** 
  299.          * 获取资源图片ID。 
  300.          *  
  301.          * @return 资源图片ID。 
  302.          */  
  303.         public int getmResId() {  
  304.             return mResId;  
  305.         }  
  306.   
  307.         /** 
  308.          * 设置资源图片ID. 
  309.          *  
  310.          * @param mResId 
  311.          *            要设置的资源图片ID. 
  312.          */  
  313.         public void setmResId(int mResId) {  
  314.             this.mResId = mResId;  
  315.         }  
  316.   
  317.         /** 
  318.          * 返回资源图片的播放时间。 
  319.          *  
  320.          * @return 资源图片的播放时间。 
  321.          */  
  322.         public int getPlayerTime() {  
  323.             return playerTime;  
  324.         }  
  325.   
  326.         /** 
  327.          * 设置资源图片的播放时间。 
  328.          *  
  329.          * @param playerTime 
  330.          *            资源图片的播放时间。 
  331.          */  
  332.         public void setPlayerTime(int playerTime) {  
  333.             this.playerTime = playerTime;  
  334.         }  
  335.   
  336.         /** 
  337.          * 得到资源开始时的透明程度。 
  338.          *  
  339.          * @return 
  340.          */  
  341.         public float getStartAlpha() {  
  342.             return startAlpha;  
  343.         }  
  344.   
  345.         /** 
  346.          * 设置资源开始时的透明程度。 
  347.          *  
  348.          * @param startAlpha 
  349.          */  
  350.         public void setStartAlpha(float startAlpha) {  
  351.             this.startAlpha = startAlpha;  
  352.         }  
  353.     }  
  354.   
  355. }  


使用时继承上面的类,主要重写里面的以下两个抽象方法: 
1、nextActivity(),在这里返回下一个Activity。 
2、setIntroResources(List<IntroImgResource> resources),在这里添加要展示的图片资源(包括展示时间)。这里仅支持图片,不支持动画、影片等。展示的效果是渐变显示。 

如果有耗时的操作,重写runOnBackground()方法,它会在一个非UI线程中运行。 
如果要进行横屏等的设置,重写runOnMainThread()方法,它在主线程中运行。设置横屏等行为,请使用代码中提供的方法。 

以上代码来自我的开源项目cfuture-androidkit,目前托管在谷歌上,网址为:http://code.google.com/p/cfuture-androidkit/ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值