今天就直接写ScaleAnimation了,欲了解 tweenAnimation的相关介绍请看这篇文章android 动画 Tweened Animation 之 TranslateAnimation
ScaleAnimation(缩放动画)
ScaleAnimation是控制对象大小的动画类,用于控制View对象的大小 ,继承自Animation,结构如下:
ScaleAnimation主要有以下几个属性 android:fromXScale (首帧X方向缩放比例) 、 android:toXScale (尾帧X方向缩放比例) 、 android:fromYScale (首帧Y方向缩放比例) 、 android:toYScale (尾帧Y方向缩放比例)、 android:pivotX(动画开始时X方向坐标) 、 android:pivotY (动画开始时Y方向坐标 ) 和Animation中共有的属性 android:duration (动画时长)。
ScaleAnimation就介绍到这里,下面就结合实例讲解一下使用ScaleAnimation的步骤 。
1、在res/drawable目录下放一张名为ting_frame_2的图片 ; 在res/anim目录下创建文件 scale_anim.xml , 内容如下
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="400"
android:pivotY="400"
android:repeatCount="-1"
android:repeatMode="reverse"
android:toXScale="1"
android:toYScale="1" >
</scale>
2、在res/layout目录下创建文件 scale_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/scale_anim_img"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:adjustViewBounds="true"
android:contentDescription="@string/app_name"
android:src="@drawable/ting_frame_2" />
</LinearLayout>
3、创建用于显示动画的activity ScaleAnimActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class ScaleAnimActivity extends Activity {
private ImageView mScaleImg;
private Animation mScaleAnim;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scale_anim);
init();
mScaleAnim = AnimationUtils.loadAnimation(this, R.anim.scale_anim);
}
private void init() {
mScaleImg = (ImageView) findViewById(R.id.scale_anim_img);
}
@Override
protected void onPause() {
super.onPause();
if (mScaleAnim != null) {
mScaleAnim.cancel();
}
}
@Override
protected void onResume() {
super.onResume();
if (mScaleAnim != null) {
mScaleImg.startAnimation(mScaleAnim);
}
}
}
ScaleAnimation和 TranslateAnimation 的使用方法一致 , 若对该文中有不解的地方可以先看看TranslateAnimation , 还有疑问的请留言,谢谢!
ok,大功告成!