/**
*
*/
package com.example.horview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/*
* 类名 HorProgressBar.java
* 说明 description of the class
* 创建日期 2015-2-8
* 作者 ou
*/
public class HorProgressBar extends View {
/**
* 画笔对象的引用
*/
private Paint paint;
/**
* 画笔对象的引用
*/
private Paint paint2;
/**
* 圆环的颜色
*/
private int roundColor;
/**
* 圆环进度的颜色
*/
private int roundProgressColor;
/**
* 中间进度百分比的字符串的颜色
*/
private int textColor;
/**
* 中间进度百分比的字符串的字体
*/
private float textSize;
/**
* 圆环的宽度
*/
private float roundWidth;
/**
* 最大进度
*/
private int max;
/**
* 当前进度
*/
private int progress;
/**
* 是否显示中间的进度
*/
private boolean textIsDisplayable;
/**
* 进度的风格,实心或者空心
*/
private int style;
public static final int STROKE = 0;
public static final int FILL = 1;
public HorProgressBar(Context context) {
this(context, null);
}
public HorProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public HorProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint();
paint2 = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 画底下的线!
*/
paint.setColor(Color.BLUE); //设置线的颜色!
paint.setStyle(Style.STROKE);//设置非填充
paint.setStrokeWidth(5);//笔宽5像素
paint.setAntiAlias(true); //消除锯齿
canvas.drawLine(0, 0, getWidth(), 0, paint);
/**
* 画第2条线
*/
paint2.setColor(Color.RED); //设置线的颜色!
paint2.setStyle(Style.STROKE);//设置非填充
paint2.setStrokeWidth(5);//笔宽5像素
paint2.setAntiAlias(true); //消除锯齿
if(progress !=0) {
canvas.drawLine(0, 0,progress*getWidth()/getWidth(), 0, paint2);
}
switch (style) {
case STROKE:{
paint.setStyle(Paint.Style.STROKE);
break;
}
case FILL:{
paint.setStyle(Paint.Style.FILL_AND_STROKE);
if(progress !=0)
break;
}
}
}
public synchronized int getMax() {
return max;
}
/**
* 设置进度的最大值
* @param max
*/
public synchronized void setMax(int max) {
if(max < 0){
throw new IllegalArgumentException("max not less than 0");
}
this.max = max;
}
/**
* 获取进度.需要同步
* @return
*/
public synchronized int getProgress() {
return progress;
}
/**
* 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步
* 刷新界面调用postInvalidate()能在非UI线程刷新
* @param progress
*/
public synchronized void setProgress(int progress) {
if(progress < 0){
throw new IllegalArgumentException("progress not less than 0");
}
this.progress = progress;
postInvalidate(); //这个是可以异步调用ondraw的方法的!
}
public int getCricleColor() {
return roundColor;
}
public void setCricleColor(int cricleColor) {
this.roundColor = cricleColor;
}
public int getCricleProgressColor() {
return roundProgressColor;
}
public void setCricleProgressColor(int cricleProgressColor) {
this.roundProgressColor = cricleProgressColor;
}
public int getTextColor() {
return textColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
}
public float getTextSize() {
return textSize;
}
public void setTextSize(float textSize) {
this.textSize = textSize;
}
public float getRoundWidth() {
return roundWidth;
}
public void setRoundWidth(float roundWidth) {
this.roundWidth = roundWidth;
}
}主要是在做视频进度条的时候用到了!
简单的自定义水平进度条
最新推荐文章于 2023-12-14 14:32:54 发布