在android开发,我们会常常使用到位移动画,普通情况下位移动画有两种实现方式。一种是直接通过java代码去实现,第二种是通过配置文件实现动画,以下是两种动画的基本是用法:

纯Java代码实现:

 

//创建渐变动画 
		Animation animation = new TranslateAnimation(0, 0, 300, 300);
		animation.setDuration(1500);
		animation.setRepeatCount(1);//动画的反复次数
		animation.setFillAfter(true);//设置为true,动画转化结束后被应用
		imageView1.startAnimation(animation);//開始动画

通过配置文件实现:

 

 

1、首先要在res文件夹下建立一个anim文件。在anim建立一个alpha1.xml文件例如以下:

<?

 

xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1500" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="200" android:toYDelta="300" android:repeatCount="3" android:interpolator="@android:anim/cycle_interpolator" android:repeatMode="reverse" /> </set>


2、载入动画

 

Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
		imageView1.startAnimation(animation);//開始动画