我试图用多色的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);
视图
我在MainMenuActivity中使用了这个方法.它给我的结果如下.我把这个链接称为https://azzits.wordpress.com/2013/11/17/customseekbar/
有没有办法画出这条垂直的有缺口线?我该如何绘制这条垂直线?
最佳答案
Progress bar with divider是@Nilesh Rathod提到的好看的地方.您可以使用canvas.drawRoundRect();而不是使用canvas.drawRect();简短的例子:
原文链接:https://www.f2er.com/android/430209.htmlfor (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.right,mPaint);
} else {
canvas.drawRoundRect(mSegment,mPaint);
}
mSegment.offset(mSegment.width() + gapWidth,0);
}
我将上述代码完全归功于上述链接的创建者,并且不要将其中的任何内容声称为我的,因为我只是说明了为达到预期效果而应该做出的改变.如果您遇到其他问题,请告诉我.