转载请标明出处:http://blog.csdn.net/u011546655/article/details/45921025
背景:
其实我们Android 中大家都知道就那些东西,什么四大组件,activity,service,content provider,当然还有其东西,今天我也去总结了下,来说说Android 中动画这一模块,可能会有许多遗漏,希望大家见谅,多多给予补充。
一:老规矩了,先上效果图,没图没真相
二:Android Animation 内容的介绍
主要的内容包括以下
1.Animation ------------ 动画
2.tween Animation -------------补间动画
3.AnimationSet ----------------动画集
4.interpolator ------------ 插值器
5.Frame Animation --------- 帧动画
2.tween Animation -------------补间动画
3.AnimationSet ----------------动画集
4.interpolator ------------ 插值器
5.Frame Animation --------- 帧动画
(1)Animation 简介
Animation是Android 中一个实现了ui界面动画的一个api ,它自身一提供了很多动画效果,有淡入淡出,旋转,缩放等很多很炫的动画,在Android 的很多控件中,都是可以使用它们的动画效果,实现起来非常的棒。
(2)Animation 分类
①:Tween Animation 补间动画
1.alpha表示的是:淡入
2.rotate表示的是:选装
3.scale表示的是:缩放
4.translate表示的是:移动
②:Fram Animation 帧动画
什么是帧动画那,帧动画就是在一定的时间内,可以按照自己设定的时间,在规定的时间内执行自己想要执行的动画。例如:在Android中gif动态图,就是帧动画,或者还有平时发的qq表情也是帧动画。
(3)Animation 的使用方法
1.创建AnimationSet对象
2.创建相应的补间动画(RotateAnimation等)对象
3.对相应的补间动画(RotateAnimation等)设置属性
4.将补间动画(RotateAnimation等)添加到AnimationSet中
5.使用控件的对象启动AnimationSet
(4)Animation 的具体实施
AlphaAnimation的使用:淡入淡出
代码:
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
image.startAnimation(animationSet);
ps:在AlphaAnimation中有两个参数,分别是float的类型,其中1.0f---0.0f这个表示的是冲完全的透明到完全的不透明,当然你也可以设置其他,比如:反过来,那么就是从完全不透明到透明,也就是所说的淡出,或者我想让它,刚出来看起来比较模糊,可以设置什么0.1等等,这个随意发挥,第2行代码,就是用一个imageview控件去启动这个动画。
第一行:AnimationSet 参数
false:表示使用Animation本身的interpolator
true:表示使用自身的
ScaleAnimation的使用:缩放
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 0, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
image.startAnimation(animationSet);
ps:在ScaleAnimation 中
参数1:x轴的初始值
参数2:x轴缩略后的值
参数3:y轴的初始值
参数4:y轴缩略后的值
参数5:确定x轴的坐标类型
参数6:x轴的值,0.5代表控件的一半长度
参数7:确定y轴的坐标类型
参数8:y轴的值,0.5代表控件的一半长度
RotateAnimation的使用:旋转
代码:
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
ps:RotateAnimation中
参数1:开始选择的位置
参数2:选择多少度
代码:
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, 0.5f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
image.startAnimation(animationSet);
ps:TranslateAnimation 中
参数1:x轴的开始位置
参数2:y轴的开始位置
参数3:x轴的结束位置
参数4:y轴的结束位置
(5)Animation的通用属性设置
setDuration(long durationMills); // 设置动画的时长
setFillAfter(Boolean fillAfter); // 执行动画结束之后,停留在执行结束后的状态
setFillBefore(Boolean fillBefore); // 执行动画结束之后,停留在执行之前的状态
setStartOffset(long startOffset); // 在执行动画之前,等待多少时间
setRepeatCount(int repeatCount); // 动画执行多少次
repeatMode(Boolean mode): // 顺序重复/倒序重复
(6)示例代码
package com.example.testdemo;
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;
/**
* 动画演示
* @author zengtao 2015年6月10日 下午4:02:13
*
*/
public class MainActivity extends Activity {
private ImageView image;
private Button alpha, scale, rotate, translate, start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initLinstener();
}
private void initLinstener() {
alpha.setOnClickListener(onClickListener);
scale.setOnClickListener(onClickListener);
rotate.setOnClickListener(onClickListener);
translate.setOnClickListener(onClickListener);
start.setOnClickListener(onClickListener);
}
private void initView() {
image = (ImageView) findViewById(R.id.image);
alpha = (Button) findViewById(R.id.alpha);
scale = (Button) findViewById(R.id.scale);
rotate = (Button) findViewById(R.id.rotate);
translate = (Button) findViewById(R.id.trans);
start = (Button) findViewById(R.id.start);
}
private void alpha() {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
image.startAnimation(animationSet);
}
private void scale() {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 0, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(1000);
animationSet.addAnimation(scaleAnimation);
image.startAnimation(animationSet);
}
private void rotate() {
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
}
private void translate() {
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, 0.5f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
image.startAnimation(animationSet);
}
private void start() {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
}
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (v == alpha) {
alpha();
} else if (v == scale) {
scale();
} else if (v == rotate) {
rotate();
} else if (v == translate) {
translate();
} else if (v == start) {
start();
}
}
};
}
ps:以上的动画都是在代码中实现的,其实我们也可以在xml文件使用,让后借助于Animation的工具类
xml文件---只写一个,看看,其他都差不多,都是用相应的属性,来设置的
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<!--
起始x轴坐标
止x轴坐标
始y轴坐标
止y轴坐标
轴的坐标
轴的坐标
-->
<scale
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"/>
</set>
代码:
Animation animation = AnimationUtils.loadAnimation(
Animation1Activity.this, R.anim.scale);
image.startAnimation(animation);
ps:loadAnimation 方法
参数1:上下文,不用说
参数2:id代表的是你自己所写的xml文件id
以上呢,其实都只执行的单个动画效果,那么,如何执行多个动画效果了呢?
三:答案是:AnimationSet
1.AnimationSet是Animation的子类;
2.一个AnimationSet包含了一系列的Animation;
3.针对AnimationSet设置一些Animation的常见属性(如startOffset,duration等),可以被包含在AnimationSet当中的Animation集成;
例:一个AnimationSet中有两个Animation,效果叠加
在上面的代码中,其实,我们也使用到了AnimationSet但是,我们都是只添加了一个动画,所以,多个动画效果的添加,也是非常容易的。
代码:
private void start() {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
}
ps:改方法中,就是实现了动画的组合,以及用到了动画的一些属性,看起来也是非常才简单。
四:Interpolator的具体使用方法
Interpolator定义了动画变化的速率,在Animations框架当中定义了一下几种
InterpolatorAccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候速率快。
AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速LinearInterpolator:动画以均匀的速率改变
代码:
AnimationSet animationSet = newAnimationSet(true);
animationSet.setInterpolator(new AccelerateInterpolator());
ps:这样呢,就能够将Interpolator插值器放入动画中了,在开启动画的过程中,就会添加相应想动画速度效果。Google给我们准备的还是非常的充分的,你能想到的,他基本都想到的,你想不到的,或许他都实现了,只是没有发现用法而已。
五:Fram Animation 帧动画
对于帧动画,我使用的不是很多,所有了解也不深,Frame Animation是一针一针的显示,先也举例子了,例如QQ表情什么,老电影等,接下来看下动画效果
效果图:
这种效果便是一帧一帧的动画gif图
六:总结
以上呢,便见简单的说明了下动画的一些基本功能和使用,并不是太深入,只针对于基础了解动画者适合。