在Android的饼进度条(Pie Progress Bar in android)

编程入门 行业动态 更新时间:2024-10-28 21:18:07
在Android的饼进度条(Pie Progress Bar in android)

您好我想在Android中创建一个Pie进度栏,如附图所示。

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

所以我决定编写自己的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循环后,它将显示进度条,如附加图像中显示黑色进度。

任何人都可以告诉我这段代码有什么问题。 为什么这段代码没有进行持续更新?

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

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

}

and the activity class from i call this..

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

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 ,它就像一个魅力一样。 代码如下:

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

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

更多推荐

本文发布于:2023-07-20 12:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199603.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进度条   Pie   Android   android   Bar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!