Android开发 自制圆形带进度显示的进度条



Android默认的进度条,如果是圆型的,是不能显示进度的,只是在转圈圈,而基于官方的ProgressBar自定义,也只能换那个圈圈的图片而已,无法实现展示进度。下图是要实现的效果:


其实原理也很简单,定义一个类继承View,在onDraw方法里画圆即可.

CircleProgressBar:


package com.pocketdigi.curcleprogressbar.view;
 
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 = 30;
private int progressStrokeWidth = 4;
//画圆所在的距形区域
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(Color.WHITE); // 设置画笔颜色
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));
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();
}
}
activity_main.xml:




<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ccc"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
 
    <com.pocketdigi.curcleprogressbar.view.CircleProgressBar
        android:id="@+id/circleProgressbar"
        android:layout_width="60dp"
        android:layout_height="60dp" />
 
</RelativeLayout>
MainActivity.java:






package com.pocketdigi.circleprogressbar;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
 
import com.pocketdigi.curcleprogressbar.view.CircleProgressBar;
 
public class MainActivity extends Activity {
 
CircleProgressBar progressBar;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=(CircleProgressBar)findViewById(R.id.circleProgressbar);
new Thread()
{
public void run()
{
int i=0;
while(i<=100)
{
progressBar.setProgressNotInUiThread(i);
i++;
try {
sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
 
}
}.start();
 
}
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
 
}


© 2013, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记

你可能感兴趣的:(Android开发 自制圆形带进度显示的进度条)