动画

作者: android小菜鸡一枚 | 来源:发表于2018-08-09 14:30 被阅读0次
View动画

View动画的作用对象是View,它支持4种动画效果,分别是平移动画,缩放动画,旋转动画和透明度动画。
平移动画 <translate> TranslateAnimation 移动View
缩放动画 <scale> ScaleAnimation 方法或缩小View
旋转动画 <rotate> RotateAnimation 旋转View
透明度动画 <alpha> AlphaAnimation 改变View的透明度
创建动画的XML文件,文件路径为:res/anim/filename

android:interpolator 插值器,默认为@android:anim/accelerate_decelerate_interpolator加速减速插值器
android:shareInterpolator 集合中的动画是否和集合共享同一个插值器
<translate>
android:fromXDelta
android:toXDelta
android:fromYDelta
android:toYDelta
<scale>
android:fromXScale
android:toXScale
android:fromYScale
android:toYScale
android:pivotX
android:pivotY
<rotate>
android:fromDegrees
android:toDegrees
<alpha>
android:fromAlpha
android:toAlpha

android:duration 动画持续的时间
android:fillAfter 动画结束以后View是否停留在结束位置,true表示View停留在结束位置。

Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_test);
mButton.startAnimation(animation);
//通过代码来应用动画
AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
alphaAnimation.setDuration(300);
mButton.startAnimation(alphaAnimation);

通过Animation的setAnimationListener方法可以给View动画添加过程监听,接口如下

public static interface AnimationListener{
    void onAnimationStart(Animation animation);
    void onAnimationEnd(Animation animation);
    void onAnimationRepeat(Animation animation);
}
自定义View动画

需要继承Animation这个抽象类,然后重写它的initialize和applyTransformation方法。自定义动画的过程主要是矩阵变换的过程。

帧动画

帧动画是顺序播放一组预先定义好的图片,系统提供了一个类AnimationDrawable来使用帧动画;
在res/drawable/frame_animation.xml

<animation-list>
  <item android:drawable="" android:duration="">
</animation-list>

mButton.setBackgroundResource(R.drawable.frame_animation);
AnimationDrawable drawable = (AnimationDrawable)mButton.getBackground();
drawable.start();

帧动画使用比较简单,但是比较容易引起OOM,尽量避免使用过多尺寸较大的图片。

View动画的特殊使用场景

LayoutAnimation
作用于ViewGroup,为ViewGroup指定一个动画,这样当它的子元素出场时都会具有这种动画效果。

<layoutAnimation>
android:delay
android:animationOrder : 子元素动画的顺序,normal,reverse和random
android:animation 为子元素指定入场动画
//为ViewGroup指定android:layoutAnimation="@anim/anim_layout"

//通过代码实现
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_item);
LayoutAnimationController controller = new LayoutAnimationController(animaton);
controller.setDelay(0.5f);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(controller);
Activity的切换效果

用到overridePendingTransition(int enterAnim, int exitAnim)方法,这个方法必须在startActivity或者finish方法之后被调用才能生效。

属性动画

属性动画可以对任意对象的属性进行动画不仅仅是View,可以达到的效果是:在一个时间间隔内完成对象从一个属性值到另一个属性值的改变。从API11才有,采用开源动画库nineoldandroids来兼容以前的版本。
可以通过XML来定义,定义在res/animator/目录下

<objectAnimator> 对应ObjectAnimator
android:propertyName 属性名
android:duration
android:valueFrom
android:valueTo
android:startOffset 动画的延迟时间
android:repeatCount 动画的重复次数 默认为0,-1为无限循环
android:repeatMode 动画的重复模式 有repeat和reverse表示连续重复和逆向重复
android:valueType 表示android:propertyName所指定的属性的类型,如intType,floatType分别表示属性的类型为整型和浮点型。
<animator> 对应ValueAnimator
<set> 对应AnimatorSet
android:ordering 有together和sequentially

AnimationSet set = (AnimationSet) AnimationInflator.loadAnimator(myContext, R.anim.property_animator);
set.setTarget(mButton);
set.start();
插值器和估值器

LinearInterpolator 线性插值器
AccelerateDecelerateInterpolator 加速减速插值器
DecelerateInterpolator 减速插值器
TypeEvaluator 类型估值算法(估值器)
IntEvaluator整型 FloatEvaluator浮点型 ArgbEvaluator针对Color属性
属性动画要求对象的该属性有set方法和get方法(可选)

属性动画监听器

主要有两个接口:AnimatorUpdateListener和AnimatorListener

//它可以监听动画的开始,结束,取消以及重复播放
public static interface AnimatorListener{
    void onAnimationStart(Animator animator);
    void onAnimationEnd(Animator animator);
    void onAnimatonCancel(Animator animator);
    void onAnimationRepeat(Animator animator);
}
系统还提供了AnimationListenerAdapter这个类,它是AnimatorListener的适配器类,可以有选择性实现上面的4个方法。

//会监听整个动画过程,动画由许多帧组成,没播放一帧,onAnimationUpdate就会被调用一次。
public static interface AnimationUpdateListener {
    void onAnimationUpdate(ValueAnimator animation);
}

1.给你的对象加上get和set方法,如果有权限的话。
2.用一个类来包装原始对象,间接为其提供get和set方法

ViewWrapper wrapper = new ViewWrapper(mButton);
ObjectAnimator.ofInt(wrapper,"width",500).setDuration(5000).start();

private static class ViewWrapper {
    private View mTarget;
    public ViewWrapper(View target){
        mTarget = target;
    }
    public int getWidth(){
        return mTarget.getLayoutParams().width();
    }
    public void setWidth(int width){
        mTarget.getLayoutParams().width = width;
        mTarget.requestLayout();
    }
}

3.采用ValueAnimator,监听动画过程,自己实现属性的改变

属性动画的工作原理

属性动画要求动画作用的对象提供该属性的set方法,属性动画根据你传递的该属性的初始值和最终值,以动画的效果多次去调用set方法。ObjectAnimator继承了ValueAnimator
ObjectAnimator.ofInt(mButton,"width",500).setDuration(5000).start();


《Android开发艺术探讨》Android动画深入分析
Android技能树 — 动画小结
属性动画(Property Animation)
HenCoder Android 自定义 View 1-6: 属性动画(上手篇)
HenCoder Android 开发进阶 自定义 View 1-7:属性动画(进阶篇)
过渡(Transition)
Android 过渡(Transition)动画解析之基础篇
酷炫的Activity切换动画,打造更好的用户体验

相关文章

  • Android回顾--(十六) 动画简析

    动画: 补间动画(Tween动画) 帧动画(Frame动画) 属性动画(Property动画) 补间动画 特点: ...

  • 在山西太原,做个二维动画需要哪些制作流程?

    二维动画有哪些类型? flash动画,课件动画,mg动画,ae动画,GIF动画,手绘动画,网页动画,企业动画,宣传...

  • Android 动画

    【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...

  • 动画学习

    动画 分为 组动画,属性动画,渐变动画,其中属性动画包括 普通动画和关键帧动画,其他动弹动画,动画层分为 pres...

  • Android动画

    Android动画分类: 视图动画:补间动画、逐帧动画 属性动画 视图动画 补间动画 可以在xml中定义动画,然后...

  • iOS动画

    iOS动画-从UIView动画说起iOS动画-Transform和KeyFrame动画iOS动画-layout动画...

  • Android动画之视图动画

    分类 Android动画主要包括视图动画和属性动画。视图动画包括Tween动画和Frame动画。Tween动画又包...

  • Android 动画

    android动画分为三种 帧动画,视图动画(补间动画),属性动画逐帧动画 视图动画 属性动画 Window和A...

  • android动画

    动画: 分类:分为视图动画和属性动画,其中视图动画又分为补间动画和逐帧动画。补间动画又分为平移动画、缩放动画、旋转...

  • Android中的动画概述

    动画可以分为三类:View动画,帧动画,属性动画。 一、View动画 1.View动画包括四种:平移动画,缩放动画...

网友评论

    本文标题:动画

    本文链接:https://www.haomeiwen.com/subject/zvsjpftx.html