androidpathshapedrawable

Android: Blurred ShapeDrawable from a Path


I am trying to make a ShapeDrawable but when I resize it, Android draws a blurred line. I know that some example codes say this doesn't happen, so I can't understand what I am doing wrong. This is what I wrote:

Path pathTriangulo = new Path();
pathTriangulo.moveTo(0, 0);
pathTriangulo.lineTo(50, 0);
pathTriangulo.lineTo(50, 50);
pathTriangulo.lineTo(0, 0);
miImagen = new ShapeDrawable(new PathShape(pathTriangulo,50,50));
miImagen.getPaint().setColor(Color.BLACK);
miImagen.getPaint().setStyle(Paint.Style.STROKE);
miImagen.setBounds(0, 0, 5000, 5000); 

If I write miImagen.setBounds(0,0,50,50) it works perfectly :/ I'll be grateful if you could help me!


Solution

  • your shapedrawable should override onBoundsChange() and create a new PathShape based on the bounds. Use setShape() to apply the new PathShape to your shapedrawable

        miImagen = new ShapeDrawable(new PathShape(pathTriangulo,50,50)) {
            @Override
            protected void onBoundsChange(Rect bounds) {
                super.onBoundsChange(bounds);
                Path pathTriangulo = new Path();
                pathTriangulo.moveTo(0, 0);
                pathTriangulo.lineTo(50, 0);
                pathTriangulo.lineTo(50, 50);
                pathTriangulo.lineTo(0, 0);
                setShape( new PathShape(pathTriangulo, bounds.width(), bounds.height()));
    
            }
        };