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);
}
}
}
网友评论