您好我想在Android中制作一个Pie Progress栏,如附图所示。
我已经完成了以下代码,但没有收获。enter link description here
所以我决定编写自己的ProgressBarView。这是我的示例代码..
public class PieProgressView extends View {
private float mProgress;
private int MAX_ANGLE = 360;
public PieProgressView(Context context) {
super(context);
}
public PieProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public PieProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawOval(canvas);
}
public void setProgress(int progress){
mProgress = (float) ((float)progress*3.6);
invalidate();
}
private void drawOval(Canvas canvas){
canvas.drawColor(Color.CYAN);
Paint paint = new Paint();
// smooths
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
RectF oval2 = new RectF(50, 50, 500, 500);
canvas.drawOval(oval2, paint);// opacity
Paint p = new Paint();
p.setColor(Color.BLACK);
canvas.drawArc(oval2, 270, mProgress, true, p);
System.out.println("drawOval Progress == "+mProgress);
//p.setAlpha(0x80); //
}
}
我的活动课叫这个..
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
pieProgressView = (PieProgressView)findViewById(R.id.pieprogress);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fillProgress();
}
});
}
private void fillProgress(){
for(int i=0;i<100 ; i++) {
pieProgressView.setProgress(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
现在在for循环期间没有显示进度,但在完成for循环后,它将显示进度条,如附加图像显示黑色进度。
任何人都可以告诉我这段代码有什么问题。为什么这段代码没有连续更新?
答案 0 :(得分:0)
Thread.sleep( )
导致UI线程被阻塞,因此您会看到&#34;已填充&#34;线程唤醒时的饼图。我将代码更改为使用CountDownTimer
代替它,它就像一个魅力。这是代码:
public class MainActivity extends AppCompatActivity {
PieProgressView pie;
CountDownTimer timer;
int progress = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pie = (PieProgressView) findViewById(R.id.pie);
scheduleNextTimer();
}
private CountDownTimer getTimer(){
return new CountDownTimer(1000,1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
Log.d("PROG",progress + "");
pie.setProgress( progress++ );
scheduleNextTimer();
}
};
}
private void scheduleNextTimer(){
if( progress < 100 ){
timer = getTimer();
timer.start();
}
}
}
答案 1 :(得分:0)
我实现了
<ProgressBar
android:id="@+id/pr_progress_in_circle"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="24dp"
android:layout_height="24dp"
android:indeterminate="false"
android:max="100"
android:progress="60"
android:progressDrawable="@drawable/progress"
android:rotation="-90" />
其中progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="@dimen/dimen_2"
android:left="@dimen/dimen_2"
android:right="@dimen/dimen_2"
android:top="@dimen/dimen_2">
<shape android:shape="oval">
<solid android:color="@color/grey_lighter" />
<stroke android:color="@android:color/transparent" />
</shape>
</item>
<item>
<shape
android:innerRadiusRatio="100"
android:shape="ring"
android:thicknessRatio="2.5"
android:useLevel="true">
<gradient
android:centerColor="@color/orange_above_avg"
android:endColor="@color/orange_above_avg"
android:startColor="@color/orange_above_avg"
android:type="sweep"
android:useLevel="false" />
</shape>
</item>
</layer-list>
您可以通过
对其进行测试 mProgressBar.setMax(totalQuestions);
mProgressBar.setProgress(correctQuestion);
或参加this library