2011-02-02 64 views
1

我畫4條線(canvas.drawLine)爲了繪製一個矩形。有沒有可能填充矩形區域? (我知道android有一個rectDraw。我的只是好奇)onDraw填充形狀創建與canvas.drawLine

在此先感謝。


ok ..我也是一個路徑創建了一個路徑段。遵守代碼..請解釋如何填補內部區域?

`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);` 

回答

2

如果您將線段繪製爲路徑,則可以填充矩形。但是,爲了填補由四個不相連但相互交叉的線段組成的區域,我想你必須編寫自己的例程。搜索「洪水填充」或「種子填充」。

+0

感謝我帶你去看看。 – Blackbelt 2011-02-03 14:08:01

1

不使用的drawLine但是,創建Path對象:

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); 

} 
+0

感謝您的回答! – Blackbelt 2015-10-08 16:43:23