1.直接上代码吧
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.util.TypedValue;
/**
* created by miaozi
* on 2020/3/3
**/
public class CircleLoadView extends View {
private Paint mPaint;
private int mLineWidth;
private boolean isRunAnimation;
private int mCount = 0;
private int[] mColors = new int[]{
Color.parseColor("#FCFCFC"),
Color.parseColor("#F0F0F0"),
Color.parseColor("#E0E0E0"),
Color.parseColor("#d0d0d0"),
Color.parseColor("#BEBEBE"),
Color.parseColor("#ADADAD"),
Color.parseColor("#9D9D9D"),
Color.parseColor("#8E8E8E"),
Color.parseColor("#7B7B7B"),
Color.parseColor("#6C6C6C"),
Color.parseColor("#5B5B5B"),
Color.parseColor("#4F4F4F"),
Color.parseColor("#3C3C3C"),
Color.parseColor("#272727"),
};
private RectF mRectF;
public CircleLoadView(Context context) {
this(context,null);
}
public CircleLoadView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public CircleLoadView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLACK);
mLineWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,20,getResources().getDisplayMetrics());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (width > height) {
width = height;
} else if (height > width) {
height = width;
}
if (mRectF == null){
int radius = width / 3;
int circleRadius = width / 20;
mRectF = new RectF(width/2 - circleRadius - radius,height/2-mLineWidth,radius,height/2+mLineWidth);
}
setMeasuredDimension(width,height);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onDraw(Canvas canvas) {
canvas.rotate(mCount * 36 ,getWidth()/2,getWidth()/2);
for(int i = 0 ; i < 10 ; i ++ ){
canvas.drawRect(mRectF,mPaint);
mPaint.setColor(mColors[i]);
//旋转36度
canvas.rotate(36,getWidth()/2,getWidth()/2);
}
if(mCount == 9){
mCount = 0;
}else {
mCount ++;
}
if(isRunAnimation) {
postInvalidateDelayed(100);
}
}
/**
* 显示 自动启动轮循
*/
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
isRunAnimation = true;
postInvalidate();
}
/**
* 关闭就停止
*/
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
isRunAnimation = false;
}
}
网友评论