Android动画

作者: jacky123 | 来源:发表于2016-06-13 09:27 被阅读320次

    相关链接


    1.视图动画

    AlphaAnimation、RotateAnimation、TranslateAnimation、ScaleAnimation继承自
    Animation。AnimationSet表示动画集合。

    动画的监听
    animation.setAnimationListener(new Animation.AnimationListener(){
        void onAnimationStart(Animation animation);
        void onAnimationEnd(Animation animation);
        void onAnimationRepeat(Animation animation);
    })
    
    加载xml动画
    Animation animation = AnimationUtils.loadAnimation(this,R.anim.abc_fade_in);
    
    代码动画
    //public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
    TranslateAnimation ta = new TranslateAnimation(0, 100, 0, 200);
    ta.setDuration(1000);
    as.addAnimation(ta);
    
    缺点

    视图动画问题:只是对View的影像做动画,并不是真正改变View的状态。有时候动画完成后View无法隐藏的现象,即 setVisibility(View.GONE)失效了,只要调用view.clearAnimation()清除view动画就可解决这个问题。


    2.帧动画(AnimationDrawable)

    在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/rocket_thrust1" android:duration="200" />
        <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
        <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
    </animation-list>
    

    注意: xml需要定义在draw目录下,否则打包时有如下错误提示:

    Element animation-list must be declared <animation-list> XML file should be in "drawable", not "anim".

    使用

    ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
    rocketImage.setBackgroundResource(R.anim.rocket_thrust);
    rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
    rocketAnimation.start();
    

    3.属性动画(Animator 框架 AnimatorSet ObjectAnimator)

    API11 android3.0 之后才有, nineoldandroids

    PropertyValuesHolder 针对作用同一个对象的多个属性
    PropertyValuesHolder pvh1 = PropertyValuesHolder.ofFloat("translationX",300f);
    PropertyValuesHolder pvh2 = PropertyValuesHolder.ofFloat("scaleX",1f,0.1f);     //缩放属性
    PropertyValuesHolder pvh3 = PropertyValuesHolder.ofFloat("scaleY",1f,0.1f);
    ObjectAnimator.ofPropertyHolder(view,pvh1,pvh2,pvh3).setDuration(1000).start();
    

    ValueAnimator
    示例1:View的显示与隐藏
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus){
            //获取要隐藏view的高度
            mHiddenViewMeasuredHeight = mHiddenView.getMeasuredHeight();
        }
        super.onWindowFocusChanged(hasFocus);
    }
    
    public void llClick(View view) {
        if (mHiddenView.getVisibility() == View.GONE) {
            // 打开动画
            animateOpen(mHiddenView);
        } else {
            // 关闭动画
            animateClose(mHiddenView);
        }
    }
    
    private void animateOpen(final View view) {
        view.setVisibility(View.VISIBLE);
        ValueAnimator animator = createDropAnimator(
                view,
                0,
                mHiddenViewMeasuredHeight);
        animator.start();
    }
    
    private void animateClose(final View view) {
        int origHeight = view.getHeight();
        ValueAnimator animator = createDropAnimator(view, origHeight, 0);
        animator.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }
        });
        animator.start();
    }
    
    private ValueAnimator createDropAnimator(
            final View view, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(
                new ValueAnimator.AnimatorUpdateListener() {
    
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int value = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams =
                        view.getLayoutParams();
                layoutParams.height = value;
                view.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }
    
    动画的监听
    ObjectAnimator anim = ObjectAnimator.ofFloat(view,"alpha",0.5f);
    anim.addListener(new AnimatorListener(){
        void onAnimationStart(Animator animation);
        void onAnimationEnd(Animator animation);
        void onAnimationCancel(Animator animation);
        void onAnimationRepeat(Animator animation);
    });
    

    也可以用 AnimatorListenerAdapter

    abstract class AnimatorListenerAdapter implements Animator.AnimatorListener,
    Animator.AnimatorPauseListener {}
    
    
    anim.addListener(new AnimatorListenerAdapter(){
        void onAnimationEnd(Animator animation){};
    });
    

    AnimatorSet 将动画以组合的形式展现出来
    示例:menu 打开关闭动画
    private void startAnim() {
        ObjectAnimator animator0 = ObjectAnimator.ofFloat(mImageViews.get(0),"alpha",1F,0.5F);
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageViews.get(1),"translationY",200F);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageViews.get(2),"translationX",200F);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageViews.get(3),"translationY",-200F);
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(mImageViews.get(4),"translationX",-200F);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(200);
        set.setInterpolator(new LinearInterpolator());
        set.playTogether(animator0,animator1,animator2,animator3,animator4);
        set.start();
    }
    private void closeAnim() {
        ObjectAnimator animator0 = ObjectAnimator.ofFloat(mImageViews.get(0),"alpha", 0.5F, 1F);
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageViews.get(1),"translationY", 200F, 0);
        ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageViews.get(2),"translationX", 200F, 0);
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageViews.get(3),"translationY", -200F, 0);
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(mImageViews.get(4),"translationX", -200F, 0);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(500);
        set.setInterpolator(new BounceInterpolator());
        set.playTogether(animator0, animator1, animator2, animator3, animator4);
        set.start();
        mFlag = true;
    }
    

    Interpolators 插值器

    TimeInterpolation: 用于定义动画变化率的接口
    系统预设的有:

    AccelerateDecelerateInterpolator(两头慢,中间快)
    LinearInterpolator(匀速)
    AccelerateInterpolator(减速)
    CycleInterpolator (正弦周期变化插值器)
    BounceInterpolator
    AccelerateDecelerateInterpolator, AccelerateInterpolator, AnticipateInterpolator, AnticipateOvershootInterpolator, BaseInterpolator, 
    CycleInterpolator, DecelerateInterpolator, FastOutLinearInInterpolator, FastOutSlowInInterpolator, LinearInterpolator, LinearOutSlowInInterpolator, OvershootInterpolator, PathInterpolator
    

    TypeEvaluator 估值器(插值器的好基友)

    TypeEvaluator: 用于定义属性值计算方式的接口,系统自带int,float,color类型。
    系统预设: IntEvaluator FloatEvaluator ArgbEvaluator

    //fraction:估值小数
    public class IntEvaluator implements TypeEvaluator<Integer> {
        public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
            int startInt = startValue;
            return (int)(startInt + fraction * (endValue - startInt));
        }
    }
    

    自定义一个贝塞尔曲线的估值器:

    class BezierEvaluator implements TypeEvaluator<PointF> {
    
        @Override
        public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
            float oneMinusT = 1.0f - fraction;
    
            //startValue;    //开始出现的点
            //endValue;      //结束终点
    
            PointF controlPoint = new PointF();    //贝塞尔曲线控制点
            controlPoint.set(mWidthPixels / 2 + 600, mHeightPixels / 2 - 300);
    
            PointF point = new PointF();    //返回计算好的点
            point.x = oneMinusT * oneMinusT * (startValue.x) + 2 * oneMinusT * fraction * (controlPoint.x) + fraction * fraction * (endValue.x);
            point.y = oneMinusT * oneMinusT * (startValue.y) + 2 * oneMinusT * fraction * (controlPoint.y) + fraction * fraction * (endValue.y);
            return point;
        }
    }
    

    View 的 animate 方法

    Android5.0后,view增加了 animate方法,供直接驱动view的属性动画。

    view.animate()
        .translationZ(15F).setDuration(300)
        .setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                view.animate()
                        .translationZ(1f)           //require api 21
                        .setDuration(500).start();
            }
        })
        .withEndAction(new Runnable() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        
                    }
                });
            }
        })
        .start();
    

    相关文章

      网友评论

        本文标题:Android动画

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