public class LoadingView extends View { private int mWidth; private int mHeight; private int mCenterX; private int mCenterY; private Paint mPaint; // 默认loading的颜色 private final int mDefaultColor = 0xffffff; // 默认loading线条的宽度 private final float mDefaultLineWidth = 10; // 默认loading线条的高度 private final float mDefaultLineLength = 20; private float mLineWidth = mDefaultLineWidth; private int mLineColor = mDefaultColor; private float mLineLength = mDefaultLineLength; private int control = 1; ValueAnimator animator; public LoadingView(Context context) { this(context, null); } public LoadingView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } private void init(Context context, AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LoadingView); try { mLineColor = typedArray.getColor(R.styleable.LoadingView_lineColor, mDefaultColor); mLineLength = typedArray.getDimension(R.styleable.LoadingView_lineLength, mDefaultLineLength); mLineWidth = typedArray.getDimension(R.styleable.LoadingView_lineWidth, mDefaultLineWidth); } catch (Exception ex) { ex.printStackTrace(); } finally { if(typedArray != null) { typedArray.recycle(); } } mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStyle(Paint.Style.STROKE); mPaint.setColor(mLineColor); mPaint.setStrokeWidth(mLineWidth); animator = ValueAnimator.ofInt(12, 1); animator.setDuration(1000); animator.setInterpolator(new LinearInterpolator()); animator.setRepeatCount(ValueAnimator.INFINITE); animator.setRepeatMode(ValueAnimator.RESTART); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { control = (int) animation.getAnimatedValue(); invalidate(); } }); animator.start(); setBackground(context.getResources().getDrawable(R.drawable.background)); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = MeasureSpec.getSize(widthMeasureSpec); mHeight = MeasureSpec.getSize(heightMeasureSpec); mCenterX = mWidth / 2; mCenterY = mHeight / 2; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (int i = 0; i < 12; i++) { mPaint.setAlpha(((i + 1 + control) % 12 * 255) / 12); canvas.drawLine(mCenterX, mCenterY - mLineLength, mCenterX, mCenterY - mLineLength * 2, mPaint); canvas.rotate(30, mCenterX, mCenterY); } } public void stopLoading(){ if (null != animator && animator.isRunning()) { animator.cancel(); this.setVisibility(GONE); } } }
values文件夹下创建attrs.xml
<resources> <declare-styleable name="LoadingView"> <attr name="lineColor" format="color"></attr> <attr name="lineLength" format="dimension"></attr> <attr name="lineWidth" format="dimension"></attr> </declare-styleable> </resources>
drawable文件夹下创建圆角背景
<shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#80000000"/> <corners android:radius="18dp"/> </shape>