美文网首页
Animation随记

Animation随记

作者: 任天一 | 来源:发表于2019-03-26 18:54 被阅读0次
    Animation是所有动画(scale,alpha,translate,rotate)的基类,它所具有的标签和对应函数为:
    android:duration         setDuration(long) 动画持续时间,以毫秒为单位
    android:fillAfter        setFillAfter(boolean) 如果设置为true,控件动画结束时,将保持动画最后时的状态
    android:fillBefore        setFillBefore(boolean)    如果设置为true,控件动画结束时,还原到开始动画前的状态
    android:fillEnabled       setFillEnabled(boolean)   与android:fillBefore 效果相同,都是在动画结束时,将控件还原到初始化状态
    android:repeatCount       setRepeatCount(int)   重复次数
    android:repeatMode        setRepeatMode(int)    重复类型,有reverse和restart两个值,取值为RESTART或 REVERSE,必须与repeatCount一起使用才能看到效果。因为这里的意义是重复的类型,即回放时的动作。
    android:interpolator      setInterpolator(Interpolator) 设定插值器,其实就是指定的动作效果,比如弹跳效果等
    

    最终实现的小球效果:

    QQ图片20190328172931.gif

    Evaluator 加速器

    public class ArgbEvaluator implements TypeEvaluator {
        @Override
        public Object evaluate(float fraction, Object startValue, Object endValue) {
            int startInt = (Integer) startValue;
            int startA = (startInt >> 24)& 0xff;
            int startR = (startInt >> 16) & 0xff;
            int startG = (startInt >> 8) & 0xff;
            int startB = startInt & 0xff;
    
            int endInt = (Integer) endValue;
            int endA = (endInt >> 24)& 0xff;
            int endR = (endInt >> 16) & 0xff;
            int endG = (endInt >> 8) & 0xff;
            int endB = endInt & 0xff;
    
            return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
                    (int)((startR + (int)(fraction * (endR - startR))) << 16) |
                    (int)((startG + (int)(fraction * (endG - startG))) << 8) |
                    (int)((startB + (int)(fraction * (endB - startB))));
        }
    }
    
    public class PointEvaluator implements TypeEvaluator<Point> {
        @Override
        public Point evaluate(float fraction, Point startValue, Point endValue) {
            int startRadius = startValue.getRadius();
            int endRadius = endValue.getRadius();
            int resultRadius = (int) (startRadius + fraction * (endRadius - startRadius));
            return new Point(resultRadius);
        }
    }
    

    ValueAnimator与ObjectAnimator

      ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator(), new Point(20), new Point(200));
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mCurrentPoint = (Point) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
    
            valueAnimator.setDuration(1000);
            valueAnimator.setInterpolator(new BounceInterpolator());
            valueAnimator.start();
    
      ObjectAnimator animator = ObjectAnimator.ofInt(acv,"PointRadius",0,300,100,200);
            animator.setDuration(3000);
            animator.start();
    
            ObjectAnimator animator2 = ObjectAnimator.ofInt(acv, "CircleBackgroundColor", 0xffff00ff, 0xffffff00, 0xffff00ff);
            animator2.setDuration(3000);
            animator2.setEvaluator(new ArgbEvaluator());
            animator2.start();
    
    public class AnimationCircleView extends View {
    
        private Context mContext;
        private Paint mPaint;
        private Point mCurrentPoint = new Point(100);
        private int widthSize;
        private int heightSize;
        private int color = getResources().getColor(R.color.color_4fd8f5);
    
    
        public AnimationCircleView(Context context) {
            this(context, null);
        }
    
        public AnimationCircleView(Context context, @Nullable AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public AnimationCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            this.mContext = context;
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            widthSize = MeasureSpec.getSize(widthMeasureSpec);
            heightSize = getMeasuredHeight();
            if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
                heightSize = widthSize;
            } else if (widthMode == MeasureSpec.EXACTLY) {
    
            }
    
            setMeasuredDimension(widthSize, heightSize);
    
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if (mCurrentPoint != null) {
                mPaint = new Paint();
                mPaint.setAntiAlias(true);
                mPaint.setColor(color);
                mPaint.setStyle(Paint.Style.FILL);
                mPaint.setDither(true);
                canvas.drawCircle(widthSize/2, heightSize/2, mCurrentPoint.getRadius(), mPaint);
            }
        }
    
        void setPointRadius(int radius){
            mCurrentPoint.setRadius(radius);
            invalidate();
        }
    
        void setCircleBackgroundColor(int color){
            this.color = color;
            invalidate();
        }
    
    //    public void doAnimation() {
    //        ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator(), new Point(20), new Point(200));
    //        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    //            @Override
    //            public void onAnimationUpdate(ValueAnimator animation) {
    //                mCurrentPoint = (Point) animation.getAnimatedValue();
    //                postInvalidate();
    //            }
    //        });
    //
    //        valueAnimator.setDuration(1000);
    //        valueAnimator.setInterpolator(new BounceInterpolator());
    //        valueAnimator.start();
    //    }
    }
    

    相关文章

      网友评论

          本文标题:Animation随记

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