Android动画基础入门

来源:互联网 发布:iccm软件测试定义 编辑:程序博客网 时间:2024/06/11 01:34

          1.逐帧动画:什么叫逐帧。每一张图片就是一帧,一帧一帧的图片按时间顺序播放,就形成了一个动画。定义逐帧动画通过<animation-list/>中使用<item/>子元素定义动画的每一帧,并在item中指定属性duration定义每一帧的持续时间,一个帧动画就这样简单的完成了。代码如下。

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="true"    >    <item android:drawable="@drawable/img1" android:duration="200"></item>    <item android:drawable="@drawable/img2" android:duration="200"></item>    <item android:drawable="@drawable/img3" android:duration="200"></item>    <item android:drawable="@drawable/img4" android:duration="200"></item>    <item android:drawable="@drawable/img5" android:duration="200"></item>    <item android:drawable="@drawable/img6" android:duration="200"></item>    <item android:drawable="@drawable/img7" android:duration="200"></item>    <item android:drawable="@drawable/img8" android:duration="200"></item>    <item android:drawable="@drawable/img9" android:duration="200"></item>    <item android:drawable="@drawable/img10" android:duration="200"></item></animation-list>

当然了,也可以通过Java代码来定义逐帧动画,不过我觉得,能在xml里面干的事情,为什么要放到Java文件里来干呢,看着多不舒服。不过,还是讲解一下步骤为1:创建animationDrawable对象、2:调用addFrame(Drawable ,int)方法像这个动画中添加一帧。代码如下(API需要LOLLIPOP)

        AnimationDrawable animationDrawable=new AnimationDrawable();        animationDrawable.addFrame(getResources().getDrawable(R.drawable.img1,null),200);        animationDrawable.addFrame(getResources().getDrawable(R.drawable.img2,null),200);        animationDrawable.addFrame(getResources().getDrawable(R.drawable.img3,null),200);        animationDrawable.addFrame(getResources().getDrawable(R.drawable.img4,null),200);        animationDrawable.addFrame(getResources().getDrawable(R.drawable.img5,null),200);        animationDrawable.addFrame(getResources().getDrawable(R.drawable.img6,null),200);        animationDrawable.addFrame(getResources().getDrawable(R.drawable.img7,null),200);        animationDrawable.addFrame(getResources().getDrawable(R.drawable.img8,null),200);              animationDrawable.addFrame(getResources().getDrawable(R.drawable.img9,null),200);        animationDrawable.addFrame(getResources().getDrawable(R.drawable.img10,null),200);

恩,在我们已经定义好了animation的资源文件以后,我们应该如何展示我们的动画呢?当然是在ImageView里面啦。ImageView有两个设置图片的方法,一种是src,一种是background,我也不大清楚这两种的设置方式会不会有什么区别,看到大部分都是Background,所以我这里也用backGround了,我们将帧动画设置为ImagView组件BackGround来展示。AnimationDrawable代表的帧动画默认是不播放的,我们需要在Java代码中调用start()方法启动动画。代码如下。

            ImageView imageView=(ImageView)findViewById(R.id.img_animationFrame);            imageView.setBackgroundResource(R.drawable.frame_animation);        AnimationDrawable animationDrawable=(AnimationDrawable)imageView.getBackground();        animationDrawable.start();//        animationDrawable.isRunning()//        animationDrawable.getDuration()//        animationDrawable.isOneShot()//        animationDrawable.setOneShot();//        animationDrawable.setVisible()

好了,这样一个逐帧动画就启动完成了,我们可以调用他的onStop方法暂停动画的播放。

          2.补间动画:开发者只需要制定动画开始和动画结束这两关键的帧还有动画的持续时间,其他的帧数都由系统来自动补齐,这就是补间动画的定义。逐帧动画由AnimationDrawable来表示,那么补间动画由Animation类来表示,Animation是一个抽象类,他有四个实现子类,分别是平移动画(TranslateAnimation)、旋转动画(RotateAnimation)、缩放动画(scaleAnimation)、透明度改变动画(alphaAnimation)。

    public void translateAnimation(View view){        TranslateAnimation translateAnimation=new TranslateAnimation(0,100,0,100);        //从坐标(0,0)移动到坐标(100,100)        TranslateAnimation translateAnimation1=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_PARENT        ,0.5f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);        //如果是相对自己,那么坐标就是自己组件的真实坐标x+参数x*自己组件宽度的大小,否则就是父组件的真实坐标x+参数x1*父组件宽度的大小        translateAnimation1.setDuration(3000);        translateAnimation1.setRepeatCount(1);        translateAnimation1.setRepeatMode(Animation.REVERSE);//重复类型有两个值,reverse表示倒序回放,restart表示从头播放//        translateAnimation1.setFillAfter();//        translateAnimation1.setFillBefore();//        translateAnimation1.setFillEnabled();与android:fillBefore效果相同//        translateAnimation1.setAnimationListener();//        translateAnimation.setInterpolator();//        translateAnimation.setStartOffset();调用start函数之后等待开始运行的时间,单位为毫秒//        translateAnimation1.setStartTime();//        translateAnimation1.reset();重置Animation的初始化//        translateAnimation.start();开始Animation动画//        translateAnimation.cancel();取消Animation动画//        translateAnimation.setZAdjustment();表示被设置动画的内容运行时在Z轴上的位置(top/bottom/normal),默认为normal        imageView.startAnimation(translateAnimation1);//        imageView.clearAnimation();取消当View在执行的Animation动画    }


    public void scaleAnimation(View view){        ScaleAnimation scaleAnimation=new ScaleAnimation(1,2,1,2);        ScaleAnimation scaleAnimation1=new ScaleAnimation(1,2,1,2,imageView.getWidth()/2,imageView.getHeight()/2);//        ScaleAnimation scaleAnimation2=new ScaleAnimation()        scaleAnimation.setDuration(3000);        scaleAnimation.setRepeatCount(1);        scaleAnimation.setRepeatMode(Animation.REVERSE);        imageView.startAnimation(scaleAnimation);    }


 public void rotateAnimation(View view){        RotateAnimation rotateAnimation=new RotateAnimation(0,180);        RotateAnimation rotateAnimation1=new RotateAnimation(0,180,imageView.getWidth()/2,imageView.getHeight()/2);//        RotateAnimation rotateAnimation2=new RotateAnimation()        rotateAnimation.setDuration(3000);        rotateAnimation.setRepeatMode(Animation.REVERSE);        rotateAnimation.setRepeatCount(1);        imageView.startAnimation(rotateAnimation);    }


public void alphaAnimation(View view){        AlphaAnimation alphaAnimation=new AlphaAnimation(0,0.8f);        alphaAnimation.setDuration(3000);        alphaAnimation.setRepeatMode(Animation.REVERSE);        alphaAnimation.setRepeatCount(1);        imageView.startAnimation(alphaAnimation);    }

如果需要多个动画一起使用,就要使用动画集

    public void animationSet(){        AnimationSet animationSet=new AnimationSet(false);//使用自己的插值//        animationSet.addAnimation();        imageView.startAnimation(animationSet);    }
好了,常见的动画,就这样简单的介绍完毕了。

1 0
原创粉丝点击