当前位置:   article > 正文

Android自定义SeekBar,带开始值结束值和Thumb上方滑动的Text_android seekbar滑动块带文字

android seekbar滑动块带文字

之前根据网友的博文写了一个Demo(点击打开链接)但是这个Demo用到attr,layout color,还有其他的类,给使用者造成很多麻烦,我想,这样的自定义控件为啥不用一个类来完成呢,这样直接复制过来就能用,于是我彻底的封装了这个自定义的SeekBar,做到了一个类完成所有功能,而且代码量也不多。效果图:


    原理,其实我不是自定义SeekBar,我是自定义了一个RelativeLayout,里面放了三个TextView和一个SeekBar,用代码控制他们的位置,实现动态显示值。

    首先,向自定义的RelativeLayout用代码添加子控件,这样就不用XML来布局了

  1. private void addChildens() {
  2. textView = new ProgressTextView(mContext);
  3. textView.setId(View.generateViewId());
  4. LayoutParams lpTextView = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 40);
  5. textView.setLayoutParams(lpTextView);
  6. addView(textView);
  7. mSeekBar = new SeekBar(mContext);
  8. mSeekBar.setId(View.generateViewId());
  9. mSeekBar.setMax(seekBarMax);
  10. mSeekBar.setMinimumHeight(12);
  11. if (thumDrawable > 0) {
  12. mSeekBar.setThumb(ContextCompat.getDrawable(mContext, thumDrawable));
  13. }
  14. if (seekBarDrawable > 0) {
  15. mSeekBar.setBackgroundResource(seekBarDrawable);
  16. }
  17. mSeekBar.setPadding(0, 0, 0, 0);
  18. mSeekBar.setThumbOffset(0);
  19. LayoutParams lpSeekBar = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  20. lpSeekBar.addRule(RelativeLayout.BELOW, textView.getId());
  21. mSeekBar.setLayoutParams(lpSeekBar);
  22. addView(mSeekBar);
  23. mTvStart = new TextView(mContext);
  24. LayoutParams lpStart = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  25. lpStart.addRule(ALIGN_PARENT_LEFT);
  26. lpStart.addRule(RelativeLayout.BELOW, mSeekBar.getId());
  27. mTvStart.setLayoutParams(lpStart);
  28. addView(mTvStart);
  29. mTvEnd = new TextView(mContext);
  30. LayoutParams lpEnd = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  31. lpEnd.addRule(ALIGN_PARENT_RIGHT);
  32. lpEnd.addRule(RelativeLayout.BELOW, mSeekBar.getId());
  33. mTvEnd.setLayoutParams(lpEnd);
  34. addView(mTvEnd);
  35. }
    然后就是自定义的一个浮标并显示SeekBar的值:

  1. class ProgressTextView extends View {
  2. private int mHeight;
  3. private int mWidth;
  4. private double mOneProgressWidth;
  5. private int mCurProgress = 0;
  6. private String mProgressText = "";
  7. private Paint mPaint;
  8. private float mThumbOffset;
  9. private int mTextSize = 12;
  10. private float thumWidth;
  11. private String mTextColor = "#303F9F";//default color of column
  12. public ProgressTextView(Context context) {
  13. super(context);
  14. initObserver();
  15. }
  16. @RequiresApi(api = Build.VERSION_CODES.M)
  17. public ProgressTextView(Context context, AttributeSet attrs) {
  18. super(context, attrs);
  19. if (null != context && attrs != null) {
  20. mThumbOffset = thumWidth / 2;
  21. }
  22. initObserver();
  23. }
  24. private void initObserver() {
  25. ViewTreeObserver vto = getViewTreeObserver();
  26. vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
  27. @Override
  28. public boolean onPreDraw() {
  29. mHeight = getMeasuredHeight();
  30. mWidth = getMeasuredWidth();
  31. initPaint();
  32. initData();
  33. return true;
  34. }
  35. });
  36. }
  37. private void initPaint() {
  38. mPaint = new Paint();
  39. mPaint.setTextSize(mTextSize);
  40. mPaint.setTextAlign(Paint.Align.CENTER);
  41. mPaint.setColor(Color.parseColor(mTextColor));
  42. }
  43. private void initData() {
  44. mOneProgressWidth = (double) (mWidth - 2 * mThumbOffset) / (seekBarMax);
  45. }
  46. public void setTextSize(int mTextSize) {
  47. this.mTextSize = mTextSize;
  48. invalidate();
  49. }
  50. @Override
  51. protected void onDraw(Canvas canvas) {
  52. initPaint();
  53. drawText(canvas);
  54. super.onDraw(canvas);
  55. }
  56. //设置字体居中显示
  57. private void drawText(Canvas canvas) {
  58. float x = (float) (mCurProgress * mOneProgressWidth);
  59. float textWidth = mPaint.measureText(mProgressText);
  60. float textOffset = textWidth / 2;
  61. if (x + textOffset > mWidth - mThumbOffset) {//超过view的右边
  62. float exWidth = x + textOffset - (mWidth - mThumbOffset);
  63. x -= exWidth;//避免超过右边
  64. }
  65. if (x + mThumbOffset < textOffset) {//超过左边
  66. float exWidth = textOffset - (x + mThumbOffset);
  67. x += exWidth;//避免超过左边
  68. }
  69. canvas.translate(mThumbOffset, 0);
  70. canvas.drawText(mProgressText, x, mHeight, mPaint);
  71. }
  72. //设置显示的进度位置和字符串
  73. public void setProgress(int progress, String showText) {
  74. mCurProgress = progress;
  75. mProgressText = showText;
  76. invalidate();
  77. }
  78. }
我还写了监听器和各种setter,保证这个自定义View的可操作性

Demo源码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/325771
推荐阅读
相关标签
  

闽ICP备14008679号