Android动画分为Tween动画和Frame动画,上一节通过一个实例介绍了Frame动画,本节将介绍Tween动画。Tween可以把对象进行缩小、放大、旋转和渐变等操作。
Tween动画有四个主要的实现,下面分别说明下:
1、AlphaAnimation:渐变动画,主要控制透明度变化动画类,常使用AlphaAnimation(float fromAlpha, float toAlpha)来构造;
fromAlpha:动画开始时的透明度(取值范围为0.0到1.0);
toAlpha:动画结束时的透明度;
2、ScaleAnimation:主要控制尺度变化的动画类,常使用ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)来构造;
fromX:动画开始X坐标上的伸缩尺度;
toX:动画结束X坐标上的伸缩尺度;
fromY:动画开始Y坐标上的伸缩尺度;
toY:动画结束Y坐标上的伸缩尺度;
pivotXType:X坐标上的伸缩模式,取值有:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT;
pivotXValue:X坐标上的伸缩值;
pivotYType:Y坐标上的伸缩模式,取值有:Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT;
pivotYValue:Y坐标上的伸缩值;
3、TranslateAnimation:主要控制位置变换的动画实现类,常使用TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)来构造;
fromXDelta:动画开始的X坐标;
toXDelta:动画结束的X坐标;
fromYDelta:动画开始的Y坐标;
toYDelta:动画结束的Y坐标;
4、RotateAnimation:主要控制旋转的动画实现类,常使用RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)来构造;
fromDegrees:旋转开始角度;
toDegrees:旋转结束角度;
pivotXType, pivotXValue, pivotYType, pivotYValue与尺度变化动画ScaleAnimation类似;
下面以一个实例来进行这些动画。
1、加入将要进行动画变化的MM图片(为了避免侵犯他人肖像权,这次的MM和上次的桌面背景不一样,这个MM没有头像的):
![](http://static.blog.csdn.net/images/hasdel.gif)
2、创建Layout布局XML配置tween.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:apk="http://schemas.android.com/apk/res/android"
apk:orientation="vertical"
apk:layout_width="fill_parent"
apk:layout_height="fill_parent">
<!-- 动画MM -->
<ImageView apk:id="@+id/TweenMM" apk:src="@drawable/tween" apk:layout_width="wrap_content" apk:layout_height="wrap_content"/>
<!-- 动画控制按钮 -->
<LinearLayout apk:layout_weight="1" apk:orientation="horizontal" apk:layout_width="fill_parent" apk:layout_height="wrap_content">
<Button apk:text="ScaleAnim" apk:layout_weight="1" apk:layout_width="fill_parent" apk:layout_height="wrap_content" apk:onClick="onBtnScaleAnimClick"/>
<Button apk:text="AlphaAnim" apk:layout_weight="1" apk:layout_width="fill_parent" apk:layout_height="wrap_content" apk:onClick="onBtnAlphaAnimClick"/>
</LinearLayout>
<LinearLayout apk:layout_weight="1" apk:orientation="horizontal" apk:layout_width="fill_parent" apk:layout_height="wrap_content">
<Button apk:text="TranslateAnim" apk:layout_weight="1" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onBtnTranslateAnimClick"/>
<Button apk:text="RotateAnim" apk:layout_weight="1" apk:layout_width="wrap_content" apk:layout_height="wrap_content" apk:onClick="onBtnRotateAnimClick"/>
</LinearLayout>
</LinearLayout>
一张图片和4个按钮,这4个按钮就是控制图片动画的。
3、对于每个动画,我们都以一个XML配置文件来设置各自动画的属性。
AlphaAnimation(tween_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:duration="5000" />
</set>
android:duration指示该动画持续的时间,以毫秒为单位。
RotateAnimation(tween_rotate.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 ="5000" />
</ set >
< 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 ="5000" />
</ set >
ScaleAnimation(tween_scale.xml)的配置文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="5000" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="5000" />
</set>
TranslateAnimation(tween_translate.xml)的配置文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="10" android:toXDelta="100" android:fromYDelta="10" android:toYDelta="100" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="10" android:toXDelta="100" android:fromYDelta="10" android:toYDelta="100" />
</set>
4、Activity界面类:
/**
* 通过XML配置文件的方式实现Tween动画
*
* @author obullxl@gmail.com
* @version $Id: TweenXMLActivity.java, v 0.1 2011-6-11 下午01:36:39 oldbulla Exp $
*/
public class TweenXMLActivity extends Activity {
public static final String TAG = "TweenActivity";
// 动画图片
private ImageView tweenMM;
/**
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
public void onCreate(Bundle cycle) {
super.onCreate(cycle);
super.setContentView(R.layout.tween);
// 取得动画图片
this.tweenMM = (ImageView) super.findViewById(R.id.TweenMM);
}
/**
* 按钮:尺寸变化动画
*/
public void onBtnScaleAnimClick(View view) {
// 动画开始
this.doStartAnimation(R.anim.tween_scale);
}
/**
* 按钮:渐变动画
*/
public void onBtnAlphaAnimClick(View view) {
// 动画开始
this.doStartAnimation(R.anim.tween_alpha);
}
/**
* 按钮:位置变化动画
*/
public void onBtnTranslateAnimClick(View view) {
// 动画开始
this.doStartAnimation(R.anim.tween_translate);
}
/**
* 按钮:旋转动画
*/
public void onBtnRotateAnimClick(View view) {
// 动画开始
this.doStartAnimation(R.anim.tween_rotate);
}
/**
* 开始动画
*/
private void doStartAnimation(int animId) {
// 加载动画
Animation animation = AnimationUtils.loadAnimation(this, animId);
// 动画开始
this.tweenMM.startAnimation(animation);
}
}
使用AnimationUtils类,可以非常方便的构造动画类;
开始动画,只要ImageView.startAnimation即可。