您的位置:首页 > 移动开发 > Android开发

Android 动画效果(二):四种基础动画的 **动态设置、动画监听、组合动画

2015-09-19 11:58 543 查看
  



  本篇博客是承接上次的 Android 动画效果(一): 四种动画基础(Alpha、Translate、Rotate、Scale) 进行编写的。这里介绍的只不过是四种基础动画的动态设置、以及它的动画监听(setAnimationListener)、组合动画(AnimationSet)。

学习点:

动画的动态使用

动画监听

动画组合

在这篇博客中我将对动画的设置做一个更加详细的介绍。

一、AlphaAnimation(透明度)




参数:

fromAlpha:开始透明度(数值在0-1之间)

toAlpha:结束透明度(数值在0-1之间)

[code]             //创建AlphaAnimation(透明度动画)
            AlphaAnimation animation=new AlphaAnimation(0.0f, 1.0f);
            //设置动画时间
            animation.setDuration(2000);
            //设置动画重复次数
            animation.setRepeatCount(2);
            //开启动画效果
            imageview.startAnimation(animation);


二、TranslateAnimation(平移)




参数:

fromXDela/toXDela:开始/结束的x坐标

fromYDela/toYDela:开始/结束的y坐标

[code]//创建TranslateAnimation位移动画
            TranslateAnimation trananimation=new TranslateAnimation(-imageview.getMeasuredWidth(), 0,0,300);
            //设置动画时间
            trananimation.setDuration(2000);
            //设置是否记录移动后的位置,true时动画将停留在当前位置,false将回到开始位置
            trananimation.setFillAfter(true);
            //设置插值器,可以理解为用于改变运动形式的东西
            //(现在设置的运动形式类似于自由落体,会有弹跳效果)
            trananimation.setInterpolator(new BounceInterpolator());
            imageview.startAnimation(trananimation);


三、RotateAnimation(旋转)




参数:

fromDegrees/toDegrees:开始/结束角度

pivotX/pivotY:分别为旋转动画相对于x,y的坐标开始位置(相对值)

注:区别与X,Y:是绝对坐标

[code]//参数:从0度旋转100度,旋转中心坐标(100,100)
RotateAnimation rotateAnimation=new RotateAnimation(0, 180, 100, 100);
            rotateAnimation.setDuration(2000);
            imageview.startAnimation(rotateAnimation);


四、ScaleAnimation(放缩)



[code]//参数:X轴从0.2倍放大到1倍(原图)Y轴也一样
ScaleAnimation scaleanimation=new ScaleAnimation(0.2f, 1f, 0.2f, 1f);
            scaleanimation.setDuration(2000);
            imageview.startAnimation(scaleanimation);


五、动画监听(setAnimationListener)

对位移动画添加动画监听器,动画完成就开始第二个动画

[code]TranslateAnimation trananimation2=new TranslateAnimation(-imageview.getMeasuredWidth(), 0,0,100);
                trananimation2.setDuration(2000);
                //设置是否记录移动后的位置,true时动画将停留在原来位置
                trananimation2.setFillAfter(true);

                imageview.startAnimation(trananimation2);

    //监听动画trananimation2,当它运行完时开始第二个动画AlphaAnimation 
    trananimation2.setAnimationListener(new AnimationListener() {
                    //监听动画开始时弹出TOAST
                    @Override
                    public void onAnimationStart(Animation animation) {
                        Toast.makeText(getApplicationContext(), "动画展示开始", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {                    
                    }                   //动画结束时开启第二个
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        AlphaAnimation animation2=new AlphaAnimation(0.0f, 1.0f);
                        animation2.setDuration(2000);
                        animation2.setRepeatCount(2);
                        imageview.startAnimation(animation2);                   

                    }
                });


六、AnimationSet动画组合

1、创建AnimationSet    2、添加动画

[code]//创建几个动画效果
AlphaAnimation animation3=new AlphaAnimation(0.0f, 1.0f);
                animation3.setDuration(2000);
                animation3.setRepeatCount(2);
                //创建AnimationSet 动画组合
                AnimationSet set=new AnimationSet(true);
                TranslateAnimation trananimation3=new TranslateAnimation(-imageview.getMeasuredWidth(), 0,0,100);
                trananimation3.setDuration(2000);           
                RotateAnimation rotateAnimation2=new RotateAnimation(0, 360, 200, 200);
                rotateAnimation2.setDuration(2000);
            //添加组合动画
                set.addAnimation(animation3);
                set.addAnimation(trananimation3);
                set.addAnimation(rotateAnimation2);
                imageview.startAnimation(set);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: