Animation(二)
本篇介绍如何用配置文件进行控件的动画设置。步骤如下:
1. 在res目录下建立anim目录
2. 在anim目录下创建动画的xml文件
3. 通过AnimationUtils这个类加载动画的xml文件
4. 给你需要的控件绑定Animation
话不多说,代码如下:
package com.kevin.animation;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.ImageView;public class AnimationDemo extends Activity {private ImageView img;private int flag = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); img = (ImageView)findViewById(R.id.imageView1); img.setOnClickListener(new ImgOnClickListenr()); } class ImgOnClickListenr implements OnClickListener{@Overridepublic void onClick(View v) {switch (flag) {case 0:// 加载动画的配置文件Animation alphaAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.alpha);// 调用animationimg.startAnimation(alphaAnimation);flag++;break;case 1:Animation rotateAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.rotate);img.startAnimation(rotateAnimation);flag++;break;case 2:Animation scaleAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.back_scale);img.startAnimation(scaleAnimation);flag++;break;case 3:Animation translateAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.translate);img.startAnimation(translateAnimation);flag = 0;default:break;}} }} <?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="1.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="5000"/> </set>