Android 三大动画

Android中动画分类如下:


一、帧动画

帧动画的原理就是让一系列的静态图片依次播放,利用人眼“视觉暂留”的原理,实现动画。也就是说,看到的动画其实只是一张张的图片短暂的呈现。

1.用xml实现帧动画

在res/drawable中创建xml文件,一般格式如下

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
                  android:oneshot="true|false">
      <item android:drawable="" android:duration=""/>
 </animation-list>

接下来创建一组动画的图片:
fire_animate.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <!--
    一个item对应一帧:一张图片
    drawable:配置动画的图片
    duration:配置这个图片在动画中中出现的时间  单位:毫秒
    -->
    <item android:drawable="@drawable/campfire01" android:duration="80"></item>
    <item android:drawable="@drawable/campfire02" android:duration="80"></item>
    <item android:drawable="@drawable/campfire03" android:duration="80"></item>
    <item android:drawable="@drawable/campfire04" android:duration="80"></item>
    <item android:drawable="@drawable/campfire05" android:duration="80"></item>
    <item android:drawable="@drawable/campfire06" android:duration="80"></item>
    <item android:drawable="@drawable/campfire07" android:duration="80"></item>
    <item android:drawable="@drawable/campfire08" android:duration="80"></item>
    <item android:drawable="@drawable/campfire09" android:duration="80"></item>
    <item android:drawable="@drawable/campfire10" android:duration="80"></item>
    <item android:drawable="@drawable/campfire11" android:duration="80"></item>
    <item android:drawable="@drawable/campfire12" android:duration="80"></item>
    <item android:drawable="@drawable/campfire13" android:duration="80"></item>
    <item android:drawable="@drawable/campfire14" android:duration="80"></item>
    <item android:drawable="@drawable/campfire15" android:duration="80"></item>
    <item android:drawable="@drawable/campfire16" android:duration="80"></item>
    <item android:drawable="@drawable/campfire17" android:duration="80"></item>
</animation-list>

在activity_main中创建一个ImageView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/anim"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/campfire01"
        android:scaleType="fitXY"
        />
</RelativeLayout>

最后,在MainActivity里面调用

public class MainActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       //1.获取动画的控件
       ImageView fires = findViewById(R.id.anim);

       //2.通过控件获取动画
       AnimationDrawable animationDrawable = (AnimationDrawable) fires.getDrawable();

       //3.启动动画
       animationDrawable.start();   
   }
2. 使用java代码创建
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1.创建一个动画资源
         AnimationDrawable anim = new AnimationDrawable();

       //2.添加每一帧的动画
       int []ids = {R.drawable.campfire02,R.drawable.campfire03,R.drawable.campfire04,
            R.drawable.campfire05,R.drawable.campfire06,R.drawable.campfire07,R.drawable.campfire08,
            R.drawable.campfire09,R.drawable.campfire10,R.drawable.campfire11,R.drawable.campfire12,
            R.drawable.campfire13,R.drawable.campfire14,R.drawable.campfire15,R.drawable.campfire16,R.drawable.campfire17};

       for(int id : ids){
                anim.addFrame(getResources().getDrawable(id),100);
                }

       //3.使用这个动画资源
        ImageView iv = findViewById(R.id.anim);
       iv.setImageDrawable(anim);

       //4.启动动画
        ((AnimationDrawable)iv.getDrawable()).start();

    }
}

效果图展示:


二、补间动画

Tween Animation叫补间动画,因为只需要给出动画的开始和结尾的‘ 关键帧 ’,而中间的部分由系统补足,但其视图本身属性等不随动画而改变。

比如,定义一个translate动画,向右移动了200dp,但它的原图没有移动,还是在原来的位置,只是呈现的这个视觉效果。

补间动画包括以下四种:

  • 平移动画(TranslateAnimation)


  • 缩放动画(ScaleAnimation)


  • 旋转动画(RotateAnimation)


  • 透明度动画(AlphaAnimation)


以上四种动画,皆继承于Animation,继承了以下Animation的通用属性和方法:

xml属性 java方法 解释
android:duration setDuration(long) 动画持续时间,毫秒为单位
android:ShareInterpolator setInterpolator(Interpolator) 设定插值器(指定的动画效果,譬如回弹等)
android:fillAfter setFillAfter(boolean) 控件动画结束时是否保持动画最后的状态
android:fillBefore setFillBefore(boolean) 控件动画结束时是否还原到开始动画前的状态
android:repeatMode setRepeatMode(int) 重复类型有两个值,reverse表示倒序回放,restart表示从头播放
android:startOffset setStartOffset(long)<span class="Apple-tab-span" style="white-space:pre"></span> 调用start函数之后等待开始运行的时间,单位为毫秒

接下来使用AnimationSet的方式调用各个动画:

1.xml方式使用

1.在res目录下创建一个目录取名anim,若不行:切换为Project

在下图路径下创建后,再切换回Android


2.在anim路径下创建一个xml,我取名为translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="1000"
        android:fillAfter="true"
        android:repeatMode="reverse"/>

    <translate
        android:fromXDelta="0"
        android:toXDelta="500"
        android:fromYDelta="0"
        android:toYDelta="500"
        android:duration= "1000"
        android:fillAfter="true"
        android:repeatMode="reverse"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        />

    <!-- 缩放,宽高都是从 1 到 0
         pivotX、pivotY 代表缩放中心点的横竖坐标
         interpolator 代表动画模式,我设置为先加速、后减速-->
    <scale
        android:fromXScale="1"
        android:toXScale="0"
        android:fromYScale="1"
        android:toYScale="0"
        android:pivotY="50%"
        android:pivotX="50%"
        android:duration="1000"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fillAfter="true"
        android:repeatMode="reverse"/>

    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0"
        android:duration="1000"
        android:fillAfter="true"
        android:repeatMode="reverse" />
</set>

3.在activity_main中创建一个ImageView,并添加id

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#00F5FF"
        android:layout_centerInParent="true"/>

</RelativeLayout>

4.最后,在MainActivity中调用

public class MainActivity extends AppCompatActivity {

    ImageView iv;
    Animation anim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 找到动画视图
        iv = findViewById(R.id.image);
        // 加载需要执行的动画
        anim = AnimationUtils.loadAnimation(this, R.anim.translate);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            //开始动画
            iv.startAnimation(anim);
        }
        return true;
    }
}
2.使用Java代码创建

MainActivity 中

public class MainActivity extends AppCompatActivity {

    ImageView iv;
    AnimationSet animationSet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 找到动画视图
        iv = findViewById(R.id.image);
        //创建动画集合    参数:是否共享插值器
        animationSet = new AnimationSet(true);

        //创建旋转动画
        RotateAnimation rotate = new RotateAnimation(0, 360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        //创建平移动画
        TranslateAnimation translate = new TranslateAnimation(0,500,0,500);
        translate.setInterpolator(new AccelerateDecelerateInterpolator());
        //创建透明的动画
        AlphaAnimation alpha = new AlphaAnimation(1.0f, 0);
        //创建缩放动画
        ScaleAnimation scale = new ScaleAnimation(1.0f, 0, 1.0f, 0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

        //将动画全部加入动画集合  并设置属性
        animationSet.addAnimation(rotate);
        animationSet.addAnimation(translate);
        animationSet.addAnimation(alpha);
        animationSet.addAnimation(scale);
        animationSet.setDuration(1000);
        animationSet.setFillAfter(true);
        animationSet.setRepeatMode(Animation.REVERSE);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            //开始动画
            iv.startAnimation(animationSet);
        }
        return true;
    }
}

实现效果图:


三、属性动画

所谓属性动画,就是直接对对象的属性进行操作,是真实的改变了对象的属性

属性动画可以看作是增强版的补间动画,与补间动画的不同之处体现在:

  • 补间动画只能定义两个关键帧在透明、旋转、位移和倾斜这四个属性的变换,但是属性动画可以定义任何属性的变化。
  • 补间动画只能对 UI 组件执行动画,但属性动画可以对任何对象执行动画。

与补间动画类似的是,属性动画也需要定义几个方面的属性:

  • 动画持续时间。默认为 300ms,可以通过 android:duration 属性指定。
  • 动画插值方式。通过 android:interploator 指定。
  • 动画重复次数。通过 android:repeatCount 指定。
  • 重复行为。通过 android:repeatMode 指定。
  • 动画集。在属性资源文件中通过 <set .../> 来组合。
  • 帧刷新率。指定多长时间播放一帧。默认为 10 ms。

继承关系如下:

接下来主要介绍ObjectAnimator的使用,因为其他类能实现的ObjectAnimator都能实现

1.在activity_main.xml中创建一个View视图

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">

   <View
       android:id="@+id/view"
       android:layout_width="200dp"
       android:layout_height="200dp"
       android:layout_centerInParent="true"
       android:background="@color/colorAccent"></View>
</RelativeLayout>

2.在MainActivity中创建动画

public class MainActivity extends AppCompatActivity {
    View v;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        v = findViewById(R.id.view);

    }

    // 透明度alpha
    private void test1(){
        /**
         * 1.target  需要动画的控件
         * 2.propertyName  控件的哪个属性
         * 3. ...values   变化的值
         * */
        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(v, "alpha", 1,0,1);
        alphaAnim.setDuration(2000);
        alphaAnim.start();

    }

    // 旋转
    private void test2(){
        ObjectAnimator rotateAnim = ObjectAnimator.ofFloat(v, "rotation", 0,360);
        rotateAnim.setDuration(1000);
        rotateAnim.start();
    }

    // 缩放
    private void test3(){
        ObjectAnimator scaleAnim = ObjectAnimator.ofFloat(v,"scaleX", 1,0.5f);
        scaleAnim.setDuration(1000);
        scaleAnim.setRepeatMode(ValueAnimator.REVERSE);

        ObjectAnimator scaleAnim2 = ObjectAnimator.ofFloat(v,"scaleY", 1,0.5f);
        scaleAnim2.setDuration(1000);
        scaleAnim2.setRepeatMode(ValueAnimator.REVERSE);

        // with 同时执行
        // after 后面执行
        // before 前面执行
        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.playSequentially(scaleAnim,scaleAnim2);
        animatorSet.play(scaleAnim).after(scaleAnim2);
        animatorSet.start();
    }

    private void test4(){
        ObjectAnimator trans = ObjectAnimator.ofFloat(v, "translationX", v.getTranslationX()+100);
        trans.setDuration(3000);
        trans.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            test1();
            test2();
            test3();
            test4();
        }
        return true;
    }
}

实现效果:


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容

  • 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让全面说说Android的动画,所以今...
    未聞椛洺阅读 2,705评论 0 10
  • 【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...
    Rtia阅读 6,144评论 1 38
  • 本笔记的原文本链接 Property Animation Overview 属性动画总览 The property...
    Jaesoon阅读 1,098评论 2 3
  • 转载一篇高质量博文,原地址请戳这里转载下来方便今后查看。1 背景不能只分析源码呀,分析的同时也要整理归纳基础知识,...
    Elder阅读 1,942评论 0 24
  • 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让全面说说Android的动画,所以今...
    lisx_阅读 964评论 0 0