public class TextProgressbarextends LinearLayout {
public FlashTextViewtvThumb;
//public TextView tvThumb;
public ProgressBarseekBar;
public RelativeLayoutrelative_circle;
private ImageViewivCircle;
private int width =0;
private Handlerhandler;
private int progress;//进度
private int color01=0xffffffFF;
private int color02=0x33FFED71;
private int color03=0xffffffFF;
private int[]colors=new int[] {color01, color02,color03};
public TextProgressbar(Context context) {
super(context);
init();
}
public TextProgressbar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
LayoutInflater.from(getContext()).inflate(R.layout.view_thumb_text_seekbar, this);
setOrientation(LinearLayout.VERTICAL);
//tvThumb = (TextView) findViewById(R.id.tvThumb);
tvThumb = (FlashTextView) findViewById(R.id.tvThumb);
tvThumb.setmAnimating(true);
tvThumb.setColors(colors);
seekBar = (ProgressBar) findViewById(R.id.sbProgress);
relative_circle = (RelativeLayout) findViewById(R.id.relative_circle);
ivCircle = (ImageView) findViewById(R.id.iv_circle);
handler =new Handler();
}
public void setCircleBackground(Drawable drawable) {
ivCircle.setBackground(drawable);
}
public void setProgressTextColor(int color) {
tvThumb.setTextColor(color);
}
public void setProgressDrawable(Drawable drawable) {
seekBar.setProgressDrawable(drawable);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
upDateProgress();
}
/**
* 设置Thumb的文字
*
* @param text
*/
public void setThumbText(String text) {
tvThumb.setText(text);
}
/**
* 设置进度
*
* @param progress
*/
public void setProgress(int progress) {
seekBar.setProgress(progress);
upDateProgress();
}
/**
* 设置进度百分比
*
* @param num 0-100
*/
public void setProgress(int num, boolean isAnim) {
if (isAnim) {
if (num >=100) {
num =100;
}else if (num <=0) {
num =0;
}
final int finalNum = num;
new Thread(new Runnable() {
@Override
public void run() {
for (int i =0; i <=finalNum; i++) {
progress = i;
try {
Thread.sleep(30);
handler.post(new Runnable() {
@Override
public void run() {
seekBar.setProgress(progress);
upDateProgress();
}
});
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}else {
seekBar.setProgress(num);
upDateProgress();
}
}
private void upDateProgress() {
String content =tvThumb.getText().toString();
float contentWidth =tvThumb.getPaint().measureText(content) /2 +70;
int realWidth =width -seekBar.getPaddingLeft()-seekBar.getPaddingRight();
// int maxLimit = (int) (width - contentWidth - seekBar.getPaddingRight());
// int minLimit = seekBar.getPaddingLeft();
float percent = (float) (1.0 *seekBar.getProgress() /seekBar.getMax());
// int left = minLimit + (int) (realWidth * percent - contentWidth / 2.0);
// left = left <= minLimit ? minLimit : left >= maxLimit ? maxLimit : left;
float progressWidth = realWidth * percent +seekBar.getPaddingLeft();
int left = (int) (progressWidth -10);
if (left <0) left =0;
if (left > realWidth - contentWidth) left = (int) (realWidth - contentWidth);
tvThumb.setPadding(left, 0, 0, 0);
int circle = (int) (progressWidth - UIUtil.dip2px(4.5f));
if ((circle + UIUtil.dip2px(8)) >width) {
relative_circle.setPadding(circle - UIUtil.dip2px(0), 0, 0, 0);
}else {
relative_circle.setPadding(circle, 0, 0, 0);
}
}
}
文字跟随的进度条
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- CircleProgress 项目中要用到进度百分比跟随进度动的效果,在网上找了一下,没找到合适的,就自己写了一个...
- 成长记录-连载(三十六) ——我的第一篇五千字长文,说了什么,你一定想不到 并不是不想每天写公众号,而是之前思考怎...