Search code examples
androidgraphicspaintshapedrawablecolorfilter

draw a non-rectangular shape using 1) texture image and 2) ColorFilter


enter image description here

This is what i want to draw. It has

  • the shape i want (from four [x/y] coordinates)
  • the texture i want (from a bmp)
  • the color i want (using ColorFilter).

Solution

  • Programatically, this is how i achieved my goal in the end:

    Path path = new Path();
        path.moveTo(coord0X, coord0Y);
        path.lineTo(coord1X, coord1Y);
        path.lineTo(coord2X, coord2Y);
        path.lineTo(coord3X, coord3Y);
        path.lineTo(coord0X, coord0Y);
    
    ShapeDrawable shapeDrawable = new ShapeDrawable(new PathShape(path, dx, dy));
    shapeDrawable.setBounds(x, y, x+dx, y+dy);
    
    shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
    Shader fillShader = new BitmapShader(myTextureBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);          
    shapeDrawable.getPaint().setShader(fillShader);     
    

    I now get a parallelogram drawn with my texture BMP.

    To add a ColorFilter:

    shapeDrawable.setColorFilter(myColorFilter);