安卓学习笔记32:实现补间动画
零、学习目标
了解安卓实现动画的三种方式
掌握如何实现四种类型的补间动画
一、安卓实现动画的三种方式
1、补间动画(tween animation)
针对同一张图片进行各种变换,比如平移、旋转、缩放、深浅等等。
2、帧式动画(frame animation)
事先准备好动画所需要的每一帧图片,然后利用线程每隔一段时间切换图片,这样来实现的动画效果。
3、属性动画(property animation)
通过动画的方式改变对象的属性。
二、补间动画类型
透明度动画(alpha animation)
缩放动画(scale animation)
平移动画(translate animation)
旋转动画(rotate animation)
三、补间动画案例 - 透明度动画
透明度动画可以通过Java代码实现,也可以通过xml配置文件实现。
(一)运行效果
在这里插入图片描述
(二)实现步骤
1、创建安卓应用【AlphaAnimation】
在这里插入图片描述
在这里插入图片描述
2、将图片素材拷贝到drawable目录
在这里插入图片描述
3、主布局资源文件activity_main.xml
在这里插入图片描述
<?xml version="1.0" encoding="utf-8"?><ImageView
android:id="@+id/ivBelle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="20dp"
android:layout_weight="7"
android:src="@drawable/belle" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btnAnimationByCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doAnimationByCode"
android:text="@string/animation_by_code" />
<Button
android:id="@+id/btnAnimationByXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doAnimationByXML"
android:text="@string/animation_by_xml" />
</LinearLayout>
4、创建透明度动画配置文件
在res目录里创建anim目录,在anim里创建alpha_animator.xml
在这里插入图片描述
duration —— 持续时间(单位:毫秒)
fromAlpha —— 起始透明度(0~1)
toAlpha —— 终止透明度(0~1)
repeatMode —— 重复模式(restart,reverse)
repeatCount —— 重复次数(1、2、3、……、infinite)
5、字符串资源文件strings.xml
在这里插入图片描述
string name=“app_name”>补间动画 - 透明度动画
通过Java代码实现动画
通过xml配置实现动画
6、主界面类 - MainActivity
在这里插入图片描述
声明变量
在这里插入图片描述
通过资源标识符获取控件实例
在这里插入图片描述
通过Java代码实现透明度动画
在这里插入图片描述
通过XML配置实现透明度动画
在这里插入图片描述
查看主界面完整源代码
package net.hw.alpha_animation;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private ImageView ivBelle; // 美女图像控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局资源文件设置用户界面
setContentView(R.layout.activity_main);
// 通过资源标识符获取控件实例
ivBelle = findViewById(R.id.ivBelle);
}
/**
* 通过Java代码实现透明度动画
*
* @param view
*/
public void doAnimationByCode(View view) {
// 创建透明度动画对象, 从不透明到透明
Animation animation = new AlphaAnimation(1, 0);
// 设置动画持续时间
animation.setDuration(3000);
// 设置动画重复模式(RESTART - 重启;REVERSE - 反向)
animation.setRepeatMode(AlphaAnimation.REVERSE);
// 设置动画重复次数
animation.setRepeatCount(1);
// 启动图像控件的透明度动画
ivBelle.startAnimation(animation);
}
/**
* 通过XML配置实现透明度动画
*
* @param view
*/
public void doAnimationByXML(View view) {
// 启动图像控件的透明度动画
ivBelle.startAnimation(AnimationUtils.loadAnimation(this, R.anim.alpha_animator));
}
}
7、启动应用,查看效果
在这里插入图片描述
四、补间动画案例 - 缩放动画
缩放动画可以通过Java代码实现,也可以通过xml配置文件实现。
(一)运行效果
在这里插入图片描述
(二)实现步骤
1、创建安卓应用【ScaleAnimation】
在这里插入图片描述
在这里插入图片描述
2、将图片素材拷贝到drawable目录
在这里插入图片描述
3、主布局资源文件activity_main.xml
在这里插入图片描述
<?xml version="1.0" encoding="utf-8"?><ImageView
android:id="@+id/ivBelle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="20dp"
android:layout_weight="7"
android:src="@drawable/belle" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btnAnimationByCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doAnimationByCode"
android:text="@string/animation_by_code" />
<Button
android:id="@+id/btnAnimationByXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doAnimationByXML"
android:text="@string/animation_by_xml" />
</LinearLayout>
4、创建缩放动画配置文件
在res目录里创建anim目录,在anim目录里创建scale_animator.xml
在这里插入图片描述
<?xml version="1.0" encoding="utf-8"?>
fromXScale —— 起始横向比例(1表示原图大小)
fromYScale —— 起始纵向比例(1表示原图大小)
toXScale —— 终止横向比例(0表示缩小到无)
toYScale —— 终止纵向比例(0表示缩小到无)
pivotX —— 缩放中心横向比例(50% - 水平居中位置)
pivotY —— 缩放中心纵向比例(50% - 垂直居中位置)
5、字符串资源文件strings.xml
在这里插入图片描述
补间动画 - 缩放动画 通过Java代码实现动画 通过xml配置实现动画6、主界面类 - MainActivity
在这里插入图片描述
声明变量
在这里插入图片描述
通过资源标识符获取控件实例
在这里插入图片描述
通过Java代码实现缩放动画
在这里插入图片描述
通过XML配置文件实现缩放动画
在这里插入图片描述
查看主界面类完整源代码
package net.hw.scale_animation;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private ImageView ivBelle; // 美女图像控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局资源文件设置用户界面
setContentView(R.layout.activity_main);
// 通过资源标识符获取控件实例
ivBelle = findViewById(R.id.ivBelle);
}
/**
* 通过Java代码实现缩放动画
*
* @param view
*/
public void doAnimationByCode(View view) {
// 创建缩放动画对象,从原尺寸到零尺寸
Animation animation = new ScaleAnimation(1, 0, 1, 0,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
// 设置动画持续时间
animation.setDuration(3000);
// 设置动画重复模式(RESTART - 重启;REVERSE - 反向)
animation.setRepeatMode(AlphaAnimation.REVERSE);
// 设置动画重复次数
animation.setRepeatCount(1);
// 启动图像控件的缩放动画
ivBelle.startAnimation(animation);
}
/**
* 通过XML配置文件实现缩放动画
*
* @param view
*/
public void doAnimationByXML(View view) {
// 启动图像控件的缩放动画
ivBelle.startAnimation(AnimationUtils.loadAnimation(this, R.anim.scale_animator));
}
}
7、启动应用,查看效果
在这里插入图片描述
8、改变缩放基准点位置
将图像控件左上角作为缩放基准点
在这里插入图片描述
启动应用,查看效果
在这里插入图片描述
五、补间动画案例 - 平移动画
(一)运行效果
在这里插入图片描述
(二)实现步骤
1、创建安卓应用【TranslateAnimation】
在这里插入图片描述
在这里插入图片描述
2、将图片素材拷贝到drawable目录
在这里插入图片描述
3、主布局资源文件activity_main.xml
在这里插入图片描述
<?xml version="1.0" encoding="utf-8"?><ImageView
android:id="@+id/ivBelle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="20dp"
android:layout_weight="7"
android:src="@drawable/belle" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btnAnimationByCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doAnimationByCode"
android:text="@string/animation_by_code" />
<Button
android:id="@+id/btnAnimationByXML"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doAnimationByXML"
android:text="@string/animation_by_xml" />
</LinearLayout>
4、创建平移动画配置文件
在res目录里创建anim目录,在anim目录里创建translate_animator.xml
在这里插入图片描述
<?xml version="1.0" encoding="utf-8"?>
5、字符串资源文件strings.xml
在这里插入图片描述
补间动画 - 平移动画 通过Java代码实现动画 通过xml配置实现动画5、主界面类 - MainActivity
在这里插入图片描述
声明变量
在这里插入图片描述
通过资源标识符获取控件实例
在这里插入图片描述
通过Java代码实现平移动画
在这里插入图片描述
通过XML动画配置文件实现平移动画
在这里插入图片描述
查看主界面完整源代码
package net.hw.translate_animation;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private ImageView ivBelle; // 美女图像控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局资源文件设置用户界面
setContentView(R.layout.activity_main);
// 通过资源标识符获取控件实例
ivBelle = findViewById(R.id.ivBelle);
}
/**
* 通过Java代码实现平移动画
*
* @param view
*/
public void doAnimationByCode(View view) {
// 创建平移动画对象,设置平移起点与终点
Animation animation = new TranslateAnimation(-730.0f, 730.0f, 0.0f, 0.0f);
// 设置动画持续时间
animation.setDuration(3000);
// 设置动画重复模式(RESTART 重启;REVERSE 反向)
animation.setRepeatMode(TranslateAnimation.REVERSE);
// 设置动画重复次数
animation.setRepeatCount(1);
// 启动图像控件的平移动画
ivBelle.startAnimation(animation);
}
/**
* 通过XML配置文件实现平移动画
*
* @param view
*/
public void doAnimationByXML(View view) {
// 启动图像控件的平移动画
ivBelle.startAnimation(AnimationUtils.loadAnimation(this, R.anim.translate_animator));
}
}
6、启动应用,查看效果
在这里插入图片描述
课堂练习:完成从左上角移到右下角的平移动画
在这里插入图片描述