您的位置:首页 > 移动开发 > Android开发

Android圆形进度条-RoundProgressBar

2016-10-13 21:24 260 查看
RoundProgressBar

public class RoundProgressBar extends View {

private Paint mPaint;
private int mRoundColor;
private int mRoundProgressColor;
private float mRoundWidth;
private int mMax;
private int mTextColor;
private float mTextSize;
private boolean mTextIsDisplayable;
private int mStyle;

private static final int STROKE = 0;
private static final int FILL = 1;

private int mProgress = 0;

public RoundProgressBar(Context context) {
this(context,null);
}

public RoundProgressBar(Context context, AttributeSet attrs) {
this(context,attrs,0);
}

public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

mPaint = new Paint();
//获取自定义的属性
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar);
mRoundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);//第二个参数均为默认值--圆的颜色
mRoundProgressColor = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.RED);//进度条的颜色
mRoundWidth = typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 3);//进度条的宽度
mMax = typedArray.getInteger(R.styleable.RoundProgressBar_max, 100);//进度条的最大值
mTextColor = typedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.RED);//进度条的文字颜色
mTextSize = typedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);//进度条的文字大小
mTextIsDisplayable = typedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);//进度条的百分比是否显示
mStyle = typedArray.getInt(R.styleable.RoundProgressBar_style, 0);//--------------设置进度条是空心的

typedArray.recycle();

}

//绘制
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

//第一步画圆
int centerOfCircle = getWidth() / 2;//圆心
int radius = (int) (centerOfCircle - mRoundWidth / 2);//半径

mPaint.setAntiAlias(true);//设置画笔
mPaint.setColor(mRoundColor);//设置圆的颜色
mPaint.setStyle(Paint.Style.FILL);//设置圆是实心的还是空心的
mPaint.setStrokeWidth(mRoundWidth);//设置画笔宽度

canvas.drawCircle(centerOfCircle,centerOfCircle,radius,mPaint);//画图

//第二步设置进度条
mPaint.setStrokeWidth(mRoundWidth);//画笔宽度
mPaint.setColor(mRoundProgressColor);//设置进度
mPaint.setAntiAlias(true);

RectF oval = new RectF(centerOfCircle - radius, centerOfCircle - radius, centerOfCircle + radius, centerOfCircle + radius);

switch (mStyle) {
case STROKE://进度条是空心的
mPaint.setStyle(Paint.Style.STROKE);//画圆弧
canvas.drawArc(oval, 180, 360 * mProgress / mMax, false, mPaint);//开始的角度
break;
case FILL://进度条是实心的
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);//画圆弧
if(mProgress!=0) {
canvas.drawArc(oval, 180, 360 * mProgress / mMax, true, mPaint);
}
break;
}

//第三步设置字体
mPaint.setStrokeWidth(0);//画百分比
mPaint.setTextSize(mTextSize);//字体大小
mPaint.setColor(mTextColor);//画笔颜色
mPaint.setTypeface(Typeface.DEFAULT);//字体
mPaint.setTextAlign(Paint.Align.CENTER);

float textY = centerOfCircle - (mPaint.descent() + mPaint.ascent()) / 2;
if (mTextIsDisplayable) {//如果文字显示那么就开始写文字
canvas.drawText("跳过", centerOfCircle, textY, mPaint);
}

//字体不显示跳过显示百分比
// int percent = (int) (((float) mProgress / (float) mMax) * 100);
// float textWidth = mPaint.measureText(percent + "%");
// 判断是否显示进度文字 不是0,风格是空心的
// if (mTextIsDisplayable && percent != 0 && mStyle == STROKE) {
// canvas.drawText(percent + "%", centerOfCircle - textWidth / 2, centerOfCircle + textWidth / 2, mPaint);
// }
}

public int getRoundColor() {
return mRoundColor;
}

public void setRoundColor(int roundColor) {
mRoundColor = roundColor;
}

public int getRoundProgressColor() {
return mRoundProgressColor;
}

public void setRoundProgressColor(int roundProgressColor) {
mRoundProgressColor = roundProgressColor;
}

public float getRoundWidth() {
return mRoundWidth;
}

public void setRoundWidth(float roundWidth) {
mRoundWidth = roundWidth;
}

public int getTextColor() {
return mTextColor;
}

public void setTextColor(int textColor) {
mTextColor = textColor;
}

public float getTextSize() {
return mTextSize;
}

public void setTextSize(float textSize) {
mTextSize = textSize;
}

public synchronized int getMax() {
return mMax;
}

public synchronized void setMax(int max) {
mMax = max;
}

public boolean isTextIsDisplayable() {
return mTextIsDisplayable;
}

public void setTextIsDisplayable(boolean textIsDisplayable) {
mTextIsDisplayable = textIsDisplayable;
}

public int getStyle() {
return mStyle;
}

public void setStyle(int style) {
mStyle = style;
}

public synchronized int getProgress() {
return mProgress;
}

public synchronized void setProgress(int progress) {
if(progress < 0){
throw new IllegalArgumentException("progress not less than 0");
}
if(progress > mMax){
mProgress=progress;
}
if(progress <= mMax){
mProgress = progress;
postInvalidate();
}
}
}activity_welcome

一张图片背景右上角有一个圆形进度条

<RelativeLayout
android:id="@+id/welcome_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/welcome"
android:layout_weight="2">
<view.RoundProgressBar
android:id="@+id/progressBar"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_width="37dp"
android:layout_height="37dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
app:roundColor="#c7c7c7"
app:roundProgressColor="#f41a29"
app:roundWidth="1dp"
app:textColor="#ffffff"
app:textIsDisplayable="true"
app:textSize="15sp" />
</RelativeLayout>WelcomeActivity:
public class WelcomeActivity extends AppCompatActivity implements View.OnClickListener{
//仿iphone带进度的进度条,线程安全的View,可直接在线程中更新进度
private RoundProgressBar mRoundProgressBar;
private static final String TAG = "welcome";
private int mProgress = 0;

//主线程接收到子线程发送过来的消息
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
mRoundProgressBar.setProgress(mProgress);
Log.d(TAG,mRoundProgressBar.getProgress()+"");
Log.d(TAG,mRoundProgressBar.getMax()+"");
if (mProgress>=100){
Intent it = new Intent(WelcomeActivity.this,MainActivity.class);
startActivity(it);
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);

mRoundProgressBar = (RoundProgressBar) findViewById(R.id.progressBar);
//进入欢迎界面后圆形滚动条开始滚动
initData();
//当点击滚动条上文字跳过的时候实现页面直接跳转
mRoundProgressBar.setOnClickListener(this);
}

private void initData() {
//开启子线程开始耗时操作
new Thread() {
@Override
public void run() {
while (mProgress < 100) {
mProgress=mProgress+2;
//子线程给主线程发送消息更新UI
handler.sendEmptyMessage(0);
SystemClock.sleep(500);
}
}
}.start();
}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.progressBar:{
Intent it = new Intent(WelcomeActivity.this,MainActivity.class);
startActivity(it);
}
}
}
}功能:当进度条满100的时候,跳转页面
            当点击进度条的时候直接跳转页面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息