直播系统开发,实现在进度条中显示文字显示进度
直播系统开发,实现在进度条中显示文字显示进度的相关代码
public class MyProgressBar extends ProgressBar {
private String mText;
private Paint mPaint;
private int remain;
public MyProgressBar(Context context) {
super(context);
initText();
}
public MyProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
initText();
}
public MyProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initText();
}
@Override
public synchronized void setProgress(int progress) {
setText(progress);
super.setProgress(progress);
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rect = new Rect();
this.mPaint.getTextBounds(this.mText, 0, this.mText.length(), rect);
int x = (getWidth() / 2) - rect.centerX();
int y = (getHeight() / 2) - rect.centerY();
canvas.drawText(this.mText, x, y, this.mPaint);
}
private void initText(){
this.mPaint = new Paint();
this.mPaint.setColor(Color.WHITE);
this.mPaint.setAntiAlias(true);
this.mPaint.setTextSize(34.f);
}
private void setText(int progress){
int i = (progress * 100)/this.getMax();
this.mText = String.valueOf(i) + "%("+getRemain()+" remaining)";
}
public int getRemain() {
return remain;
}
public void setRemain(int remain) {
this.remain = remain;
}
}
首先讲讲Android的Rect类的使用:
构造方法:
Public Constructors
Rect ()
Create a new empty Rect.
Rect(int left, int top, int right, int bottom)
Create a new rectangle with the specified coordinates.
Rect ( Rect r)
Create a new rectangle, initialized with the values in the specified rectangle (which is left unmodified).
常用方法:
final int centerX () //获取矩阵中心点(x,y)
Paint对象在开发过程中经常会用到,先看下Paint的常用Api
mPaint = new Paint();//初始化
mPaint.setColor(Color.RED);//设置颜色
mPaint.setARGB(255,255,255,0);//设置paint对象颜色,范围0~255
mPaint.setAlpha(200);//设置透明度 范围0~255
mPaint.setAntiAlias(true);//设置抗锯齿
mPaint.setStyle(Paint.Style.STROKE);//设置描边效果STROKE(描边) FILL(填充) 和FILL_AND_STROKE(同时作用)
mPaint.setStrokeWidth(4);//设置描边宽度
mPaint.setStrokeCap(Paint.Cap.ROUND);//设置圆角效果 BUTT(默认) ROUND(圆角) SQUARE(方形)
mPaint.setStrokeJoin(Paint.Join.MITER);//设置拐角风格 MITER(默认) ROUND(圆角填充) BEVEL(切除)
mPaint.setShader(new SweepGradient(200,200,Color.BLUE,Color.RED));//设置环形渲染器(着色器)
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));//设置图层混合模式
mPaint.setColorFilter(new LightingColorFilter(0x00ffff,0x000000));//设置颜色过滤器
mPaint.setFilterBitmap(true);//设置双线性过滤
mPaint.setMaskFilter(new BlurMaskFilter(10,BlurMaskFilter.Blur.NORMAL));//设置画笔遮罩滤镜,传入度数和样式
mPaint.setTextScaleX(2);//设置文本缩放倍数 默认是1
mPaint.setTextSize(38);//设置字体大小
mPaint.setTextAlign(Paint.Align.LEFT);//设置对其方式
mPaint.setUnderlineText(true);//设置下划线
String str="Paint测试效果";
Rect rect = new Rect();
mPaint.getTextBounds(str,0,str.length(),rect);//测量文本大小,将文本大小信息存放在rect中
mPaint.measureText(str);//获取文本的宽
mPaint.getFontMetrics();//获取字体度量对象
?以上就是直播系统开发,实现在进度条中显示文字显示进度的相关代码, 更多内容欢迎关注之后的文章