圆形进度条

转载:https://blog.youkuaiyun.com/jdsjlzx/article/details/42497253

一、通过动画实现

定义res/anim/loading.xml如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <animation-list android:oneshot="false"  
  3. xmlns:android="http://schemas.android.com/apk/res/android">  
  4.   <item android:duration="150" android:drawable="@drawable/loading_01" />  
  5.   <item android:duration="150" android:drawable="@drawable/loading_02" />  
  6.   <item android:duration="150" android:drawable="@drawable/loading_03" />  
  7.   <item android:duration="150" android:drawable="@drawable/loading_04" />  
  8.   <item android:duration="150" android:drawable="@drawable/loading_05" />  
  9.   <item android:duration="150" android:drawable="@drawable/loading_06" />  
  10.   <item android:duration="150" android:drawable="@drawable/loading_07" />  
  11. </animation-list>   

在layout文件中引用如下:

  1. <ProgressBar android:id="@+id/loading_process_dialog_progressBar"  
  2.   android:layout_width="wrap_content" android:layout_height="wrap_content"  
  3.   android:indeterminate="false" android:indeterminateDrawable="@anim/loading" />  

二、通过自定义颜色实现

定义res/drawable/dialog_style_xml_color.xml如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <rotate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.   android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"  
  4.   android:toDegrees="360">  
  5.   <shape android:shape="ring" android:innerRadiusRatio="3"  
  6.     android:thicknessRatio="8" android:useLevel="false">  
  7.     <gradient android:type="sweep" android:useLevel="false"  
  8.      android:startColor="#FFFFFF" android:centerColor="#FFDC35"  
  9.      android:centerY="0.50" android:endColor="#CE0000" />  
  10.   </shape>  
  11. </rotate>  

在layout文件中引用如下:

  1. <ProgressBar android:id="@+id/loading_process_dialog_progressBar"  
  2.   android:layout_width="wrap_content" android:layout_height="wrap_content"  
  3.   android:indeterminate="false" android:indeterminateDrawable="@drawable/dialog_style_xml_color" />  


三、使用一张图片进行自定义

定义res/drawable/dialog_style_xml_icon.xml如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
  3. <item>  
  4.   <rotate android:drawable="@drawable/dialog_progress_round"  
  5.    android:fromDegrees="0.0" android:toDegrees="360.0" android:pivotX="50.0%"  
  6.    android:pivotY="50.0%" />  
  7. </item>  
  8. </layer-list>

在layout文件中引用如下:

[html]  view plain  copy
  1. <ProgressBar android:id="@+id/loading_process_dialog_progressBar"    
  2.   android:layout_width="wrap_content" android:layout_height="wrap_content"    
  3.   android:indeterminate="false" android:indeterminateDrawable="@drawable/dialog_style_xml_icon" />   

main.xml如下:

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:orientation="vertical" android:layout_width="fill_parent"  
  4. android:layout_height="fill_parent" android:gravity="center"  
  5. android:background="#FFF">  
  6. <Button android:text="@string/anim" android:id="@+id/anim"  
  7.   android:layout_width="120dip" android:layout_height="wrap_content" />  
  8. <Button android:text="@string/color" android:id="@+id/color"  
  9.   android:layout_width="120dip" android:layout_height="wrap_content" />  
  10. <Button android:text="@string/icon" android:id="@+id/icon"  
  11.   android:layout_width="120dip" android:layout_height="wrap_content" />  
  12. </LinearLayout>  

之后通过三个按钮将ProgressBar 放在对话框中显示出来就完成了。

  AnimRoundProcessDialog.rar 


截图如下:


package com.example.task; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; public class CircleProgressBar extends View { private int maxProgress = 100; private int progress = 10; private int progressStrokeWidth = 20; // 画圆所在的距形区域 RectF oval; Paint paint; public CircleProgressBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO 自动生成的构造函数存根 oval = new RectF(); paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { // TODO 自动生成的方法存根 super.onDraw(canvas); int width = this.getWidth(); int height = this.getHeight(); if (width != height) { int min = Math.min(width, height); width = min; height = min; } paint.setAntiAlias(true); // 设置画笔为抗锯齿 paint.setColor(0xFFE2E2E2); // 设置画笔颜色 canvas.drawColor(Color.TRANSPARENT); // 白色背景 paint.setStrokeWidth(progressStrokeWidth); // 线宽 paint.setStyle(Style.STROKE); oval.left = progressStrokeWidth / 2; // 左上角x oval.top = progressStrokeWidth / 2; // 左上角y oval.right = width - progressStrokeWidth / 2; // 左下角x oval.bottom = height - progressStrokeWidth / 2; // 右下角y canvas.drawArc(oval, -90, 360, false, paint); // 绘制白色圆圈,即进度条背景 // paint.setColor(Color.rgb(0x57, 0x87, 0xb6)); paint.setColor(0xFFFF4700); canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 绘制进度圆弧,这里是蓝色 paint.setStrokeWidth(1); String text = progress + "分"; int textHeight = height / 4; paint.setTextSize(textHeight); int textWidth = (int) paint.measureText(text, 0, text.length()); paint.setStyle(Style.FILL); // canvas.drawText(text, width / 2 - textWidth / 2, height / 2 // + textHeight / 2, paint); } public int getMaxProgress() { return maxProgress; } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } public void setProgress(int progress) { this.progress = progress; this.invalidate(); } /** * 非UI线程调用 */ public void setProgressNotInUiThread(int progress) { this.progress = progress; this.postInvalidate(); } } 上面的代码就是画两个圆弧 package com.example.task; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity { CircleProgressBar completedView1; CircleProgressBar completedView2; CircleProgressBar completedView3; CircleProgressBar completedView4; CircleProgressBar completedView5; CircleProgressBar completedView6; CircleProgressBar completedView7; CircleProgressBar completedView8; CircleProgressBar completedView9; CircleProgressBar completedView10; CircleProgressBar completedView11; CircleProgressBar completedView12; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); completedView1 = (CircleProgressBar) findViewById(R.id.circleProgressBar1); completedView2 = (CircleProgressBar) findViewById(R.id.circleProgressBar2); completedView3 = (CircleProgressBar) findViewById(R.id.circleProgressBar3); completedView4 = (CircleProgressBar) findViewById(R.id.circleProgressBar4); completedView5 = (CircleProgressBar) findViewById(R.id.circleProgressBar5); completedView6 = (CircleProgressBar) findViewById(R.id.circleProgressBar6); completedView7 = (CircleProgressBar) findViewById(R.id.circleProgressBar7); completedView8 = (CircleProgressBar) findViewById(R.id.circleProgressBar8); completedView9 = (CircleProgressBar) findViewById(R.id.circleProgressBar9); completedView10 = (CircleProgressBar) findViewById(R.id.circleProgressBar10); completedView11 = (CircleProgressBar) findViewById(R.id.circleProgressBar11); completedView12 = (CircleProgressBar) findViewById(R.id.circleProgressBar12); this.findViewById(R.id.button_show).setOnClickListener( new OnClickListener() { private int i2 = 40; @Override public void onClick(View v) { // completedView1.setProgressNotInUiThread(i2 % 100); // completedView2.setProgressNotInUiThread(i2 % 100); // completedView3.setProgressNotInUiThread(i2 % 100); // completedView4.setProgressNotInUiThread(i2 % 100); // completedView5.setProgressNotInUiThread(i2 % 100); // completedView6.setProgressNotInUiThread(i2 % 100); // completedView7.setProgressNotInUiThread(i2 % 100); // completedView8.setProgressNotInUiThread(i2 % 100); // completedView9.setProgressNotInUiThread(i2 % 100); // completedView10.setProgressNotInUiThread(i2 % 100); // completedView11.setProgressNotInUiThread(i2 % 100); // completedView12.setProgressNotInUiThread(i2 % 100); // i2 += 10; new Thread() { public void run() { int i = 0; while (i <= i2) { completedView1.setProgressNotInUiThread(i); completedView2.setProgressNotInUiThread(i); completedView3.setProgressNotInUiThread(i); completedView4.setProgressNotInUiThread(i); completedView5.setProgressNotInUiThread(i); completedView6.setProgressNotInUiThread(i); completedView7.setProgressNotInUiThread(i); completedView8.setProgressNotInUiThread(i); completedView9.setProgressNotInUiThread(i); completedView10.setProgressNotInUiThread(i); completedView11.setProgressNotInUiThread(i); completedView12.setProgressNotInUiThread(i); i++; try { sleep(10); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } }.start(); i2 += 10; } }); } } 上面的主活动界面使用线程画图
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值