How to draw filled polygon?

前端 未结 8 2099
生来不讨喜
生来不讨喜 2020-12-01 08:57

How to draw filled polygon in Android ?

8条回答
  •  爱一瞬间的悲伤
    2020-12-01 09:27

    Old question, but a trick for anyone who finds this. If you include a font with the desired polygon as a glyph you can use the drawText function to draw your polygon.

    The downside is you have to know ahead of time what shapes you'll need. The upside it is if you do know ahead of time you can include a nice shape library. This code assumes you have a font called shapes in your assets/fonts folder of your project.

                TypeFace shapesTypeFace = Typeface.createFromAsset(getAssets(), "fonts/shapes.ttf");
                Paint stopSignPaint = new Paint();
                stopSignPaint.setColor(Color.RED);
                //set anti-aliasing so it looks nice
                stopSignPaint.setAntiAlias(true);
                stopSignPaint.setTextSize(200);
                stopSignPaint.setTypeface(shapesTypeFace);
    
                //will show up as a box or question mark since 
                //the current display font doesn't have this glyph. 
                //open the shapes font in a tool like Character Map 
                //to copy and paste the glyph into your IDE
                //saving it as a variable makes it much easier to work with
                String hexagonGlyph = ""
                String triangleGlyph = ""
    
    
                ....whatever code you got...
    
    
                //arguments: text, x coordinate, y coordinate, paint
                 canvas.drawText(hexagonGlyph, 200, 100, stopSignPaint);
    
                //make it into a go sign
                stopSignPaint.setColor(Color.Green);
                 canvas.drawText(hexagonGlyph, 200, 100, stopSignPaint);
    
    
                //make a tiny one
                stopSignPaint.setTextSize(20);
                stopSignPaint.setColor(Color.RED);
                 canvas.drawText(hexagonGlyph, 200, 100, stopSignPaint);
    
    
                 //make a triangle
                 canvas.drawText(triangleGlyph, 200, 100, stopSignPaint);
    

提交回复
热议问题