VerticalProgressBar.java:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class VerticalProgressBar extends View {
private Paint paint; // 画笔
private int progress; // 进度值
private int width; // 宽度值
private int height; // 高度值
public VerticalProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public VerticalProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public VerticalProgressBar(Context context) {
super(context);
init();
}
private void init() {
paint = new Paint();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getMeasuredWidth() - 1;// 宽度值
height = getMeasuredHeight() - 1;// 高度值
}
@Override
protected void onDraw(Canvas canvas) {
paint.setColor(Color.rgb(10, 73, 160));// 设置画笔颜色, blue
canvas.drawLine(0, 0, width, 0, paint);// 画顶边
canvas.drawLine(0, 0, 0, height, paint);// 画左边
canvas.drawLine(width, 0, width, height, paint);// 画右边
canvas.drawLine(0, height, width, height, paint);// 画底边
float count_height = height;
float valid_height = progress / 100f * height;
float one_setp_height = height / 50;
float interval_height = 7;
float drawed_height = 0;
Log.d("wujiang", "onDraw: valid_height = " + valid_height);
Log.d("wujiang", "onDraw: one_setp_height = " + one_setp_height);
if (valid_height > 0) {
while (drawed_height <= valid_height) {
paint.setColor(Color.rgb(10, 73, 160));// 设置画笔颜色, blue
canvas.drawRect(1, height - (drawed_height + one_setp_height), width, height - drawed_height, paint);// 画矩形
drawed_height += one_setp_height;
if (drawed_height + one_setp_height >= valid_height) {
break;
} else {
paint.setColor(Color.TRANSPARENT); // 设置画笔颜色,设置间隔的颜色
canvas.drawRect(1, height - (drawed_height + interval_height), width, height - drawed_height, paint);// 画矩形
drawed_height += interval_height;
}
}
} else {
canvas.drawRect(0, height - progress / 100f * height, width, height, paint);// 画矩形
}
paint.setColor(Color.GREEN);
paint.setTextSize(width / 3);// 设置文字大小
canvas.drawText(String.valueOf(progress), (width - getTextWidth(String.valueOf(progress))) / 2, height / 2, paint);// 画文字
super.onDraw(canvas);
}
/**
* 拿到文字宽度
* @param str 传进来的字符串
* return 宽度
*/
private int getTextWidth(String str) {
// 计算文字所在矩形,可以得到宽高
Rect rect = new Rect();
paint.getTextBounds(str, 0, str.length(), rect);
return rect.width();
}
/** 设置progressbar进度 */
public void setProgress(int progress) {
this.progress = progress;
postInvalidate();
}
public int getProgress() {
return this.progress;
}
}
xml里的定义:
<com.amlogic.dvb_view.VerticalProgressBar
android:id="@+id/progress_quality"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/px_20.0" />
activity里的使用:
VerticalProgressBar mProgressQuality = findViewById(R.id.progress_quality);
mProgressQuality.setProgress(68);
THE END
2187

被折叠的 条评论
为什么被折叠?



