3.0以前,android支持两种动画模式,Tween Animation,Frame Animation,在android3.0中又引入了一个新的动画系统:Property Animation,这三种动画模式在SDK中被称为Property Animation,View Animation,Drawable Animation。

我今天要说的就是Tween Animation.要实现它有两种方式。

  • 通过代码控制
  • 通过xml文件进行控制(推荐)

代码控制


大致的步骤是这样的:

  • 先定义一个AnimationSet,用来“盛装”我们的Animation。
  • 然后是创建相应的Animation(android中可以创建的Animation有四种,分别是AlphaAnimation,ScaleAnimation,TransAnimation以及RotateAnimation)。
  • 然后根据帮助文档进行参数的设置就可以了
  • 最后将设置好的Animation添加到AnimationSet中,就可以应用了。

下面是一个简单的代码:

package com.summer.animationutils;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private Button button_rotate,button_alpha,button_translate,button_scale;
    private ImageView imageView;

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

        imageView=(ImageView) findViewById(R.id.imageview1);

        button_rotate=(Button) findViewById(R.id.button_rotate);
        button_alpha=(Button) findViewById(R.id.button_alpha);
        button_translate=(Button) findViewById(R.id.button_translate);
        button_scale=(Button) findViewById(R.id.button_scale);

        button_rotate.setOnClickListener(new RotateAnimationListener());
        button_alpha.setOnClickListener(new AlphaAnimationListener());
        button_translate.setOnClickListener(new TranslateAnimationListener());
        button_scale.setOnClickListener(new ScaleAnimationListener());

    }

    class RotateAnimationListener implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            AnimationSet animationSet=new AnimationSet(true);
            RotateAnimation rotateAnimation=new RotateAnimation(0, 360,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setDuration(1500);
            animationSet.addAnimation(rotateAnimation);
            imageView.startAnimation(animationSet);
        }

    }

    class AlphaAnimationListener implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            AnimationSet animationSet=new AnimationSet(true);
            AlphaAnimation alphaAnimation=new AlphaAnimation(1,0);
            alphaAnimation.setDuration(1500);
            animationSet.addAnimation(alphaAnimation);
            imageView.startAnimation(animationSet);
        }

    }

    class TranslateAnimationListener implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            AnimationSet animationSet=new AnimationSet(true);
            TranslateAnimation translateAnimation=new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,0.5f,
                    Animation.RELATIVE_TO_SELF,0f,
                    Animation.RELATIVE_TO_SELF,1.0f);
            translateAnimation.setDuration(1500);
            animationSet.addAnimation(translateAnimation);
            imageView.startAnimation(animationSet);
        }

    }

    class ScaleAnimationListener implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            AnimationSet animationSet=new AnimationSet(true);
            ScaleAnimation scaleAnimation=new ScaleAnimation(1,0.1f,1,0.1f,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
            scaleAnimation.setDuration(1500);
            animationSet.addAnimation(scaleAnimation);
            imageView.startAnimation(animationSet);
        }

    }

}

小总结:

  • 代码比较简单,就不再一一的进行说明了。
  • 在AnimationSet中可以添加多个Animtion,实现不同的动画效果的叠加。
  • 对于AnimationSet的属性的设置可以全部映射到其内部的所有的动画中。

通过XML文件控制


大致的步骤如下:

  • 在res目录下创建一个anim的文件夹(名称不必拘泥于这一个)
  • 在anim文件夹下创建一个xml文件(以set标签内嵌套alpha,rotate,scale,translate等子标签的方式设置动画效果)
  • 然后在使用到动画效果的地方用相关代码进行添加即可。(稍后详述)

下面是我的一个动画文件示例分解:

如alpha.xml。简单明了清晰

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="0.1"
        android:toAlpha="1.0"
        android:startOffset="500"
        android:duration="1500"
        />
</set>

再看rotate.xml文件,其中pivotX属性的值分别代表着三种不同的情况(当然其他的属性值也是有这个情况滴)

  • android:pivotX=”60”;//绝对位置定位
  • android:pivotX=”60%”;//相对于控件自身的比例定位
  • android:pivotX=”60%p”;//相对于父控件的比例定位
<?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="1500"
        />
</set>

再看translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="50%"
        android:fromYDelta="0%"
        android:toXDelta="0%"
        android:toYDelta="100"
        android:duration="1500"
        />
</set>

最后是scale.xml.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="0.0"
        android:toYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"
        />
</set>

完成了xml动画文件的设置,下一步当然是要进行使用了,否则要它做什么,我们可以使用下慢的代码来进行动画的使用。

Animation animation=(Animation) AnimationUtils.loadAnimation(MainActivity.this,                    R.anim.scale);
imageView.startAnimation(animation);

同样的,其他的动画也可以这么做。


偷偷的告诉你,可以在一个xml文件中同时设置好几个子标签,来完成好几个动画的叠加效果哦。(即如果想要共享一个Interpolator的话,需要将android:shareInterpolator的值设置为true)。至于什么是Interpalotor,就是android自带的一些动画的效果在这个安装目录下可以看到

Android学习之Animation(一)的更多相关文章

  1. Android学习之Animation(三)

    今天观看了一个关于android动画的一些知识,就顺便记录下来,以备之后的学习和参考. 在XML文件中使用LayoutAnimationController 第一步: 在res/anim文件夹下创建一 ...

  2. Android学习之Animation(二)

    接着上次的View Animation动画,这次是Frame Animation.具体点来讲就是在Frame层面上进行变化的动画效果的设置.说白了就是定时更换"背景"图.来实现不同 ...

  3. android学习之-Theme和Style

    android学习之-Theme和Style 分类: android 2013-10-11 15:01 960人阅读 评论(0) 收藏 举报 android style和theme的使用. style ...

  4. 《Android学习指南》目录

    源:<Android学习指南>目录 Android学习指南的内容分类: 分类 描述 0.学习Android必备的Java基础知识 没有Java基础的朋友,请不要先看Android的课程,这 ...

  5. Android 动画——Frame Animation与Tween Animation

    很多手机应用的引导页都是动画的,添加动画后的应用画面会更加生动灵活,今天博主也学习了Android中Animation的使用,下面来总结下.  android中的Animation分为两种,一种是Fr ...

  6. 《Android学习指南》文件夹

    转自:http://android.yaohuiji.com/about Android学习指南的内容分类: 分类 描写叙述 0.学习Android必备的Java基础知识 没有Java基础的朋友,请不 ...

  7. 【Android】完善Android学习(四:API 3.1)

    备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...

  8. 【Android】完善Android学习(三:API 3.0)

    备注:之前Android入门学习的书籍使用的是杨丰盛的<Android应用开发揭秘>,这本书是基于Android 2.2API的,目前Android已经到4.4了,更新了很多的API,也增 ...

  9. Android学习笔记之滑动翻页(屏幕切换)

    如何实现手机上手动滑动翻页效果呢?呵呵,在这里我们就给你们介绍一下吧. 一般实现这个特效会用到一个控件:ViewFlipper <1>View切换的控件—ViewFlipper 这个控件是 ...

随机推荐

  1. CSS :focus 选择器

    :focus 选择器用于选取获得焦点的元素. <!DOCTYPE html> <html> <head> <style> input:focus { b ...

  2. [Java] 设计模式:代码形状 - lambda表达式的一个应用

    [Java] 设计模式:代码形状 - lambda表达式的一个应用 Code Shape 模式 这里介绍一个模式:Code Shape.没听过,不要紧,我刚刚才起的名字. 作用 在应用程序的开发中,我 ...

  3. day05 Servlet 开发和 ServletConfig 与 ServletContext 对象

    day05 Servlet 开发和 ServletConfig 与 ServletContext 对象 1. Servlet 开发入门 - hello world 2. Servlet 的调用过程和生 ...

  4. Docker配置 DNS

    Docker 没有为每个容器专门定制镜像,那么怎么自定义配置容器的主机名和 DNS 配置呢? 秘诀就是它利用虚拟文件来挂载到来容器的 3 个相关配置文件. 在容器中使用 mount 命令可以看到挂载信 ...

  5. Linux常见目录及命令介绍

    一.Linux中常用的目录介绍:     /        -根目录     /bin    -命令保存目录(普通用户亦可读取的命令)     /boot    -启动目录,启动相关文件     /d ...

  6. Azure AI 服务之语音识别

    笔者在前文<Azure AI 服务之文本翻译>中简单介绍了 Azure 认知服务中的文本翻译 API,通过这些简单的 REST API 调用就可以轻松地进行机器翻译.如果能在程序中简单的集 ...

  7. Winform DevExpress控件库(一) DevExpress控件库的安装与新建第一个DevExpress项目

    前言:因为这段时间要接触到DevExpress控件库,而我本身甚至对winform的控件都了解甚少,所以处在学习中,写下博客主要是为了方便后期的回顾,当然也可以给一些新人第一次接触时做为学习的参考,以 ...

  8. MacOS下安装rvm的几点注意

    如果用以下链接无法下载的话: curl -sSL https://get.rvm.io | bash -s stable #或者 curl -L https://rvm.io | bash -s st ...

  9. Device Mapper 代码分析

    Device Mapper(DM)是Linux 2.6全面引入的块设备新构架,通过DM可以灵活地管理系统中所有的真实或虚拟的块设备. DM以块设备的形式注册到Linux内核中,凡是挂载(或者说&quo ...

  10. android MultiDex multidex原理下超出方法数的限制问题(三)

    android MultiDex 原理下超出方法数的限制问题(三)    插件化?自动化?multiDex?是不是觉得已经懵逼了?请先看这篇文章的内容,在下篇文章中将会详解具体的过程- 随着应用不断迭 ...