您可以通过扩展创建自定义进度视图
View
类和重写
onDraw()
方法
drawArc()
和使用
ValueAnimator
改变弧度
代码:
TimerView.java
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.math.MathUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
import java.util.concurrent.TimeUnit;
public class TimerView extends View {
private Paint ProgressPaint,indicatorPaint;
private float mProgressValue;
private ValueAnimator mTimerAnimator;
float stratAngel=270;
private int strokeWidth=10;
public TimerView(Context context) {
super(context);
ini();
}
public TimerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
ini();
}
public TimerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
ini();
}
void ini(){
ProgressPaint =new Paint();
ProgressPaint.setAntiAlias(true);
ProgressPaint.setStyle(Paint.Style.STROKE);
ProgressPaint.setStrokeWidth(strokeWidth);
ProgressPaint.setColor(Color.WHITE);
indicatorPaint=new Paint();
indicatorPaint.setAntiAlias(true);
indicatorPaint.setStyle(Paint.Style.FILL);
indicatorPaint.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float w = (getWidth() - getPaddingLeft() - getPaddingRight())/2;
float h = (getHeight() - getPaddingTop() - getPaddingBottom())/2;
float radius = Math.min(w, h) / 2.0f;
PointF center = new PointF(getLeft() + radius, getTop() + radius);
RectF rect = new RectF(center.x - radius, center.y - radius,
center.x + radius, center.y + radius);
float progressAngel=360 * mProgressValue;
canvas.drawArc(rect, stratAngel, progressAngel, false, ProgressPaint);
float xPos = radius * (float)Math.cos(Math.toRadians(stratAngel+progressAngel)) + center.x;
float yPos = radius * (float)Math.sin(Math.toRadians(stratAngel+progressAngel)) + center.y;
canvas.drawCircle(xPos, yPos, 30, indicatorPaint);
}
public void start(int secs) {
stop();
mTimerAnimator = ValueAnimator.ofFloat(1f, 0f);
mTimerAnimator.setDuration(TimeUnit.SECONDS.toMillis(secs));
mTimerAnimator.setInterpolator(new LinearInterpolator());
mTimerAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mProgressValue=(float) animation.getAnimatedValue();
invalidate();
}
});
mTimerAnimator.setRepeatCount(Integer.MAX_VALUE);
mTimerAnimator.start();
}
public void stop() {
if (mTimerAnimator != null && mTimerAnimator.isRunning()) {
mTimerAnimator.cancel();
mTimerAnimator = null;
mProgressValue=0.0f;
}
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
start(5);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#dedede"
android:padding="16dp"
>
<<your package name>.TimerView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
screenshot