天天看点

Android启动应用前的动画

本启动动画主要是利用了布局中一张图片的拉伸、缩放、旋转、渐变的混合而成的动态效果;主要分以下三步展开:

第一:创建一个app工程,并在布局RelativeLayout中添加一个ImageView 控制,宽度设置成满屏,随意选择自己喜欢的一张图片,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.startapp.SplashActivity">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imageViewId"
    android:src="@drawable/r"
    />
</RelativeLayout>           

第二:回到Activity中为其布局添加效果,AlphaAnimation(渐变)范围为0到1,ScaleAnimation(缩放)范围为0到1,RotateAnimation(旋转)范围0到360度;代码如下:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        activity_splash = (RelativeLayout) findViewById(R.id.activity_splash);
        //渐变
        AlphaAnimation aa = new AlphaAnimation(0,1);
        //aa.setDuration(500); //持续时间
        aa.setFillAfter(true);

        //拉伸  ScaleAnimation.RELATIVE_TO_SELF :相对于自身
        ScaleAnimation sa = new ScaleAnimation(0,1,0,1,ScaleAnimation.RELATIVE_TO_SELF,0.5f,ScaleAnimation.RELATIVE_TO_SELF,0.5f);
        //sa.setDuration(500);
        sa.setFillAfter(true);

        //旋转
        RotateAnimation ra = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
       // ra.setDuration(500);
        ra.setFillAfter(true);

        //添加动画到集合,此集合是无序的,
        AnimationSet as = new AnimationSet(false);
        as.addAnimation(aa);
        as.addAnimation(ra);
        as.addAnimation(sa);
        as.setDuration(2000);
        activity_splash.startAnimation(as);
        as.setAnimationListener(new myAnimationListener());
    }           

第三:为动画添加监听事件,当动画执行完后回调函数,以便可以执行其他的操作;此案例使用Toast的方式做演示。代码如下:

class  myAnimationListener implements Animation.AnimationListener{
        //动画开始前回调此函数
        @Override
        public void onAnimationStart(Animation animation) {
        }
        //动画结束后回调此函数
        @Override
        public void onAnimationEnd(Animation animation) {
            Toast.makeText(SplashActivity.this,"动画结束",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }           

继续阅读