Android nine patch - allow only a specific area of a background drawable to stretch

后端 未结 4 1629
傲寒
傲寒 2021-02-06 15:11

How does one go about creating a View that has boundaries with triangular perforations ?

I\'ve been using background drawables to achieve this so

4条回答
  •  遇见更好的自我
    2021-02-06 15:44

    You can draw a zigzag path in background of your view

    Rect rectZigzag = new Rect();
    private Path pathZigzag = new Path();
    private Paint paintZigzag;
    private void init(){
        this.paintZigzag = new Paint();
        this.paintZigzag.setColor(zigzagBackgroundColor);
        this.paintZigzag.setStyle(Style.FILL);
    }
    
    
    private void drawZigzag() {
        float left = rectZigzag.left;
        float right = rectZigzag.right;
        float top = rectZigzag.top;
        float bottom = rectZigzag.bottom;
        int width = (int) (right - left);
    
        pathZigzag.moveTo(right, bottom);
        pathZigzag.lineTo(right, top);
        pathZigzag.lineTo(left, top);
        pathZigzag.lineTo(left, bottom);
    
        int h = zigzagHeight;
        int seed = 2 * h;
        int count = width / seed;
        int diff = width - (seed * count);
        int sideDiff = diff / 2;
    
    
        float x = (float) (seed / 2);
        float upHeight = bottom - h;
        float downHeight = bottom;
    
        for (int i = 0; i < count; i++) {
            int startSeed = (i * seed) + sideDiff + (int) left;
            int endSeed = startSeed + seed;
    
            if (i == 0) {
                startSeed = (int) left + sideDiff;
            } else if (i == count - 1) {
                endSeed = endSeed + sideDiff;
            }
    
            this.pathZigzag.lineTo(startSeed + x, upHeight);
            this.pathZigzag.lineTo(endSeed, downHeight);
        }
    }
    

    refrence

提交回复
热议问题