目录
第一章:动画基础
动画的意义:
- 视觉效果:优雅的动画能大幅度的提升用户的观感,给用户带来良好的视觉感受。
- 引导用户:好的动画能够合理吸引用户的注意力,起到引导用户的作用,让他们更轻松的理解我们应用的行为。
动画的分类:
- 逐帧动画:逐帧动画的基础是帧,也就是图片,这些图片一般由公司的美工来制作,没有原图就无法定义逐帧动画,应用的范围也就比较小。
- 视图动画(补间动画Tween Animation)系统:视图动画系统操作的是视图对象,可以让它们产生透明度渐变、位移、旋转等效果,视图动画系统已经可以定义比较丰富的效果,但是它也有局限性,通过对视图动画的学习,我们会掌握动画的一些基本属性。
- 属性动画系统:属性动画系统操作的对象不再局限于视图,而且它可以真实的改变对象的属性。在Android 3.0的(API级别11)引入。
第一节:逐帧动画
一、逐帧动画定义:
逐帧动画也叫图片动画,通过在一个固定区域一张一张的加载事先准备好的图片而产生动画效果。
二、定义帧动画的方法:
在Android平台使用AnimationDrawable对象来定义逐帧动画,它是一个Drawable的容器,和普通的Drawable对象一样,它可以设置为视图对象的背景。
最简单的定义帧动画的方法就是在XML文件中通过<animation-list>来定义AnimationDrawable对象。
三、逐帧动画实例:
1、在drawable目录中创建一个Drawable类型资源文件loading.xml。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"><!--只播放一次-->
<item
android:drawable="@drawable/frame_1"
android:duration="100"/>
<item
android:drawable="@drawable/frame_2"
android:duration="100"/>
<item
android:drawable="@drawable/frame_3"
android:duration="100"/>
</animation-list>
2、在布局文件中创建View对象,背景设置为loading.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<View
android:id="@+id/view"
android:background="@drawable/loading"
android:layout_centerInParent="true"
android:layout_width="300dp"
android:layout_height="300dp"/>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/start_btn"
android:text="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/stop_btn"
android:text="Stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
3、在代码中创建AnimationDrawable对象,并初始化为View对象的背景。通过Animation的start方法播放动画,stop方法停止动画,setOneShot(true)方法可以设置只播放一次。
package com.administrator.animationdrawable;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private View mView;
private AnimationDrawable mAnimationDrawable;
private Button mBtnStart;
private Button mBtnStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//添加监听器
mBtnStart.setOnClickListener(this);
mBtnStop.setOnClickListener(this);
}
private void initView() {
mView = findViewById(R.id.view);
mBtnStart = findViewById(R.id.start_btn);
mBtnStop = findViewById(R.id.stop_btn);
mAnimationDrawable = (AnimationDrawable) mView.getBackground();
mAnimationDrawable.setOneShot(true);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.start_btn:
mAnimationDrawable.start();
break;
case R.id.stop_btn:
mAnimationDrawable.stop();
break;
}
}
}
第二节:视图动画系统
视图动画系统操作的是Android中的视图对象,简单说通过视图动画可以让Button、TextView、ImageView等动起来。
视图动画系统相关的类都定义在android.view.animation包中,其中的Animation类,它有一些子类AlphaAnimation、ScaleAnimation、TranslateAnimation、RotateAnimation、AnimationSet。
视图对象系统能够对视图对象展示补间动画(补间动画:提供起点、终点和时长,自动完成中间过程)。
详情参考:https://blog.youkuaiyun.com/pzm1993/article/details/77167049
一、透明度动画AlphaAnimation
可以在资源文件中定义透明度动画,也可以在java文件中定义。在资源文件中通过<alpha>标签定义动画,每个标签都对应一个AlphaAnimation对象。
1、在res中创建anim目录,在anim目录中创建一个透明度动画资源文件alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"><!--持续时间-->
<alpha
android:fromAlpha="1.0"<!--开始时透明度-->
android:toAlpha="0.1"/><!--结束时透明度-->
</set>
2、在java文件中获取动画对象,并在目标对象上应用。
//获取透明度对象
Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha);
//为对象设置透明度动画,如Button、TextView等
view.startAnimation(alphaAnimation);
提示:xml标签的属性可以快速提取:Refator->Extract->Style...
二、缩放动画ScaleAnimation
可以在X轴或Y轴移动动画,同透明度动画一样,在资源文件中每个<scale>标签对应一个ScaleAnimation对象。
<scale>标签常用属性
- android:fromXScale 起始时X方向上相对自身的缩放比例,浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍;
- android:toXScale 结束时X方向上相对自身的缩放比例,浮点值;
- android:fromYScale 起始时Y方向上相对自身的缩放比例,浮点值,
- android:toYScale 结束时Y方向上相对自身的缩放比例,浮点值;
- android:pivotX X轴的基准点,可以是数值、百分数、百分数p 三种样式,比如 20、20%、20%p,当值为数值时,表示在当前视图的左边,20表示视图基准点向右50px做为X轴基准点;当值为20%时,表示在当前基准点加上自身宽度的20%做为起始点;如果是20%p,表示当前基准点加上父控件宽度的20%做为起始点。
- android:pivotY Y轴的基准点,取值及意义跟android:pivotX一样。
- android:fillAfter 结束时保持状态,取值true、false,一般用在上一级标签中
- android:fillBefore 结束时保持状态,取值true、false,一般用在上一级标签中
- android:duration 动画持续时间
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"><!--动画结束后保持状态-->
<scale
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="1.0"
android:pivotX="40%p"
android:pivotY="0"/>
</set>