前段时间在项目中遇到一个自定义控件,需要实现拖动一个类似SeekBar的东西来选择时间段。花费了一些时间和精力搞了出来。效果图如下



具体的代码和注释在源码中都有,这里只贴出关键代码

关键代码

 @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        String measureHeight = "获取文本高度";        paint.getTextBounds(measureHeight, 0, measureHeight.length(), textound);        //计算控件高度        timeheight = textound.height() + marginText + blockHeight + marginTimeBlock + seekBarHeight;        /** * 高度为时间控件所需的高度,宽度占满屏幕宽度 */        setMeasuredDimension(timeWidth, timeheight);    }
@Override    protected void onDraw(Canvas canvas) {        //绘制不可能选择的时间块        if (disSelectBlock != null && disSelectBlock.size() > 0) {            Rect rect = new Rect(0,0,blockWidth,blockHeight);            RectF rectf = new RectF();            rectf.top = marginText + textound.height();            rectf.bottom = textound.height() + blockHeight;            BitmapFactory.Options options = new BitmapFactory.Options();            options.outWidth = blockWidth;            options.outHeight = blockHeight;            Bitmap notSelectBg = BitmapFactory.decodeResource(getResources(),R.drawable.time_disselect_bg,options);            for (int i = 0; i < disSelectBlock.size(); i++) {                rectf.left =disSelectBlock.get(i) * blockWidth;                rectf.right = disSelectBlock.get(i) * blockWidth+blockWidth;                canvas.drawBitmap(notSelectBg,rect,rectf,null);            }        }        //绘制文字        paint.setColor(getResources().getColor(R.color.white));        int time = beginTime;        int lineStartY = 0;//绘制分割线的起始纵坐标        int lineEndX = textound.height() + blockHeight;//绘制分割线的终止纵坐标        for (int i = 0; i < timeBlockNum; i++) {            if (i % 2 == 0) {                String timeText = time + "时";                paint.getTextBounds(timeText, 0, timeText.length(), textound);                canvas.drawText(timeText, blockWidth + blockWidth * i - textound.height(), textound.height(), paint);                time++;                //短的分割线的起始纵坐标                lineStartY = textound.height() + blockHeight / 2;            } else {                //长的分割线的起始纵坐标                lineStartY = textound.height();            }            lineStartY += marginText;            //绘制时间块分割线            canvas.drawLine(blockWidth * i, lineStartY, blockWidth * i, lineEndX, paint);        }        //绘制最后端的一条分割短线        canvas.drawLine(timeWidth, lineStartY + blockHeight / 2, timeWidth, lineEndX, paint);        paint.setStrokeWidth(1);        paint.setColor(getResources().getColor(R.color.gray));        //绘制拖动条顶部的线条        canvas.drawLine(0, timeheight - seekBarHeight, timeWidth, timeheight - seekBarHeight, paint);        //绘制拖动条底部的线条        paint.setStrokeWidth(2);        canvas.drawLine(0, timeheight, timeWidth - 2, timeheight - 2, paint);        if (seekBarStart != 0) {            //绘制拖动条尾部            paint.setStrokeWidth(0);            paint.setColor(getResources().getColor(R.color.color_fabe00));            int disNum = seekBarEnd - seekBarStart;            int startX = seekBarStart * blockWidth + bodyDistance;            if (startX < blockWidth) {                startX = blockWidth;            } else if (startX > timeWidth - blockWidth - disNum * blockWidth) {                startX = timeWidth - blockWidth - disNum * blockWidth;            }            int endX = seekBarEnd * blockWidth + moveDistance + bodyDistance;            if (endX > timeWidth - blockWidth) {                endX = timeWidth - blockWidth;            } else if (bodyDistance != 0 && endX < startX + blockWidth * disNum) {                endX = startX + blockWidth * disNum;            } else if (endX < startX + blockWidth) {                endX = startX + blockWidth;            }            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {                canvas.drawRoundRect(startX, timeheight - seekBarHeight + 10, endX, timeheight - 10, 4, 4, paint);            } else {                canvas.drawRect(startX, timeheight - seekBarHeight + 10, endX, timeheight - 10, paint);            }            //绘制拖动条头部圆圈            paint.setColor(getResources().getColor(R.color.color_eb7938));            canvas.drawCircle(endX, timeheight - seekBarHeight / 2, seekBarHeight / 2, paint);            paint.setColor(getResources().getColor(R.color.white));            canvas.drawCircle(endX, timeheight - seekBarHeight / 2, seekBarHeight / 4, paint);        }    }

——————————————————————

源码地址

更多相关文章

  1. Android系统中读写和显示图片
  2. Android中View绘制流程
  3. Android中BaseAdapter的用法分析与理解
  4. OpenGL ES for Android(安卓)绘制矩形和正方形
  5. Android(安卓)SurfaceView 双缓冲机制
  6. Android(安卓)Window的添加和显示过程
  7. Android(安卓)P wakeup 亮屏流程
  8. Android-使用OpengGL实现的Canvas进行绘制(简单介绍)
  9. Android(安卓)文字绘制,多行文字绘制

随机推荐

  1. Android小应用——监控屏幕使用时间
  2. Android热修复和插件化(初识篇)
  3. 别再抱怨了,国内这么多优秀的Android资源
  4. Android(安卓)内功心法(1.6)——android
  5. 微软:WP不会授权给小OEM商以及山寨厂商
  6. Android中 3D 圆形旋转动态实现
  7. 【android 蓝牙开发——传统蓝牙】
  8. java/android String和List的区别
  9. [Android] 收录几个比较火的安卓网
  10. android DexClassLoader动态加载技术详解