实现效果如下:
使用方法:
在layout文件中添加以下代码:
<com.example.jack.ui.widget.RingLoading android:layout_width="20.0dip" android:layout_height="20.0dip" />
具体代码如下:
package com.example.jack.ui.widget; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.SweepGradient; import android.util.AttributeSet; import android.view.View; /** * Created by pengf on 2017/4/10. */ public class RingLoading extends View { private static final int PROGRESS = 5; private int centerX; private int centerY; private Context context; private int mProgressColor ; private Matrix matrix; private final Paint paint; private int roundWidth = 10; private int start = 0; public RingLoading(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; this.paint = new Paint(); this.paint.setAntiAlias(true); this.paint.setStyle(Paint.Style.STROKE); this.paint.setStrokeWidth(this.roundWidth); this.mProgressColor =Color.parseColor("#ffffcc00"); } @Override protected void onDraw(Canvas canvas) { int center = getWidth() / 2; int radius = center - this.roundWidth / 2; SweepGradient sweepGradient = new SweepGradient(this.centerX, this.centerY, Color.TRANSPARENT, this.mProgressColor); this.paint.setShader(sweepGradient); this.matrix = new Matrix(); this.matrix.postRotate(this.start, this.centerX, this.centerY); canvas.concat(this.matrix); canvas.drawCircle(center, center, radius, this.paint); this.start = (PROGRESS + this.start); invalidate(); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); this.centerX = (w / 2); this.centerY = (h / 2); } }
时间: 2024-10-24 23:38:35