How to create white border around bitmap?

后端 未结 8 1718
萌比男神i
萌比男神i 2021-02-12 19:24

For example I want a white border of 10pixel around all 4 side of the bitmap. I am not using it for imageview I am currently using this code to crop image. May I know how I coul

相关标签:
8条回答
  • 2021-02-12 19:27

    Try this it will also add border to your canvas

        canvas.drawLine(0, 0, canvas.getWidth(), 0, paint2);
            canvas.drawLine(0, 0, 0, canvas.getHeight(), paint2);
            canvas.drawLine(0, canvas.getHeight(), canvas.getWidth(),
                    canvas.getHeight(), paint2);
            canvas.drawLine(canvas.getWidth(), 0, canvas.getWidth(),
                    canvas.getHeight(), paint2);
    
    0 讨论(0)
  • 2021-02-12 19:28

    Its not elegant but you can always just draw a rectangle behind it, you already have the code to do this and any performance impact is going to be unnoticeable

    0 讨论(0)
  • 2021-02-12 19:31

    You can draw 4 rectangles after painting your bitmap's stuff.

    point 0,0,3,sizey
    point 0,0,sizex,3
    point 0,sizey-3,sizex,sizey
    point sizex-3,0,sizex,sizey
    
    0 讨论(0)
  • 2021-02-12 19:34

    As for a way of doing this. You make your bitmap bigger than the one your adding to it and then fill the canvas with the background you want. If you need to add other effects you can look into the canvas options for clipping the rect and adding rounded corners and such.

    RectF targetRect = new RectF(left+10, top+10, left + scaledWidth, top + scaledHeight);
    Bitmap dest = Bitmap.createBitmap(newWidth+20, newHeight+20, source.getConfig());
    Canvas canvas = new Canvas(dest);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(source, null, targetRect, null);
    
    0 讨论(0)
  • 2021-02-12 19:40

    A super easy way of doing it would be to set the ImageView background to white and add a padding value.

    If that doesn't work, create a FrameLayout with w/h of wrap_content, set its background to white, put the ImageView in there, and set the ImageView's margins to the desired border width.

    0 讨论(0)
  • 2021-02-12 19:41

    You can create your targetRectangle 20px wider and 20px higher

    RectF targetRect = new RectF(left, top, left + scaledWidth + 20, top + scaledHeight + 20);
    

    and paint the background white

    0 讨论(0)
提交回复
热议问题