为了账号安全,请及时绑定邮箱和手机立即绑定

动画

标签:
JavaScript
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:fromXDeltaandroid:toXDeltaandroid:fromYDeltaandroid:toYDelta
<scale>android:fromXScaleandroid:toXScaleandroid:fromYScaleandroid:toYScaleandroid:pivotXandroid:pivotY
<rotate>android:fromDegreesandroid:toDegrees
<alpha>android:fromAlphaandroid:toAlphaandroid: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();



作者:James0525
链接:https://www.jianshu.com/p/e6358c09913e


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消