2017-02-08 40 views
1

我需要這種形狀(矩形或任何方式)在View機器人上繪製。只有一條曲線邊。 1個純色,彎曲側透明。如何畫這個?安卓繪製矩形只有曲線一邊自定義視圖

public class CurveView extends View { 
    @Override 
    protected void onDraw(Canvas canvas) { 
     // how to ???? 
    } 
} 

這樣的:

enter image description here

感謝。

+2

'canvas.drawPath(路徑,油漆)' – pskink

+1

閱讀此頁上的信息,[Android的路徑(https://developer.android.com /reference/android/graphics/Path.html),然後編寫一些代碼。如果您遇到錯誤或結果與您期望的不同,請回來並提出具體問題。 – Alex

+0

@Alex android路徑和圖形複雜,需要很長時間。你能幫我畫嗎? – grizzly

回答

1

解決方案:

UPDATE:

public class MyView extends View { 

    private Paint paint; 
    private Path path; 

    public MyView(Context context) { 
     super(context); 
     init(); 
    } 

    public init() { 

     path = new Path(); 

     paint = new Paint(); 
     paint.setAntiAlias(true); 
     paint.setDither(true); 
     paint.setStyle(Paint.Style.Fill); 
     paint.setColor(Color.RED); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     canvas.drawColor(Color.TRANSPARENT); 
     paint.setShader(null); 

     float width = getWidth(); 
     float height = getHeight(); 

     path.moveTo(0, 0); 

     path.lineTo(0, height); 

     path.lineTo(width, height); 

     path.lineTo(width, 0); 

     path.cubicTo(4*width/6, 3*height/4, 2*width/6, 3*height/4, 0, 0); 

     paint.setColor(Color.RED); 
     paint.setStyle(Paint.Style.FILL); 
     canvas.drawPath(path, paint); 

    } 

} 
+0

您能否顯示您定義「繪畫」變量和「路徑」的部分?謝謝。 –

+0

@FaustinoGagneten更新完成。查看。寫完後重建你的視圖。 – grizzly