您好我想在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(); } } }更多推荐
发布评论