安卓中的饼图进度条 [英] Pie Progress Bar in android

查看:65
本文介绍了安卓中的饼图进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 android 中制作一个 Pie Progress bar,就像附加的图片一样..

Hi I want to make a Pie Progress bar in android like the attached image..

我已经通过以下代码但没有收获.在此处输入链接描述

I have go through the following code but no gain.enter link description here

所以我决定编写自己的ProgressBarView.这是我的示例代码..

So i decided to write my own ProgressBarView. Here is my sample code ..

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 循环完成后,它将显示进度条,如附图所示,显示黑色进度.

Now during this for loop there is no progress shown but after completion of for loop it will shows the progress bar like in attached image showing a black progress.

谁能告诉我这段代码有什么问题.为什么这段代码没有持续更新?

can anyone tell me what the wrong with this code. Why this code not doing a continuous update ?

推荐答案

Thread.sleep( ) 导致 UI 线程阻塞,因此您会在线程唤醒时看到填充"的饼图向上.我更改了代码以使用 CountDownTimer 代替,它就像一个魅力.代码如下:

Thread.sleep( ) is causing the UI thread to block and hence you see the "filled" pie chart when the thread wakes up. I changed the code to use a CountDownTimer instead and it works like a charm. Here's the code:

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();
        }
    }
}

这篇关于安卓中的饼图进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆