android仿ios动画,Android 高仿iOS加载动画

效果图

512497a7c35c

gggggg.gif

无需多言, 代码

java 类 (直接拷贝使用)

/**

* Project: android

* ClassName: LoadingView

* Date: 2020/7/28 15:34

* Creator: voisen

* Email: voisen@icloud.com

* Version: 1.0

* Description: this is LoadingView description !

*/

public class LoadingView extends View {

private String TAG = "LoadingView";

//自定义变量区域

private int num = 12;

private float strokeWidth = 5;

private int changeDelay = 75;

private int color;

private int minAlpha = 20;

//以下变量为自计算, 无需修改

private Paint mPaint;

private PointF center = new PointF(0,0);

private int alphaStep = 0;

private float angleStep = 0f;

private float baseAngle = 0f;

private float maxRadius = 0.0f;

private float minRadius = 0.0f;

private Timer timer;

private TimerTask timerTask = new TimerTask() {

@Override

public void run() {

float a = baseAngle - angleStep;

if (a % 360 == 0){

a = 0;

}

baseAngle = a;

postInvalidate();

}

};

public LoadingView(@NonNull Context context) {

this(context, null);

}

public LoadingView(@NonNull Context context, @Nullable AttributeSet attrs) {

this(context, attrs, 0);

}

public LoadingView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.LoadingView);

initAttrs(attributes);

mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

mPaint.setDither(true);

mPaint.setStrokeWidth(strokeWidth);

mPaint.setStyle(Paint.Style.STROKE);

mPaint.setStrokeJoin(Paint.Join.ROUND);

mPaint.setStrokeCap(Paint.Cap.ROUND);

mPaint.setColor(color);

alphaStep = (0xff-minAlpha)/num;

angleStep = -360.0f/num;

timer = new Timer(false);

timer.schedule(timerTask,0, changeDelay);

setWillNotDraw(false);

}

private void initAttrs(TypedArray attributes) {

minAlpha = attributes.getInt(R.styleable.LoadingView_minAlpha,0x11);

changeDelay = attributes.getInt(R.styleable.LoadingView_runDuration,75);

num = attributes.getInt(R.styleable.LoadingView_lineCount,12);

strokeWidth = attributes.getDimension(R.styleable.LoadingView_lineWidth,5);

color = attributes.getColor(R.styleable.LoadingView_lineColor,Color.parseColor("#555555"));

}

@Override

protected void onAttachedToWindow() {

super.onAttachedToWindow();

}

@Override

protected void onDetachedFromWindow() {

timerTask.cancel();

timer.cancel();

super.onDetachedFromWindow();

}

@Override

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

super.onLayout(changed, left, top, right, bottom);

if (changed){

center.x = getWidth()/2.0f;

center.y = getHeight()/2.0f;

maxRadius = Math.min(getWidth(),getHeight())/2 - strokeWidth - 5;

minRadius = maxRadius/1.75f;

}

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int desiredWidth = 80;

int desiredHeight = 80;

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int widthSize = MeasureSpec.getSize(widthMeasureSpec);

int heightMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

int width;

int height;

if (widthMode == MeasureSpec.EXACTLY) {

width = widthSize;

} else if (widthMode == MeasureSpec.AT_MOST) {

width = Math.min(desiredWidth, widthSize);

} else {

width = desiredWidth;

}

if (heightMode == MeasureSpec.EXACTLY) {

height = heightSize;

} else if (heightMode == MeasureSpec.AT_MOST) {

height = Math.min(desiredHeight, heightSize);

} else {

height = desiredHeight;

}

setMeasuredDimension(width, height);

center.x = getWidth()/2.0f;

center.y = getWidth()/2.0f;

maxRadius = Math.min(getWidth(),getHeight())/2 - strokeWidth - 5;

minRadius = maxRadius/1.75f;

}

@Override

public void draw(Canvas canvas) {

super.draw(canvas);

double a;

float x1,y1,x2,y2;

for (int i = 0; i < num; i++) {

a = angleStep * i + baseAngle;

x1 = (float) (Math.cos(Math.PI*a/180.0) * minRadius);

x2 = (float) (Math.cos(Math.PI*a/180.0) * maxRadius);

y1 = (float) (Math.sin(Math.PI*a/180.0) * minRadius);

y2 = (float) (Math.sin(Math.PI*a/180.0) * maxRadius);

Path path = new Path();

path.moveTo(center.x+x1,center.y+y1);

path.lineTo(center.x+x2,center.y+y2);

int alpha = 0xff - (i * alphaStep);

mPaint.setAlpha(alpha);

canvas.drawPath(path,mPaint);

}

}

}

styleable

作者言

喜欢请收藏点赞 ❤️

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值