Android学习之Animation(一)

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

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自带的一些动画的效果在这个安装目录下可以看到

目录
相关文章
|
1月前
|
XML 缓存 Android开发
Android开发,使用kotlin学习多媒体功能(详细)
Android开发,使用kotlin学习多媒体功能(详细)
101 0
|
3月前
|
安全 Linux Android开发
Android安全启动学习(一):AVB校验是什么?
Android安全启动学习(一):AVB校验是什么?
99 0
|
3月前
|
存储 安全 Linux
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
Android安全启动学习(四):device-mapper-verity (dm-verity)和哈希树
124 0
|
1天前
|
网络协议 Shell Android开发
Android 深入学习ADB调试原理(1)
Android 深入学习ADB调试原理(1)
7 1
|
3月前
|
开发框架 物联网 数据库
89个android学习样例源码
89个android学习样例源码
76 0
|
3月前
|
Java Android开发 C++
2023安卓逆向 -- JNI学习(从开发到反编译)
2023安卓逆向 -- JNI学习(从开发到反编译)
21 0
|
3月前
|
Android开发
Android源码学习(五):AVB2.0-libavb库介绍2
Android源码学习(五):AVB2.0-libavb库介绍2
105 0
|
3月前
|
安全 算法 Android开发
Android安全启动学习(五):Android Verified Boot 2.0
Android安全启动学习(五):Android Verified Boot 2.0
230 0
|
3月前
|
存储 安全 Android开发
Android安全启动学习(三):AVB校验的内容、怎么校验、AVB的作用
Android安全启动学习(三):AVB校验的内容、怎么校验、AVB的作用
152 0
|
3月前
|
存储 缓存 安全
Android安全启动学习(二):android镜像有什么?
Android安全启动学习(二):android镜像有什么?
83 0