您的位置:首页 > 移动开发 > Android开发

Android学习笔记——Animation

2016-03-09 15:47 477 查看

Frame Animation

Frame动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似,我们称为逐帧动画。Frame动画可以被定义在XML文件中,也可以完全编码实现。

如果被定义在XML文件中,我们可以放置在/res下的anim或drawable目录中, 文件名可以作为资源ID在代码中引用,定义如下:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot=["true" | "false"] >   //是否重复播放     false表示重复播放
<!-- 如果有多帧,添加多个<item>标签即可;duration单位为毫秒-->
<item  android:drawable="@[package:]drawable/drawable_resource_name"
android:duration="integer" />
</animation-list>


// 然后把定义的动画设置给ImageView,并开启动画。
frameImageView.setBackgroundResource(R.drawable.frame);   //  要在xml文件中设置背景
AnimationDrawable anim = (AnimationDrawable) frameImageView.getBackground();
anim.start(); // 停止使用anim.stop();


如果由完全由编码实现(当然帧动画很少这样使)

先创建AnimationDrawable对象,然后调用addFrame(Drawable frame, int duration)向该动画中添加帧,每调用一次addFrame方法,就向

<ImageView
android:id="@+id/frame_iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/frame_iv"
/>
<ImageView
android:id="@+id/frame_iv2"
android:layout_below="@id/frame_iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/frame_iv2"      frame_iv2/里面没有子元素
/>


ImageView iv = (ImageView) findViewById(R.id.frame_iv1);
AnimationDrawable anim1 = (AnimationDrawable) iv.getBackground();
anim1.start(); // 停止使用anim.stop();
//自定义 慢慢添加 动画针
AnimationDrawable anim2 = (AnimationDrawable) iv.getBackground();
for (int i = 0; i < 9; i++) {
anim2.addFrame(getResources().getDrawable(R.drawable.bomb10+i), 60);
}
anim2.start();


Tween Animation

Tween动画概述

Tween动画表示从一个状态A向状态B变化的一个过程(有过CSS 3的2D动 画的使用经历理解起来很轻松),Tween动画在Android中分为4类,它们都 继承自android.view.Animation类,分别是:

AlphaAnimation(透明度动画)

关键:开始透明度、结果透明度、动画持续时间

描述:创建该动画时要指定动画开始时的透明度、结束时的透明度和动 画持续时间。其中透明度可从0变化到1

TranslateAnimation(平移动画)

关键:开始时的X和结束时的X、开始时的Y和结束时的Y、动画持续时间

描述:创建该动画时只要指定动画开始时的位置(以X、Y坐标来表示)、 结束时的位置(以X、Y坐标来表示),并指定动画持续时间即可

ScaleAnimation(缩放动画)

关键:开始时的X和结束时的X、开始时的Y和结束时的Y、动画持续时间、 缩放中心

描述:创建该动画时要指定动画开始时的缩放比(以X、Y轴的缩放参数 来表示)、结束时动画的缩放比(以X、Y轴的缩放参数来表示),并指定动画持续时间。由于缩放时以不同点为中心时缩放效果并 不相同,因此指定缩放动画时还要通过pivotX、pivotY来指定“缩 放中心”的坐标

RotateAnimation(旋转动画)

关键:开始时的X和结束时的X、开始时的Y和结束时的Y、动画持续时间

描述:创建该动画时要指定动画开始时的旋转角度、结束时的旋转角度, 并指定动画持续时间。由于旋转时以不同点为中心时旋转效果并 不相同,因此指定旋转动画时还要通过pivotX、pivotY来指定“旋 转轴心”的坐标

AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢,在中间的时候加速

AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速

AnticipateInterpolator 开始的时候向后然后向前甩

AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值

BounceInterpolator 动画结束的时候弹起

CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线

DecelerateInterpolator 在动画开始的地方快然后慢

LinearInterpolator 以常量速率改变

OvershootInterpolator 向前甩一定值后再回到原来位置

Tween动画的两种创建方式

Java代码

常用方法:

animation.setDuration(2000):持续时间

animation.setRepeatCount(3):设置重复次数,-1表示循环执行

animation.setFillAfter(true):动画执行完后是否停留在执行完的状态

animation.setStartOffset(1000):执行前的等待时间

imageView.startAnimation(animation):把动画添加到组件上



Tween Animation与 interpolator

注:两种创建方式,请参考代码!

package com.hsu.part5_animation;

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.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

/**
* Tween By Code
*/
public class TweenByCodeActivity extends Activity implements OnClickListener {

private Button alphaButton;
private Button rotateButton;
private Button scaleButton;
private Button translateButton;
private Button combinationButton;
private ImageView imageView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_tween_by_code);
this.alphaButton = (Button) super.findViewById(R.id.button_alpha);
this.rotateButton = (Button) super.findViewById(R.id.button_rotate);
this.scaleButton = (Button) super.findViewById(R.id.button_scale);
this.translateButton = (Button) super.findViewById(R.id.button_translate);
this.combinationButton = (Button) super.findViewById(R.id.button_combination);
this.alphaButton.setOnClickListener(this);
this.rotateButton.setOnClickListener(this);
this.scaleButton.setOnClickListener(this);
this.translateButton.setOnClickListener(this);
this.combinationButton.setOnClickListener(this);
this.imageView = (ImageView) super.findViewById(R.id.imageview_girl);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_alpha:
// 关键:开始透明度、结果透明度、动画持续时间
// 描述:创建该动画时要指定动画开始时的透明度、结束时的透明度和动画持续时间。其中透明度可从0变化到1
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0F, 0.8F);
// 持续时间
alphaAnimation.setDuration(2000);
// 设置重复次数,如果为-1表示循环执行
alphaAnimation.setRepeatCount(3);
// 动画执行完后是否停留在执行完的状态
alphaAnimation.setFillAfter(true);
// 执行前的等待时间
alphaAnimation.setStartOffset(1000);
// 把动画添加到组件上
this.imageView.startAnimation(alphaAnimation);
System.out.println("ALPHA:" + this.imageView.getAnimation().getClass().getSimpleName());
break;

case R.id.button_rotate:
// 关键:开始角度、结束角度、动画持续时间、旋转轴心
// 描述:创建该动画时要指定动画开始时的旋转角度、结束时的旋转角度,并指定动画持续时间。
//      由于旋转时以不同点为中心时旋转效果并不相同,因此指定旋转动画时还要通过pivotX、pivotY来指定“旋转轴心”的坐标
// 设置围绕中心点旋转注意:xml:pivotX="50%" pivotY="50%";Java:RotateAnimation.RELATIVE_TO_SELF, 0.5F, RotateAnimation.RELATIVE_TO_SELF, 0.5F
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5F, RotateAnimation.RELATIVE_TO_SELF, 0.5F);
rotateAnimation.setDuration(2000);
this.imageView.startAnimation(rotateAnimation);
System.out.println("ROTATE:" + this.imageView.getAnimation().getClass().getSimpleName());
break;

case R.id.button_scale:
// 关键:开始时的X和结束时的X、开始时的Y和结束时的Y、动画持续时间、缩放中心
// 描述:创建该动画时要指定动画开始时的缩放比(以X、Y轴的缩放参数来表示)、结束时动画的缩放比(以X、Y轴的缩放参数来表示),并指定动画持续时间。
//       由于缩放时以不同点为中心时缩放效果并不相同,因此指定缩放动画时还要通过pivotX、pivotY来指定“缩放中心”的坐标
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0F, 1.2F, 1.0F, 1.2F, RotateAnimation.RELATIVE_TO_SELF, 0.5F, RotateAnimation.RELATIVE_TO_SELF, 0.5F);
scaleAnimation.setDuration(2000);
this.imageView.startAnimation(scaleAnimation);
System.out.println("SCALE:" + this.imageView.getAnimation().getClass().getSimpleName());
break;

case R.id.button_translate:
// 关键:开始时的X和结束时的X、开始时的Y和结束时的Y、动画持续时间
// 描述:创建该动画时只要指定动画开始时的位置(以X、Y坐标来表示)、结束时的位置(以X、Y坐标来表示),并指定动画持续时间即可
TranslateAnimation translateAnimation = new TranslateAnimation(0.0F, 0.0F, 200.0F, 50.0F);
translateAnimation.setDuration(2000);
this.imageView.startAnimation(translateAnimation);
System.out.println("TRANSLATE:" + this.imageView.getAnimation().getClass().getSimpleName());
break;

case R.id.button_combination:
// 组合动画
RotateAnimation ra = new RotateAnimation(0, 360);
ra.setDuration(2000);
TranslateAnimation ta = new TranslateAnimation(0.0F, 0.0F, 200.0F, 50.0F);
ta.setDuration(2000);
AnimationSet animationSet = new AnimationSet(false);
animationSet.addAnimation(ra);
animationSet.addAnimation(ta);
this.imageView.startAnimation(animationSet);
System.out.println("Combination 组合动画");
break;
}
}
}

/**
*  Tween By XML
*/
// 1、先在/res/anim/rotate.xml定义一个动画
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" >
</rotate>
// 2、然后使用AnimationUtils.loadAnimation()加载动画
Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate);
// 3、最后把动画添加到组件上
this.imageView.startAnimation(rotateAnimation);

// 1、先在/res/anim/combination.xml定义一个组合动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" >
<rotate
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" >
</rotate>

<translate
android:duration="2000"
android:fromXDelta="0.0"
android:fromYDelta="0.0"
android:toXDelta="50.0"
android:toYDelta="50.0" >
</translate>
</set>
// 2、然后使用AnimationUtils.loadAnimation()加载动画
Animation combinationAnimation = AnimationUtils.loadAnimation(this, R.anim.combination);
// 3、最后把动画添加到组件上
this.imageView.startAnimation(combinationAnimation);


Property Animation

概述 : 针对于 没有xml的VIew的 动画操作

Android中的动画分为View Animation(Tween)、Drawable Animation(Frame)、Property Animation。从Android 3.0(API Level 11)开始,Android开始支持属性动画。

视图动画局限比较大,如下所述:

视图动画只能使用在View上面。

视图动画并没有真正改变View相应的属性值,这导致了UI效果与实际 View状态存在差异,并导致了一系列怪异行为,比如在使用了视图动画 TranslateAnimation的View的UI上对其触摸,你可能惊讶地发现并没有触 发触摸事件。

鉴于视图动画以上缺陷,从Android 3.0引入了属性动画。具有以下特性:

属性动画应用面更广,不仅仅应用于View,可以将其应用到任意的对象 上面,且该对象不需要具有UI界面

当将属性动画作用于某个对象时,可以通过调用对象的setXXX方法实际 改变对象的值。所以,当将属性动画作用于某个View时,View对象对应 的属性值会被改变。

propertyName:可能的取值

1)translationX 和 translationY:这两个属性控制了View所处的位置,它们的值是由layout容器设置的,是相对于坐标原点(0,0左上角)的一个偏移量。

2)rotation, rotationX 和 rotationY:控制View绕着轴点(pivotX和pivotY)旋转。

3)scaleX 和 scaleY:控制View基于pivotX和pivotY的缩放。

4)pivotX 和 pivotY:旋转的轴点和缩放的基准点,默认是View的中心点。

5)x 和 y:描述了view在其父容器中的最终位置,是左上角左标和偏移量(translationX,translationY)的和。

6)aplha:透明度,1是完全不透明,0是完全透明。

1:ObjectAnimActivity.java

2:ValueAnimator实现动画

3:AnimatorSet的使用

4:如何使用xml文件来创建属性动画

5布局动画(Layout Animations)

6View的anim方法

属性动画中主要的类



github示例代码:https://github.com/mikelkl/AnimationTest
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: