SeekBarWithText带字体的进度条

来源:互联网 发布:红米2不能清除数据 编辑:程序博客网 时间:2024/05/17 00:51

思路重新seekBar在对应进度的位置写上进度文字。

import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.widget.SeekBar;import com.huawei.tjbi.R;public class SeekBarWithText extends SeekBar {private int with;// 游标textprivate String text = "--";// 百分比private Double percent = 0.0;// 游标离字体的距离private int textPading = 20;public int getTextPading() {return textPading;}public void setTextPading(int textPading) {this.textPading = textPading;}public Double getPercent() {return percent;}public void setPercent(Double percent) {this.percent = percent;}public String getText() {return text;}public void setText(String text) {this.text = text;}public SeekBarWithText(Context context) {super(context);}public SeekBarWithText(Context context, AttributeSet attrs) {super(context, attrs);}public SeekBarWithText(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@SuppressLint("NewApi")@Overrideprotected synchronized void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);Paint paint = new Paint();paint.setColor(getResources().getColor(R.color.topic_seekBar_text_color));paint.setTextSize(35);float textWith = paint.measureText(getText());with = getWidth() - getPaddingLeft() - getPaddingRight();int thumbWith = 0;
<span style="white-space:pre"></span>//android系统4.1以下不能使用该方法if (Integer.parseInt(android.os.Build.VERSION.SDK) > 16) {thumbWith = getThumb().getIntrinsicWidth();} else {thumbWith = getResources().getDrawable(R.drawable.seekbar_thumb_icon).getIntrinsicWidth();}setMax(100);setProgress((int) (getPercent() * 100));float x = (float) (getPercent() * with);if (getPercent() > 0) {
<span style="white-space:pre"></span>//判断位置是否能写得下文字if (with > x) {if (with - x > textWith + thumbWith + getTextPading()) {canvas.drawText(getText(), x + thumbWith + getTextPading(),getHeight() / 3.0f, paint);} else {canvas.drawText(getText(), x - thumbWith - getTextPading()- textWith, getHeight() / 3.0f, paint);}} else {canvas.drawText(getText(), with - thumbWith - getTextPading()- textWith, getHeight() / 3.0f, paint);}}}}



0 0