1、res目录中创建anim的目录
a、创建名称为anim_alpha.xml的xml资源文件
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1" android:toAlpha="0" android:fillAfter="true" android:repeatMode="reverse" android:repeatCount="1" android:duration="2000"/> </set>b、创建名称为anim_rotate.xml的xml资源文件
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:interpolator="@android:anim/accelerate_interpolator" android:fromDegrees="0" android:toDegrees="720" android:pivotX="50%" android:pivotY="50%" android:duration="2000"> </rotate> <rotate android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="2000" android:fromDegrees="360" android:toDegrees="0" android:pivotX="50%" android:pivotY="50%" android:duration="2000"> </rotate> </set>c、创建名称为anim_scale .xml的xml资源文件
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="1" android:interpolator="@android:anim/decelerate_interpolator" android:fromYScale="1" android:toXScale="2.0" android:toYScale="2.0" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" android:repeatCount="1" android:repeatMode="reverse" android:duration="2000"/> </set>d、创建名称为anim_translate.xml的xml资源文件
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="860" android:fromYDelta="0" android:toYDelta="0" android:fillAfter="true" android:repeatMode="reverse" android:repeatCount="1" android:duration="2000"> </translate> </set>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout2" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋转" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="平移" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="缩放" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="透明度渐变" /> </LinearLayout> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50px" android:src="@drawable/cat" /> </LinearLayout>
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Animation rotate=AnimationUtils.loadAnimation(this, R.anim.anim_rotate); //获取“旋转”动画资源 final Animation translate=AnimationUtils.loadAnimation(this, R.anim.anim_translate); //获取“平移”动画资源 final Animation scale=AnimationUtils.loadAnimation(this, R.anim.anim_scale); //获取“缩放”动画资源 final Animation alpha=AnimationUtils.loadAnimation(this, R.anim.anim_alpha); //获取“透明度变化”动画资源 final ImageView iv=(ImageView)findViewById(R.id.imageView1); //获取要应用动画效果的ImageView Button button1=(Button)findViewById(R.id.button1); //获取“旋转”按钮 button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { iv.startAnimation(rotate); //播放“旋转”动画 } }); Button button2=(Button)findViewById(R.id.button2); //获取“平移”按钮 button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { iv.startAnimation(translate); //播放“平移”动画 } }); Button button3=(Button)findViewById(R.id.button3); //获取“缩放”按钮 button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { iv.startAnimation(scale); //播放“缩放”动画 } }); Button button4=(Button)findViewById(R.id.button4); //获取“透明度渐变”按钮 button4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { iv.startAnimation(alpha); //播放“透明度渐变”动画 } }); } }