android 扩散效果,Android高级UI之手写粒子扩散效果

Android高级UI之手写粒子扩散效果

math?formula=%5Ccolor%7Bred%7D%7B%E7%BA%A2%E8%89%B2%E5%AD%97%7D

先来看效果图(gif图片看起来有卡顿,真机 不会)

41a29efb3886?utm_campaign=haruki&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image

1.重写onSizeChanged方法得到 屏幕正中心点坐标

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

// 屏幕正中心点坐标

mCenterX = w /2f;

mCenterY = h /2f;

//屏幕对角线

mDiagonalDist = (float) (Math.sqrt((w * w + h * h)) /2f);//勾股定理

}

2.初始化

public SplashView(Context context) {

super(context);

init(context);

}

private void init(Context context) {

mCircleColors = context.getResources().getIntArray(R.array.splash_circle_colors);

//画笔

mPaint.setAntiAlias(true);

mPaintBackground.setAntiAlias(true);

mPaintBackground.setStyle(Paint.Style.STROKE);

mPaintBackground.setColor(mSplashBgColor);

}

3.新建SplashState 侧列模式

private abstract class SplashState {

public abstract void drawState(Canvas canvas);

public void cencel() {

mAnimator.cancel();

}

}

4.旋转动画

private class RotateState extends SplashState {

public RotateState() {

mAnimator = ValueAnimator.ofFloat(0f, (float) (Math.PI * 2));

mAnimator.setInterpolator(new LinearInterpolator());

mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

mCurrentRotationAngle = (float) animation.getAnimatedValue();

postInvalidate();

}

});

mAnimator.setDuration(mRotationDuration);

mAnimator.setRepeatCount(ValueAnimator.INFINITE);

mAnimator.start();

}

@Override

public void drawState(Canvas canvas) {

drawBackground(canvas);

//绘制小圆

draCircles(canvas);

}

}

private void drawBackground(Canvas canvas) {

if (mHoleRadius > 0f) {

mPaintBackground.setColor(Color.WHITE);

float strokeWith= mDiagonalDist-mHoleRadius;

mPaintBackground.setStrokeWidth(strokeWith);

float v = mHoleRadius + strokeWith / 2;

canvas.drawCircle(mCenterX, mCenterY, v, mPaintBackground);

} else {

canvas.drawColor(mSplashBgColor);

}

}

private void draCircles(Canvas canvas) {

Log.e("小圆的圆心点",mCenterX+","+mCenterY);

float rotertionAngle = (float) (2 * Math.PI / mCircleColors.length);

for (int i = 0; i < mCircleColors.length; i++) {

/**

* x=r*cos(a)+mCenterX

* y=r*sin(a)+mCenterY

*/

float angle = i * rotertionAngle + mCurrentRotationAngle;

float cx = (float) (mCurrentRotationRadius * Math.cos(angle) + mCenterX);

float cy = (float) (mCurrentRotationRadius * Math.sin(angle) + mCenterY);

mPaint.setColor(mCircleColors[i]);

canvas.drawCircle(cx, cy, mCircleRadius, mPaint);

}

}

4.集合动画

private class MergingState extends SplashState {

public MergingState() {

mAnimator = ValueAnimator.ofFloat(0f, mRotationRadius);

mAnimator.setDuration(mRotationDuration);

mAnimator.setInterpolator(new OvershootInterpolator(10f));

mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

Log.e("animation====", animation.getAnimatedValue() + "");

mCurrentRotationRadius = (float) animation.getAnimatedValue();

invalidate();

}

});

mAnimator.addListener(new AnimatorListenerAdapter() {

@Override

public void onAnimationEnd(Animator animation) {

super.onAnimationEnd(animation);

mState = new ExpandState();

}

});

mAnimator.reverse();

}

@Override

public void drawState(Canvas canvas) {

drawBackground(canvas);

//绘制小圆

draCircles(canvas);

}

}

5.水波纹扩散动画

private class ExpandState extends SplashState {

public ExpandState() {

mAnimator = ValueAnimator.ofFloat(mCircleRadius, mDiagonalDist);

mAnimator.setDuration(mRotationDuration);

mAnimator.setInterpolator(new LinearInterpolator());

mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

mHoleRadius = (float) animation.getAnimatedValue();

invalidate();

}

});

mAnimator.start();

}

@Override

public void drawState(Canvas canvas) {

drawBackground(canvas);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值