zoukankan      html  css  js  c++  java
  • 安卓动画学习笔记

    Animations的分类

    Animations从总体上可以分为两大类:

    1.Tweened Animations:该类Animations提供了旋转、移动、伸展和淡出等效果。

      Alpha——淡入淡出,Scale——缩放效果,Rotate——旋转,Translate——移动效果。

    2.Frame-by-frame Animations:这一类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示(多张图片在指定时间内播放完)。

    使用TweenedAnimations的步骤:

    1.创建一个AnimationSet对象(Animation子类);(可以把几个动画对象放到这个set里面)

    2.根据需要创建相应的Animation对象;

      2.1 淡入淡出 AlphaAnimation(fromAlpha, toAlpha):参数是透明度,1表示完全不透明0表示完全透明

      2.2 旋转 RotateAnimation(fromDegree, toDegree, pivotxType, pivotxValue, pivotyType, pivotyValue)

         fromDegree :0代表图片当前位置  

            pivotxType: x轴坐标的类型

            Absoulte

            relative_to_self:(pivotxValue 0.5f 表示控件自己宽度一半的位置),

              relative_to_parent:父控件为参考值:0.5f表示父控件宽度一半的位置

      2.3 缩放效果 ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue

         fromX, toX: x轴从多大(fromX*self. 1.0*self.width)缩放到多大(toX*self.height 0.5*self.height) , y轴同理

         pivotXType:absoulate, self, parent

         pivotXValue:缩放的x轴的值:0.5f 表示self or parent一半的width, pivotYValue同理

          2.4 移动效果 TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

        fromx, tox: x轴从(parent or self or absloute) fromx*(self or parent) 移动 tox*(self or parent), y轴同理

    <!-- 左进右出 :slide_in_left,slide_out_right:-100->0, 0->100  -->
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime"/>
    </set>
    
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="0%p" android:toXDelta="100%p"android:duration="@android:integer/config_mediumAnimTime"/>
    </set>
    
    
    <!-- 右进左出 :slide_in_right,slide_out_left:100->0, 0->-100  -->
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="@android:integer/config_mediumAnimTime"/>
    </set>

    <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="-100%p"android:duration="@android:integer/config_mediumAnimTime"/> </set>
    AlphaAnimation alphaAnimation = new AlphaAnimation(tmpAlpha[0], tmpAlpha[1]);
            alphaAnimation.setFillAfter(true);
            alphaAnimation.setDuration(duration);
            alphaAnimation.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    bgGrayView.setVisibility(View.VISIBLE);
                }
                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
                }
                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub
                }
            });
    listView.startAnimation(alphaAnimation);

    3.根据动画的需求,为Animation对象设置相应的数据;

    4.Animatin对象添加到AnimationSet对象当中;

    5.使用控件对象开始执行AnimationSet

    6.如果要设置动画的xml文件,对于interpolator这个属性,必须要设置在<set>里面才可以生效<set android:interpolator="@android:anim/bounce_interpolator"></set>

    @android:anim/accelerate_interpolator: 越来越快
    @android:anim/decelerate_interpolator:越来越慢
    @android:anim/accelerate_decelerate_interpolator:先快后慢
    @android:anim/anticipate_interpolator: 先后退一小步然后向前加速
    @android:anim/overshoot_interpolator:快速到达终点超出一小步然后回到终点
    @android:anim/anticipate_overshoot_interpolator:到达终点超出一小步然后回到终点
    @android:anim/bounce_interpolator:到达终点产生弹球效果,弹几下回到终点
    @android:anim/linear_interpolator:均匀速度

     7.Animation和Visible的问题

    在做一个功能,底部有个Bar,一点击就从底部升上来,再点击就消失,并在onAnimationEnd里visible设为gone,但是发现,这个gone没有效果,这个bar竟然还在响应点击事件。
    上网一查,发现要清除动画,即在onAnimationEnd里调用view.clearAnimation();即可,目前来看不用设置gone

     8.AnimatorSet有序或者同时运行多个动画代码如下

    AnimatorSet animSet = new AnimatorSet();
    //view是要被动画的对象
    ObjectAnimator startAlpha = ObjectAnimator.ofFloat(view, "alpha", 1.0f, 1.0f); 
    ObjectAnimator endAlpha = ObjectAnimator.ofFloat(view, "alpha", 1f, 0.0f); 
    startAlpha.setDuration(500); 
    endAlpha.setDuration(1000);
    //在endAlpha前先播放startAlpha的动画
    animSet.play(startAlpha).before(endAlpha);
    //同时播放
    animSet.play(anim1).with(anim2);
     animSet.start();
    
    /**
    先播放anim1,再同时播放anim2,anim3,anim4,最后播anim5
    animSet.play(anim1).before(anim2);
    .play(anim2).with(anim3);
    .play(anim2).with(anim4);
    .play(anim5).after(amin2);
    **/

     9.view本来隐藏状态,想通过动画显示,第一次动画无效:该情况是,view的visiblity是gone,应该设为invisible,这样第一次动画才会生效

  • 相关阅读:
    python 并发编程 多线程 event
    python 并发编程 多线程 定时器
    python 并发编程 多线程 信号量
    linux top 查看CPU命令
    python 并发编程 多线程 GIL与多线程
    python 并发编程 多线程 死锁现象与递归锁
    python 并发编程 多线程 GIL与Lock
    python GIL全局解释器锁与互斥锁 目录
    python 并发编程 多线程 GIL全局解释器锁基本概念
    执行python程序 出现三部曲
  • 原文地址:https://www.cnblogs.com/baron89/p/3616589.html
Copyright © 2011-2022 走看看