多色自定义搜索栏

Aja*_*y P 6 java android android-layout

我试图用多色的android创建自定义搜索栏.我试过下面的代码

customseekbar.java

int proBarWidth = getWidth();
int proBarHeight = getHeight();
int thumboffset = getThumbOffset();
int lastproX = 0;
int proItemWidth, proItemRight;
for (int i = 0; i < mproItemsList.size(); i++) {
proItem proItem = mproItemsList.get(i);
Paint proPaint = new Paint();
proPaint.setColor(getResources().getColor(proItem.color));

proItemWidth = (int) (proItem.proItemPercentage
        * proBarWidth / 100);

proItemRight = lastproX + proItemWidth;

// for last item give right of the pro item to width of the
// pro bar
if (i == mproItemsList.size() - 1
        && proItemRight != proBarWidth) {
    proItemRight = proBarWidth;
}
Rect proRect = new Rect();
proRect.set(lastproX, thumboffset / 2, proItemRight,
        proBarHeight - thumboffset / 2);
canvas.drawRect(proRect, proPaint);
lastproX = proItemRight;
}
super.onDraw(canvas);
Run Code Online (Sandbox Code Playgroud)

视图

<mypackage.customseekbar
android:id="@+id/customseekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"
android:progressDrawable="@android:color/transparent"
android:thumb="@drawable/seek_thumb_normal"
android:thumbOffset="12dp" />
Run Code Online (Sandbox Code Playgroud)

我用过这种方法MainMenuActivity.它给我的结果如下.我推荐了这个链接https://azzits.wordpress.com/2013/11/17/customseekbar/ 在此输入图像描述

但我期待下面的事情 在此输入图像描述

有没有办法画出这条垂直的有缺口线?我该如何绘制这条垂直线?

小智 3

正如@Nilesh Rathod 提到的,带有分隔线的进度条是一个很好看的地方。您可以使用canvas.drawRoundRect(),而不是使用canvas.drawRect();简短的例子:

for (int i = 0; i < NUM_SEGMENTS; i++) {
        float loLevel = i / (float) NUM_SEGMENTS;
        float hiLevel = (i + 1) / (float) NUM_SEGMENTS;
        if (loLevel <= level && level <= hiLevel) {
            float middle = mSegment.left + NUM_SEGMENTS * segmentWidth * (level - loLevel);
            canvas.drawRoundRect(mSegment.left, mSegment.top, middle, mSegment.bottom, mPaint);
            mPaint.setColor(mBackground);
            canvas.drawRoundRect(middle, mSegment.top, mSegment.right, mSegment.bottom, mPaint);
        } else {
            canvas.drawRoundRect(mSegment, mPaint);
        }
        mSegment.offset(mSegment.width() + gapWidth, 0);
    }
Run Code Online (Sandbox Code Playgroud)

我将上面的代码完全归功于上述链接的创建者,并且不声称其中任何代码是我的,因为我只是说明为达到预期效果而应进行的更改。如果您遇到其他问题,请告诉我。