动画Animation

简介

动画可以添加视觉提示,通知用户您的应用中发生了什么。 当UI更改状态时,例如新内容加载或新操作可用时,它们尤其有用。 动画还为您的应用添加了抛光外观,使其具有更高品质的外观和感觉。

Android包含不同的动画API,具体取决于您想要的动画类型,因此该页面概述了向UI添加动画的不同方法。

动画的分类

View Animation
Drawable Animation

Android中提供了两种实现View动画的方式

纯编码的方式
Xml配置的方式

Animation的常用API

setDuration(long durationMillis) : 设置持续时间(单位ms)
setStartOffset(long startOffset) : 设置开始的延迟的时间(单位ms)
setFillBefore(boolean fillBefore) : 设置最终是否固定在起始状态
setFillAfter(boolean fillAfter) : 设置最终是否固定在最后的状态
setAnimationListener(AnimationListener listener) : 设置动画监听
坐标类型:
Animation.ABSOLUTE
Animation.RELATIVE_TO_SELF
Animation.RELATIVE_TO_PARENT

启动动画 : view.startAnimation(animation);
结束动画: view.clearAnimation()
动画监听器 : AnimationListener
onAnimationStart(Animation animation) : 动画开始的回调
onAnimationEnd(Animation animation) : 动画结束的回调
onAnimationRepeat(Animation animation) : 动画重复执行

单一动画(Animation)

缩放动画(ScaleAnimation)

code方式实现:

ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
fromX : 开始时X轴上的缩放比例
toX : 结束时X轴上的缩放比例
fromY :开始时Y轴上的缩放比例
toY :结束时Y轴上的缩放比例

pivotXType : X轴坐标的类型(计算x轴上的偏移量的方式)
pivotXVlaue : 中心点在X轴相对视图左顶点在x轴上的偏移量
pivotYType : Y轴坐标的类型(计算x轴上的偏移量的方式)
pivotYValue : 中心点相对视图左顶点在y轴上的偏移量
示例代码:

//1. 创建动画对象
ScaleAnimation animation = new ScaleAnimation(0.5f, 1.5f, 0, 1,
        Animation.ABSOLUTE, view.getWidth() / 2, Animation.ABSOLUTE, 0);
animation = new ScaleAnimation(0.5f, 1.5f, 0, 1,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0);
//2. 设置
//延迟1s开始
animation.setStartOffset(1000);
//持续2s
animation.setDuration(2000);
//最终还原
animation.setFillBefore(true);
//3. 启动动画
view.startAnimation(animation);

xml方式实现

一,定义XML动画文件
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:pivotX="1"
    android:pivotY="0.5"
    android:toXScale="1.0"
    android:toYScale="1.0"
    android:fillAfter="true"/>
    
//2. 加载动画文件得到动画对象
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim);
//3. 启动动画
view.startAnimation(animation);

pivotX、pivotY 坐标类型 Type :
Animation.ABSOLUTE :
数值(默认以px为单位) 100
Animation.RELATIVE_TO_SELF :
百分数,如:50% (以当前视图的宽度或高度其为基数来计算)
Animation.RELATIVE_TO_PARENT :
百分数+p,如:50%p (以父视图的宽度或高度其为基数来计算)

旋转动画(RotateAnimation)

Code实现

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
fromDegrees : 开始时的角度
toDegrees : 结束时的角度

pivotXType : X轴坐标的类型
pivotXVlaue : X轴坐标的值
pivotYType : Y轴坐标的类型
pivotYValue : Y轴坐标的值
示例代码:

//1. 创建动画对象
RotateAnimation animation = new RotateAnimation(-90, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//2. 设置
animation.setDuration(5000);
//3. 启动动画
view.startAnimation(animation);

xml实现

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:fromDegrees="+90"
    android:toDegrees="-90"
    android:pivotX="0%"
    android:pivotY="0%" />
使用:
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim);
view.startAnimation(animation);

透明度动画(AlphaAnimation)

code实现

AlphaAnimation(float fromAlpha, float toAlpha)
fromAlpha : 开始时的缩放比例
toAlpha : 结束时的缩放比例
示例:

//1. 创建动画对象
AlphaAnimation animation = new AlphaAnimation(0, 1);
// 2. 设置
animation.setDuration(4000);
// 3. 启动动画
view.startAnimation(animation);

XML实现

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="1000" />

平移动画(TranslateAnimation)

code实现:

TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
fromXType : 坐标类型
fromXValue : 开始时X轴的坐标
toXType :坐标类型
toXValue : 结束时X轴的坐标
fromYType :坐标类型
fromYValue : 开始时Y轴的坐标
toYType :坐标类型
toYValue : 结束时Y轴的坐标
示例:

//1. 创建动画对象
TranslateAnimation animation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.RELATIVE_TO_SELF, 1, Animation.ABSOLUTE, 0, Animation.RELATIVE_TO_SELF, 1);
//2. 设置
animation.setDuration(2000);
//3. 启动动画
view.startAnimation(animation);

xml实现

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXDelta="-100%p"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="0%" />

复合动画(AnimationSet)

多个单一动画组合在一起的动画

code实现

//1. 创建透明动画并设置
AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
alphaAnimation.setDuration(2000);
//2. 创建旋转动画并设置
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
rotateAnimation.setStartOffset(2000);//延迟
//3. 创建复合动画对象
AnimationSet animationSet = new AnimationSet(true);
//4. 添加两个动画
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
//5. 启动复合动画对象
view.startAnimation(animationSet);

xml实现

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
</set>

动画的监听

示例代码:

Animation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1000);
//设置线性变化
animation.setInterpolator(new LinearInterpolator());
//设置动画重复次数
animation.setRepeatCount(Animation.INFINITE);
//设置动画监听
animation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        Log.e("TAG", "动画开始");
    }
    @Override
    public void onAnimationEnd(Animation animation) {
        Log.e("TAG", "动画结束");
    }
    @Override
    public void onAnimationRepeat(Animation animation) {
        Log.e("TAG", "动画重复");
    }
});
view.startAnimation(animation);

Interpolator 设置变化速率:

Interpolator 被用来修饰动画效果,定义动画的变化率,可以使存在的动画效果accelerated(加速),decelerated(减速),repeated(重复)等。

@android:anim/linear_interpolator  : 线性变化

@android:anim/accelerate_interpolator  : 加速变化

@android:anim/decelerate_interpolator  : 减速变化

@android:anim/cycle_interpolator : 周期循环变化

Drawable Animation

定义资源文件:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/nv1" android:duration="500" />
<item android:drawable="@drawable/nv2" android:duration="500" />
...
</animation-list>

控件使用:
<ImageView
    ...
    android:background="@drawable/anim_da" />
    
启动、停止动画

//得到背景动画图上
AnimationDrawable ad = (AnimationDrawable) imageView.getBackground();
//启动动画
ad.start();
//停止动画
ad.stop();

界面动画

自定义方式:

进入界面在oncreate方法中进行动画操作(控制rootView),并设置动画监听

系统API方式

overridePendingTransition(int enterAnim, int exitAnim)
enterAnim : 将要显示的新界面进入动画
exitAnim : 当前界面退出的动画
示例:

开始动画
startActivity(new Intent(this, SetupGuide3Activity.class));
overridePendingTransition(R.anim.right_in, R.anim.left_out);

结束动画
finish();
overridePendingTransition(R.anim.left_in, R.anim.right_out);

自定义进度条

环形进度条自定义
<ProgressBar
android:indeterminateDrawable="@anim/image_progress" //需要的动画
android:indeterminateDuration="500"/>//持续时间

指定动画的图片
<rotate android:drawable="@drawable/progess"/>

水平进度条自定义
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 指定背景图片 id为background -->
    <item android:id="@android:id/background"
        android:drawable="@drawable/security_progress_bg"/>
    <!-- 指定宽度变化的进度图片 id为progress -->
    <item android:id="@android:id/progress"
         android:drawable="@drawable/security_progress"/>
</layer-list>

使用
<ProgressBar
...
android:progressDrawable="@drawable/my_progress"/>

猜你喜欢

转载自blog.csdn.net/lijianbiao0/article/details/85596968