zoukankan      html  css  js  c++  java
  • Android动画主要包含补间动画(Tween)View Animation、帧动画(Frame)Drawable Animation、以及属性动画Property Animation

    程序运行效果图:


    Android动画主要包含补间动画(Tween)View Animation、帧动画(Frame)Drawable Animation、以及属性动画Property Animation。下面依次介绍一下各个动画。


    1. 补间动画(Tween)


    Tween动画,通过对View的内容进行一系列的图形变换 (包括平移、缩放、旋转、改变透明度)来实现动画效果。动画效果的定义可以采用XML来做也可以采用编码来做。Tween动画有4种类型:

    动画的类型

    Xml定义动画使用的配置节点

    编码定义动画使用的类

    渐变透明度动画效果

     

    AlphaAnimation

    渐变尺寸缩放动画效果

     

    ScaleAnimation

    画面位置移动动画效果

     

    TranslateAnimation

    画面旋转动画效果

     

    RotateAnimation

    我们可以为每一个动画设置动画插入器,Android自带的几种动画插入器:

    AccelerateInterpolator

    加速,开始时慢中间加速

    DecelerateInterpolator

    减速,开始时快然后减速

    AccelerateDecelerateInterolator

    先加速后减速,开始结束时慢,中间加速

    AnticipateInterpolator

    反向,先向相反方向改变一段再加速播放

    AnticipateOvershootInterpolator

    反向加超越,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值

    BounceInterpolator

    跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100

    CycleIinterpolator

    循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles* Math.PI* input)

    LinearInterpolator

    线性,线性均匀改变

    OvershottInterpolator

    超越,最后超出目的值然后缓慢改变到目的值

    1.1预备知识:

    抽象类Animation是一个实现androidUI界面动画效果的API,Animation是补间动画的基类,它的直接子类AlphaAnimation, RotateAnimation, ScaleAnimation, TranslateAnimation,AnimationSet,提供了一系列的动画效果,可以进行淡入淡出、旋转、缩放、动画集等,这些效果可以应用在绝大多数的控件中。

    1.2AlphaAnimation实现淡入淡出的动画效果

    //方式一通过代码的方式定义透明度动画

    AnimationalphaAnimation=new AlphaAnimation(1, (float) 0.1);

    alphaAnimation.setDuration(3000);//设置动画持续时间为3秒

    alphaAnimation.setFillAfter(true);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)

    imgShow.startAnimation(alphaAnimation);

    //方式二通过在xml中定义透明度动画

    第一步:定义xml动画文件:alpha.xml

    "1.0"encoding="utf-8"?>

    "http://schemas.android.com/apk/res/android"

    android:fromAlpha="1.0"

    android:toAlpha="0.1"

    android:duration="3000"

    android:fillAfter="true"

    android:repeatCount="2">

    第二步:加载xml动画文件并将其设置到指定的View

    AnimationalphaAnimation2=AnimationUtils.loadAnimation(this, R.anim.alpha);//加载Xml文件中的动画

    imgShow.startAnimation(alphaAnimation2);

    程序运行效果图:


     

    1.3.RotateAnimation实现旋转的动画效果

    主要属性及说明:

    repeatCount 重复次数

    fromDegrees为动画起始时物件的角度:

    当角度为负数——表示逆时针旋转

    当角度为正数——表示顺时针旋转

    (负数fromDegrees——toDegrees正数:顺时针旋转)

    (负数fromDegrees——toDegrees负数:逆时针旋转)

    (正数fromDegrees——toDegrees正数:顺时针旋转)

    (正数fromDegrees——toDegrees负数:逆时针旋转)

    toDegrees属性为动画结束时物件旋转的角度可以大于360度

    pivotX,pivotY 为动画相对于物件的X、Y坐标的开始位.说明:以上两个属性值从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置。

    实例:

    //方式一通过代码的方式定义旋转动画

    AnimationrotateAnimation=new RotateAnimation(0, 45);

    rotateAnimation.setDuration(3000);//设置动画持续时间为3秒

    rotateAnimation.setFillAfter(true);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)

    imgShow.startAnimation(rotateAnimation);

    //方式二通过在xml中定义旋转动画

    第一步:定义xml动画文件:rotate.xml

    "1.0"encoding="utf-8"?>

    "http://schemas.android.com/apk/res/android"

    android:fromDegrees="0"

    android:toDegrees="45"

    android:duration="300"

    android:fillAfter="true">

     

    第二步:加载xml动画文件并将其设置到指定的View

    Animation rotateAnimation2=AnimationUtils.loadAnimation(this, R.anim.rotate);//加载Xml文件中的动画

    imgShow.startAnimation(rotateAnimation2);

    程序运行效果图:



    1.4.ScaleAnimation实现缩放动画效果

    主要属性及说明:

    fromXScale(浮点型)属性为动画起始时X坐标上的缩放尺寸

    fromYScale(浮点型)属性为动画起始时Y坐标上的缩放尺寸

    toXScale(浮点型) 属性为动画结束时X坐标上的缩放尺寸

    toYScale(浮点型) 属性为动画结束时Y坐标上的缩放尺寸

    说明: 以上四种属性值

    0.0表示收缩到没有

    1.0表示正常无缩放

    值小于1.0表示收缩

    值大于1.0表示放大

    pivotX(浮点型) 属性为动画相对于物件的X坐标的开始位置

    pivotY(浮点型) 属性为动画相对于物件的Y坐标的开始位置

    说明:

    以上两个属性值从0%-100%中取值

    50%为物件的X或Y方向坐标上的中点位置

    duration(长整型)属性为动画持续时间。说明: 时间以毫秒为单位

    fillAfter(布尔型)属性当设置为true,该动画转化在动画结束后被应用

    实例:

    //方式一通过代码的方式定义缩放动画

    AnimationscaleAnimation=new ScaleAnimation(0.5f, 1.0f,1.0f, 1.0f);

    scaleAnimation.setDuration(2000);//设置动画持续时间为3秒

    scaleAnimation.setFillAfter(true);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)

    scaleAnimation.setRepeatCount(3);

    imgShow.startAnimation(scaleAnimation);

    //方式二通过在xml中定义缩放动画

    第一步:定义xml动画文件:scale.xml

    "1.0"encoding="utf-8"?>

    "http://schemas.android.com/apk/res/android"

    android:fromXScale="0.5"

    android:toXScale="1.0"

    android:fromYScale="1.0"

    android:toYScale="1.0"

    android:duration="3000"

    android:fillAfter="true">

     

     

    第二步:加载xml动画文件并将其设置到指定的View

    AnimationscaleAnimation2=AnimationUtils.loadAnimation(this, R.anim.scale);//加载Xml文件中的动画imgShow.startAnimation(scaleAnimation2);

    程序运行效果图:



    1.5. TranslateAnimation实现位移动画效果

    //方式一通过代码的方式定义位移动画

    AnimationtranslateAnimation=new TranslateAnimation(0, 100, 0, 0);

    translateAnimation.setDuration(3000);//设置动画持续时间为3秒

    translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);//设置动画插入器

    translateAnimation.setFillAfter(true);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)

    imgShow.startAnimation(translateAnimation);

    //方式二通过在xml中定义位移动画

    第一步:定义xml动画文件:translate.xml

    "1.0"encoding="utf-8"?>

    "http://schemas.android.com/apk/res/android"

    android:fromXDelta="0"

    android:toXDelta="260"

    android:fromYDelta="0"

    android:toYDelta="600"

    android:duration="3600"

    android:fillAfter="true"

    android:interpolator="@android:anim/accelerate_decelerate_interpolator">

     

    第二步:加载xml动画文件并将其设置到指定的View

    AnimationtranslateAnimation2=AnimationUtils.loadAnimation(this, R.anim.translate);//加载Xml文件中的动画

    imgShow.startAnimation(translateAnimation2);

     

    程序运行效果图:



    1.6.AnimationSet实现多种动画混合效果

    定义动画集主要用到了AnimationSet类,该类可以添加多个补间动画啊。

    //方式一通过代码的方式定义动画集

    AnimationSetanimationSet=new AnimationSet(true);//定义一个动画集,并设定所有的动画使用一个动画插入其

    Animation translateAnimation2=AnimationUtils.loadAnimation(this, R.anim.translate);//加载Xml文件中的动画

    AnimationalphaAnimation2=AnimationUtils.loadAnimation(this, R.anim.alpha);//加载Xml文件中的动画

    Animation rotateAnimation2=AnimationUtils.loadAnimation(this, R.anim.rotate);//加载Xml文件中的动画

    Animation scaleAnimation2=AnimationUtils.loadAnimation(this, R.anim.scale);//加载Xml文件中的动画

    animationSet.addAnimation(translateAnimation2);

    animationSet.addAnimation(alphaAnimation2);

    animationSet.addAnimation(rotateAnimation2);

    animationSet.addAnimation(scaleAnimation2);

    animationSet.setInterpolator(this, android.R.anim.anticipate_interpolator);

    imgShow.startAnimation(animationSet);

    //方式二在xml文件中设置动画集合

    第一步:定义xml动画文件:animset.xml

    "1.0"encoding="utf-8"?>

    "http://schemas.android.com/apk/res/android">

     

    android:fromAlpha="1.0"

    android:toAlpha="0.1"

    android:duration="3000"

    android:fillAfter="true"

    android:repeatCount="2">

     

    android:fromDegrees="0"

    android:toDegrees="45"

    android:duration="300"

    android:fillAfter="true">

     

    android:fromXScale="0.5"

    android:toXScale="1.0"

    android:fromYScale="1.0"

    android:toYScale="1.0"

    android:duration="3000"

    android:fillAfter="true">

     

    android:fromXDelta="0"

    android:toXDelta="260"

    android:fromYDelta="0"

    android:toYDelta="600"

    android:duration="3600"

    android:fillAfter="true"

    android:interpolator="@android:anim/accelerate_decelerate_interpolator">

     

     

    第二步:加载xml动画文件并将其设置到指定的View

    AnimationSetanimationSet2=(AnimationSet) AnimationUtils.loadAnimation(this, R.anim.animset);

    程序运行效果图:



     

    提示:虽然可以通过代码的方式定义动画,但是Android官方还是建议在xml中定义动画效果,这样可做到最大程度上的解耦,方便项目的后期维护。

     

    2. 帧动画(Frame)


    Frame动画,即顺序播放事先做好的图像,跟放胶片电影类似。

    开发步骤:

    (1)把准备好的图片放进项目res/ drawable下。

    (2)在项目的drawable文件夹下面定义动画XML文件,文件名称可以自定义。当然也可以采用编码方式定义动画效果(使用AnimationDrawable类)。

    (3)为View控件绑定动画效果。调用代表动画的AnimationDrawable的start()方法开始动画。

    实例:新建anim文件夹,建立framelist.xml文件

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
     3 
     4 android:oneshot="true">
     5 
     6 <item android:drawable="@drawable/skin_tab_icon_contact_normal"
     7 
     8 android:duration="200" />
     9 
    10 <item android:drawable="@drawable/skin_tab_icon_conversation_normal"
    11 
    12 android:duration="200" />
    13 
    14 <item android:drawable="@drawable/skin_tab_icon_plugin_normal"
    15 
    16 android:duration="200" />
    17 
    18 <item android:drawable="@drawable/skin_tab_icon_setup_normal"
    19 
    20 android:duration="200" />
    21 
    22 </animation-list>

    代码分析:

    上面的XML就定义了一个Frame动画,其包含8帧动画,8帧动画中分别应用了drawable中的8张图片:zzlx1、zzlx2、zzlx3....,每帧动画持续200毫秒。android:oneshot属性如果为true,表示动画只播放一次停止在最后一帧上,如果设置为false表示动画循环播放。

    在Xml中定义好帧动画之后就可以将其设置到View上了,请看下面代码:

    //第一步将animation-list设置为ImageView的背景

    image.setBackgroundResource(R.anim.framelist);

    //第二步获取ImagView的背景并将其转换成AnimationDrawable

    imageanim = (AnimationDrawable) image.getBackground();

    //第三步开始播放动画

    imageanim.start();

    提示:

    有一点需要强调的是:启动Frame动画的代码animationDrawable.start();不能应 用在OnCreate()方法中,因为在OnCreate()中AnimationDrawable还没有完全的与ImageView绑定。在 OnCreate()中启动动画,只能看到第一张图片。这里在触摸事件中实现的

     1 public class MainActivity extends Activity {
     2 
     3     private ImageView image;
     4     private AnimationDrawable imageanim;
     5 
     6     @Override
     7     protected void onCreate(Bundle savedInstanceState) {
     8         super.onCreate(savedInstanceState);
     9         setContentView(R.layout.activity_main);
    10         
    11         image = (ImageView) findViewById(R.id.iv);
    12         image.setBackgroundResource(R.anim.framelist);
    13         imageanim = (AnimationDrawable) image.getBackground();
    14     } 
    15         
    16     public boolean onTouchEvent(MotionEvent event) {
    17 
    18             if (event.getAction() == MotionEvent.ACTION_DOWN) {
    19 
    20                 imageanim.start();
    21 
    22             return true;
    23 
    24             }
    25 
    26             return super.onTouchEvent(event);
    27 
    28             }
    29     
    30 
    31   
    32 }

    程序运行效果图:



    3. 属性动画(Property Animation)

    使用属性动画注意事项:

    1). object必须要提供setXxx方法,如果动画的时候没有传递初始值,那么还要提供getXxx方法,因为系统要去拿xxx属性的初始值(如果这条不满足,程序直接Crash)

    2). object的setXxx对属性xxx所做的改变必须能够通过某种方法反映出来,比如会带来ui的改变啥的(如果这条不满足,动画无效果但不会Crash)

    以上条件缺一不可。

    3).对应没有getXxx和setXxx方法或有getXxx和setXxx方法但和getXxx和 setXxx方法设置的属性不是我们想要的效果如,我们对Button的width属性做动画没有效果?这是因为Button内部虽然提供了 getWidth和setWidth方法,但是这个setWidth方法并不是改变视图的大小,它是TextView新添加的方法,View是没有这个 setWidth方法的,由于Button继承了TextView,所以Button也就有了setWidth方法.getWidth的确是获取View 的宽度的,而setWidth是TextView和其子类的专属方法,它的作用不是设置View的宽度,而是设置TextView的最大宽度和最小宽度 的,这个和TextView的宽度不是一个东西,具体来说,TextView的宽度对应Xml中的android :layout_width属性,而TextView还有一个属性android :width,这个android:width属性就对应了TextView的setWidth方法。这里给出一种解决方法及使用包装类:用一个类来包装 原始对象,间接为其提供get和set方法。如下:

    /**

    *包装类用于封装View的with、height,

    *你可以通过getXxx以及setXxx方法设置View的宽和高

    *@author jph

    */

    class WrapView{

    private Viewview;

    privateintwidth;

    privateintheight;

    public WrapView(View view){

    this.view=view;

    }

    publicint getWidth() {

    returnview.getLayoutParams().width;

    }

    publicvoid setWidth(int width) {

    this.width = width;

    view.getLayoutParams().width=width;//修改View的高度

    view.requestLayout();//刷新View的布局

    }

    publicint getHeight() {

    returnview.getLayoutParams().height;

    }

    publicvoid setHeight(int height) {

    this.height=height;

    view.getLayoutParams().height = height;

    view.requestLayout();

    }

    }

    实例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    package com.jph.anim;
     
    import android.animation.ObjectAnimator;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageView;
     
    /**
     * 属性动画实例
     * @author jph
     *
     */
    public class PropertyActivity extends Activity {
        private ImageView imgShow;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.property);
            imgShow=(ImageView)findViewById(R.id.imgShow);     
        }
        public void play(View view) {
            switch (view.getId()) {
            case R.id.btnStart:
                ObjectAnimator animator=ObjectAnimator.ofInt(new WrapView(imgShow),
                        "width", 10);
                animator.setDuration(2000);//设置动画持续的时间
                animator.setRepeatCount(2);//设置动画重复的次数 
                animator.start();//开启动画
            default:
                break;
            }
        }
        /**
         * 包装类用于封装View的with、height,
         * 你可以通过getXxx以及setXxx方法设置View的宽和高
         * @author jph
         */
        class WrapView{
            private View view;
            private int width;
            private int height;    
            public WrapView(View view){
                this.view=view;
            }      
            public int getWidth() {
                return view.getLayoutParams().width;
            }
            public void setWidth(int width) {
                this.width = width;
                view.getLayoutParams().width=width;//修改View的高度
                view.requestLayout();//刷新View的布局
            }
            public int getHeight() {
                return view.getLayoutParams().height;
            }
            public void setHeight(int height) {
                this.height=height;
                view.getLayoutParams().height = height;
                view.requestLayout();
            }              
        }
    }

    程序运行效果图:

  • 相关阅读:
    AX2009 VS平台报表开发学习笔记(二)数据源
    字符集的问题
    php侧拉菜单,漂亮,可以向右或者向左展开,支持FF,IE
    ASP正则函数替换分页后的参数
    asp 图片正则 替换,替换前检查图片是不是本地地址的方法
    asp正则过滤重复字符串的代码
    PHP 正则 email语句详解
    签东软了
    东软医疗面试归来
    linux的svn co
  • 原文地址:https://www.cnblogs.com/wangying222/p/5303441.html
Copyright © 2011-2022 走看看