0

I want to draw line of box with box corner radius.

Expected Output

But what i get

Code

@Override
    protected void onDraw(Canvas canvas) {
        if (bytes != null) {
            int[] gradientColors = new int[]{
                    Color.parseColor("#B3F90403"),
                    Color.parseColor("#B3E7D104"),
                    Color.parseColor("#B317A300")
            };

            float[] gradientColorPos = new float[]{
                    0.33f, 0.66f, 0.99f
            };

            paint.setShader(new LinearGradient(0, 0, 0f, getHeight() / 2, gradientColors, gradientColorPos, Shader.TileMode.MIRROR));


            float barWidth = getWidth() / density;
            float div = bytes.length / density;
            paint.setStrokeWidth(barWidth - gap);
            for (int i = 0; i < density; i++) {
                int count = 0;
                int bytePosition = (int) Math.ceil(i * div);
                int top = getHeight() + ((byte) (Math.abs(bytes[bytePosition]) + 128)) * getHeight() / 128;
                int col = Math.abs((getHeight() - top));
                for (int j = 0; j < col + 1; j += barWidth) {
                    float barX = (i * barWidth) + (barWidth / 2);
                    float y1 = getHeight() - ((barWidth + (gap / 2f)) * count);
                    float y2 = getHeight() - ((barWidth - gap / 2f) + ((barWidth + gap / 2f) * count));
                    canvas.drawLine(barX, y1, barX, y2, paint);
                    count++;
                }
            }
            super.onDraw(canvas);
        }
    }
Ali
  • 3,346
  • 4
  • 21
  • 56
  • Does this answer your question? [How to draw smooth / rounded path?](https://stackoverflow.com/questions/7608362/how-to-draw-smooth-rounded-path) – Stanislav Bondar Mar 31 '22 at 11:52
  • @StanislavBondar I already try this but not working. Your given example not provide gap between two box and it's only make round at top. – Ali Mar 31 '22 at 12:10
  • I'm not sure if I understand the issue correctly, but why don't you use `Canvas#drawRoundRect()` instead of `drawLine()`? Also, about half of that stuff should _not_ be in `onDraw()`, but I realize that it might be there just for testing currently. – Mike M. Mar 31 '22 at 12:12
  • @MikeM. *I'm not sure if I understand the issue correctly*. I'm trying to draw line with rounded corner box. and each box have gap. but if I use round RectF this not show proper line. instead of it's show one single box at some area. `canvas.drawRoundRect(new RectF(0,100,100,100),6,6,paint);` – Ali Mar 31 '22 at 12:34
  • You need a Bitmap Mask (please see this accepted Answer): https://stackoverflow.com/questions/23966272/android-mask-bitmap-on-canvas-gen-a-black-space – emandt Mar 31 '22 at 12:58
  • @MikeM. I don't have idea how to use drawRoundRect() to get proper output. – Ali Mar 31 '22 at 13:08
  • 1
    Well, I don't fully follow your draw routine, but simply replacing the `drawLine()` call with `canvas.drawRoundRect(barX, y2, barX + barWidth - gap, y1, 15, 15, paint);` gives me [this result](https://i.stack.imgur.com/sOyNg.png) in a quick mock-up. You'll have to adjust things, obviously, but that was just to give you the idea. – Mike M. Mar 31 '22 at 13:15
  • Okay thanks @MikeM. I' try to do some changes. – Ali Mar 31 '22 at 13:20
  • 1
    No problem. I realize it's not much, but it's too early in the morning for me to decipher that `onDraw()` routine. :-) I should also mention that you won't need the `paint.setStrokeWidth(barWidth - gap);` anymore, either. Your original method was basically drawing a piece of a very thick line for each one, but this new method is drawing an individual round rectangle for each instead. – Mike M. Mar 31 '22 at 13:30

0 Answers0