【例6-2】 补间动画示例
编写一个可以旋转、缩放、淡入/淡出、移动的补间动画程序 。先将图片an.jpg复制到项目的drawable目录下。

教学视频
布局文件activity_main.xml的源代码
<?xml version="1.0" encoding="utf-8"?>
<!--【例6-2】编写一个可以旋转、缩放、淡入/淡出、移动的补间动画程序。-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="动画演示"
android:textSize="26sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/rotateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="旋转"
android:textSize="18sp" />
<Button
android:id="@+id/scaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放"
android:textSize="18sp" />
<Button
android:id="@+id/alphaButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="淡入淡出"
android:textSize="18sp" />
<Button
android:id="@+id/translateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="移动"
android:textSize="18sp" />
</LinearLayout>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:src="@drawable/an" />
</LinearLayout>
</LinearLayout>
控制文件TwAnimationActivity.java的源代码如下:
package com.example.chap06;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class TwAnimationActivity extends AppCompatActivity {
private Button rotateButton ,scaleButton,alphaButton ,translateButton;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rotateButton = (Button) findViewById(R.id.rotateButton);
scaleButton = (Button)findViewById(R.id.scaleButton);
alphaButton = (Button)findViewById(R.id.alphaButton);
translateButton = (Button)findViewById(R.id.translateButton);
image = (ImageView)findViewById(R.id.image);
rotateButton.setOnClickListener(new RotateButtonListener());
scaleButton.setOnClickListener(new ScaleButtonListener());
alphaButton.setOnClickListener(new AlphaButtonListener());
translateButton.setOnClickListener(new TranslateButtonListener());
}
class RotateButtonListener implements View.OnClickListener{ //补间动画的4种子类之一:旋转
@Override
public void onClick(View view) {
RotateAnimation rotateAnimation=new RotateAnimation(0,360,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(1000);
AnimationSet animationSet= new AnimationSet(true);//动画集
animationSet.addAnimation(rotateAnimation);
image.startAnimation(animationSet);
}
}
class ScaleButtonListener implements View.OnClickListener{ //补间动画的4种子类之一:缩放
@Override
public void onClick(View view) {
ScaleAnimation scaleAnimation= new ScaleAnimation(0,1,0,1,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
scaleAnimation.setDuration(1000);
AnimationSet animationSet= new AnimationSet(true);
animationSet.addAnimation(scaleAnimation);
image.startAnimation(animationSet);
}
}
class AlphaButtonListener implements View.OnClickListener{ //补间动画的4种子类之一:淡入淡出
@Override
public void onClick(View view) {
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(1000);
AnimationSet animationSet= new AnimationSet(true);
animationSet.addAnimation(alphaAnimation);
image.startAnimation(animationSet);
}
}
class TranslateButtonListener implements View.OnClickListener{ //补间动画的4种子类之一:移动
@Override
public void onClick(View view) {
TranslateAnimation translateAnimation=new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,0.5f);
translateAnimation.setDuration(1000);
AnimationSet animationSet= new AnimationSet(true);
animationSet.addAnimation(translateAnimation);
image.startAnimation(animationSet);
}
}
}
打开项目配置文件AndroidManifest.xml,配置要启动的Activity类名
<activity android:name=".TwAnimationActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

