小言_互联网的博客

Android旋转,扩散聚合,水波纹动画

413人阅读  评论(0)

实现效果:

需要注意的点:

Paint常用的Flag:

  • ANTI_ALIAS_FLAG:绘制时可以实现抗锯齿

  • DITHER_FLAG:使位图进行有利的抖动

  • EMBEDDED_BITMAP_TEXT_FLAG:可以在绘制文本时使用位图字体

  • FAKE_BOLD_TEXT_FLAG:绘制文本应用合成加粗效果

  • FILTER_BITMAP_FLAG:可在缩放位图上实现双线性采样

  • LINEAR_TEXT_FLAG:使文本能够平滑线性缩放

  • STRIKE_THRU_TEXT_FLAG:使用透油装饰绘制文本

  • SUBPIXEL_TEXT_FLAG:使文本的子像素定位

  • UNDERLINE_TEXT_FLAG:绘制文本下划线

Paint.Style:

Style指定画笔绘制图形时的样式:只绘制图形轮廓(描边),只绘制图形内容 或者既绘制轮廓也绘制内容。

  • STROKE:只绘制图形轮廓(描边

  • FILL:只绘制图形内容,不绘制轮廓

  • FILL_AND_STROKE:既绘制轮廓也绘制内容

用到的插值器:

  • LinearInterpolator :以常量速率变化

  • OvershootInterpolator: 向前甩一定值后再回到原来位置

实现流程:

1.初始化旋转圆和扩散圆的画笔,圆心位置

    private void init(Context context) {
        //旋转圆的画笔
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //扩散圆的画笔
        mHolePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mHolePaint.setStyle(Paint.Style.STROKE);
        mHolePaint.setColor(mBackgroundColor);
​
        mCircleColors = context.getResources().getIntArray(R.array.splash_circle_colors);
    }
​
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCenterX = w * 1f / 2;
        mCenterY = h * 1f / 2;
        mDistance = (float) (Math.hypot(w, h) / 2);
    }

2.新建一个抽象状态类,其后的旋转状态,扩散聚合状态,水波纹状态都继承这个类

    private abstract class SplashState{
        abstract void drawState(Canvas canvas);
    }

3.旋转效果实现

    private class RotateState extends SplashState{
​
        private RotateState(){
            mValueAnimator = ValueAnimator.ofFloat(0, (float) (Math.PI *2));
            mValueAnimator.setRepeatCount(2);
            mValueAnimator.setDuration(mRotateDuration);
            mValueAnimator.setInterpolator(new LinearInterpolator());
            mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mCurrentRotateAngle = (float) animation.getAnimatedValue();
                    invalidate();
                }
            });
            mValueAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    mState = new MerginState();
                }
            });
            mValueAnimator.start();
        }
​
        @Override
        void drawState(Canvas canvas) {
            //绘制背景
            drawBackground(canvas);
            //绘制6个小球
            drawCircles(canvas);
        }
    }

4.扩散聚合效果实现

    private class MerginState extends SplashState{
​
        private MerginState(){
            mValueAnimator = ValueAnimator.ofFloat(mCircleRadius, mRotateRadius);
            mValueAnimator.setDuration(mRotateDuration);
            mValueAnimator.setInterpolator(new OvershootInterpolator(10f));
            mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mCurrentRotateRadius = (float) animation.getAnimatedValue();
                    invalidate();
                }
            });
            mValueAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    mState = new ExpandState();
                }
            });
            mValueAnimator.reverse();
        }
​
        @Override
        void drawState(Canvas canvas) {
            drawBackground(canvas);
            drawCircles(canvas);
        }
    }

5.水波纹效果实现

    private class ExpandState extends SplashState{
​
        public ExpandState() {
            mValueAnimator = ValueAnimator.ofFloat(mCircleRadius, mDistance);
            mValueAnimator.setDuration(mRotateDuration);
            mValueAnimator.setInterpolator(new LinearInterpolator());
            mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mCurrentHoleRadius = (float) animation.getAnimatedValue();
                    invalidate();
                }
            });
​
            mValueAnimator.start();
        }
​
        @Override
        void drawState(Canvas canvas) {
            drawBackground(canvas);
        }
    }

demo地址:https://github.com/Adolphsa/UITestDemo

 


转载:https://blog.csdn.net/adolph_lu/article/details/101374043
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场