Android动画基础
添加链接描述
动画简介
什么是动画
当我们的应用界面发生变化时,比如添加一个窗口,跳转到新的Activity,新启动一个应用,这些界面的切换都需要一个过渡效果来保证流畅性,此时动画就能达到这些效果。
Android为我们定义了诸多动画属性来达到相应的动画效果,简单介绍下以下属性的含义。
interpolator:用来定义插值器,插值器会根据当前动画执行的时间进度来计算整个动画的执行进度
duration:用来定义动画播放的总时长
fromXScale:用来定义动画开始时x轴的缩放比例,为1代表原图大小
toXScale:用来定义动画结束时x轴的缩放比例
pivotX:用来定义x轴缩放位置的中心点
pivotY:用来定义y轴缩放位置的中心点
动画分类
我们在使用手机时发现,在桌面点击启动APP,下拉通知栏,解锁,应用内界面切换,进入多任务列表等场景下,都可以看到动画效果,那这些动画都是一样的吗?
虽然这些动画效果看起来基本一样,但是在安卓框架中,这些动画的执行路径(开发者把动画理解成一套流程,包括动画条件的准备,动画的触发,动画资源的准备以及动画的播放)存在区别。
我们按照动画的执行路径,将Android动画大致分为:窗口动画,过渡动画以及View动画三类。
添加链接描述
窗口动画实战
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="WindowAnim" mce_bogus="1" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/enter_anim</item>
<item name="android:windowExitAnimation">@anim/exit_anim</item>
</style>
</resources>
enter_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="2000"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="0%"
android:pivotY="100%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
exit_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="500"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="0%"
android:pivotY="100%"
android:toXScale="1.0"
android:toYScale="0.0" />
</set>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.windowdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = new Button(getBaseContext());
button.setText("this is a window");
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.width = 500;
layoutParams.height = 2000;
layoutParams.y = 100;
layoutParams.x = 50;
layoutParams.windowAnimations = R.style.WindowAnim;
Button button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WindowManager wm = getWindowManager();
wm.addView(button, layoutParams);
}
});
}
}
过渡动画实战
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstActivity">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.windowdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
Button button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, MainActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.enter_anim, R.anim.exit_anim);
}
});
}
}
窗口动画
通过将自定义的动画资源赋值给布局参数Layoutparams.windowAnimation,从而实现了相应的窗口动画效果,接下来我们将学习在Framework层是如何处理窗口动画资源,并将动画执行起来的。
窗口动画相关的类图
本文介绍了Android动画的基础知识,包括动画的定义、插值器、时长等属性。详细讲解了窗口动画和过渡动画的分类,并通过实战示例展示了如何在Android应用中实现窗口动画,提供styles.xml、enter_anim.xml、exit_anim.xml等相关代码片段。

1854

被折叠的 条评论
为什么被折叠?



