参考博客
效果图
MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private CircleProgressBar progressBar;
private TextView percentageTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
percentageTextView = findViewById(R.id.percentageTextView);
// 设置最大进度值为100
progressBar.setProgress(100);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
int i = 0;
@Override
public void run() {
if (i <= 100) {
final int progress = i;
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progress);
percentageTextView.setText("SDC" + "\n" + progress + "%" + "\n" + "充电中");
}
});
i++;
} else {
timer.cancel();
}
}
}, 0, 50);
}
}
CircleProgressBar
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;