packagecom.bu_ish.blog;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.graphics.Color;importandroid.graphics.Paint;importandroid.os.Build;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.util.TypedValue;importandroid.view.View;importandroid.widget.LinearLayout;importandroid.widget.SeekBar;importjava.lang.reflect.Field;public class FloatingTextSeekBar extendsLinearLayout {privateSeekBar sb;privateFloatingTextView ftv;private String startText = "100";private int max = 49;private floatfloatingTextX;private static final String TAG = FloatingTextSeekBar.class.getName();publicFloatingTextSeekBar(Context context, AttributeSet attrs) {super(context, attrs);
setOrientation(VERTICAL);
sb= newSeekBar(context);
LayoutParams sbLayoutParams= newLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
sb.setLayoutParams(sbLayoutParams);
sb.setPadding(16, 0, 16, 0);try{
Class cls=sb.getClass().getSuperclass().getSuperclass();
Field field= cls.getDeclaredField("mMaxHeight");
field.setAccessible(true);
field.set(sb, PixelTool.dpToPx(context,5));
}catch (NoSuchFieldException |IllegalAccessException ex) {
Log.e(TAG,null, ex);
}
sb.setMax(max);
sb.setProgressDrawable(getResources().getDrawable(R.drawable.seek_bar_progress_drawable));
sb.setThumb(getResources().getDrawable(R.mipmap.ic_seek_bar_thumb));if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.LOLLIPOP)
sb.setSplitTrack(false);
addView(sb);
ftv= newFloatingTextView(context);
LayoutParams ftvLayoutParams= new LayoutParams(LayoutParams.MATCH_PARENT, PixelTool.dpToPx(context, 12));
ftvLayoutParams.topMargin= PixelTool.dpToPx(context, 2);
ftv.setLayoutParams(ftvLayoutParams);
ftv.setText(startText);
addView(ftv);
sb.setOnSeekBarChangeListener(newSeekBar.OnSeekBarChangeListener() {
@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, booleanfromUser) {float step = seekBar.getWidth() / (float) max;
floatingTextX= progress *step;
ftv.setText((progress+ Integer.parseInt(startText) / 100) * 100 + "");
}
@Overridepublic voidonStartTrackingTouch(SeekBar seekBar) {
}
@Overridepublic voidonStopTrackingTouch(SeekBar seekBar) {
}
});
}public voidsetStartText(String startText) {this.startText =startText;
}public void setMax(intmax) {this.max =max;
sb.setMax(max);
}private class FloatingTextView extendsView {privateString text;privateFloatingTextView(Context context) {super(context);
}private voidsetText(String text) {this.text =text;
invalidate();
}
@Overrideprotected voidonDraw(Canvas canvas) {super.onDraw(canvas);
Paint paint= newPaint();
paint.setColor(Color.parseColor("#ff5158e7"));float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics());
paint.setTextSize(textSize);
paint.setTextAlign(Paint.Align.CENTER);float textWidth =paint.measureText(text);if (floatingTextX - textWidth / 2
floatingTextX+= textWidth / 2;
}if (floatingTextX + textWidth / 2 >getRight()) {
floatingTextX-= textWidth / 2;
}
canvas.drawText(text, floatingTextX, getHeight(), paint);
}
}
}