onDraw fill shape create with canvas.drawLine

☆樱花仙子☆ 提交于 2019-12-12 09:08:59

问题


i draw 4 lines (canvas.drawLine) in order to draw a rectangle. Is there any possibility to fill the area of the rectangle? (I know that android have a rectDraw. Mine is only curiosity)

thanks in advance.


ok.. I ve also a path created a path segment. Following the code.. Please can u explain how fill the internal area?

    `Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);            
     paint.setStrokeWidth(2);
     paint.setColor(android.graphics.Color.RED);
     paint.setStyle(Paint.Style.FILL_AND_STROKE);
     paint.setAntiAlias(true);
     path.reset();
     path.setFillType(Path.FillType.INVERSE_EVEN_ODD);      
     path.moveTo(mPin[0].getX() + 10, mPin[0].getY() + 10);
     path.lineTo(mPin[1].getX() + 10, mPin[1].getY() + 10);

     path.moveTo(mPin[1].getX() + 10, mPin[1].getY() + 10);
     path.lineTo(mPin[3].getX() + 10, mPin[3].getY() + 10);

     path.moveTo(mPin[3].getX() + 10, mPin[3].getY() + 10);
     path.lineTo(mPin[2].getX() + 10, mPin[2].getY() + 10);

     path.moveTo(mPin[2].getX() + 10, mPin[2].getY() + 10);
     path.lineTo(mPin[0].getX() + 10, mPin[0].getY() + 10);
     paint.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));
     c.drawPath(path, paint);`

回答1:


You could fill the rect if you drew your line segments as a Path. But to just fill an area bounded by four unconnected but intersecting line segments, I think you'd have to write your own routine. Search for "flood fill" or "seed fill".




回答2:


Dont use drawLine but, create Path object :

protected void onDraw(Canvas canvas) {


    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.parseColor("#FFA800"));


    Path path = new Path();

    path.moveTo(0, 0);
    path.lineTo(getWidth() / 2, 0);
    path.lineTo(getWidth(), getHeight()/2);
    path.lineTo(getWidth() / 2, getHeight());
    path.lineTo( 0, getHeight());
    path.lineTo( 0, 0);

    canvas.drawPath(path, paint);

}


来源:https://stackoverflow.com/questions/4875673/ondraw-fill-shape-create-with-canvas-drawline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!