Paint 代表了Canvas上的畫筆
以下範列記錄幾種Canvas繪製幾何圖向的方法
canvas.drawArc (扇形)
canvas.drawCircle(圓)
canvas.drawOval(橢圓)
canvas.drawPoint(點)
canvas.drawRect(矩形)
canvas.drawRoundRect(圓角矩形)
canvas.drawLine(線)
cnavas.drawPath(路徑)
DrawCircle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void drawCircle(){ | |
Paint p = new Paint(); | |
p.setStrokeWidth(3); | |
p.setColor(Color.RED); | |
p.setStyle(Paint.Style.STROKE); | |
Canvas canvas = new Canvas(bitmap); | |
canvas.drawCircle(240, 240, 150, p); | |
} |

DrawArc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void drawArc(){ | |
Paint p = new Paint(); | |
p.setStrokeWidth(2); | |
p.setColor(Color.RED); | |
p.setStyle(Paint.Style.STROKE); | |
Canvas canvas = new Canvas(bitmap); | |
RectF oval1=new RectF(100, 100,400,400); | |
canvas.drawArc(oval1, 30, 300, true, p); | |
} |

DrawOval
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void drawOval() | |
{ | |
Paint p = new Paint(); | |
p.setStrokeWidth(10); | |
p.setColor(Color.RED); | |
p.setStyle(Paint.Style.STROKE); | |
Canvas canvas = new Canvas(bitmap); | |
RectF oval=new RectF(); | |
oval.left=100; | |
oval.top=100; | |
oval.right=400; | |
oval.bottom=300; | |
canvas.drawOval(oval, p); | |
} |

DrawLine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void drawLine(){ | |
Paint p = new Paint(); | |
p.setStrokeWidth(10); | |
p.setColor(Color.RED); | |
p.setStyle(Paint.Style.STROKE); | |
Canvas canvas = new Canvas(bitmap); | |
canvas.drawLine(60, 40, 200, 400, p); | |
} |

DrawPath
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void drawPath(){ | |
Paint p = new Paint(); | |
p.setStrokeWidth(5); | |
p.setColor(Color.RED); | |
p.setStyle(Paint.Style.STROKE); | |
Canvas canvas = new Canvas(bitmap); | |
Path path =new Path(); | |
path.moveTo(200, 200); | |
path.lineTo(400, 200); | |
path.lineTo(400, 400); | |
path.close(); | |
canvas.drawPath(path, p); | |
} |

DrawPoints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void drawPoints() | |
{ | |
Paint p = new Paint(); | |
p.setStrokeWidth(20); | |
p.setColor(Color.RED); | |
p.setStyle(Paint.Style.STROKE); | |
Canvas canvas = new Canvas(bitmap); | |
canvas.drawPoint(60, 390, p); | |
canvas.drawPoints(new float[]{60,300,100,300,200,300}, p); | |
} |

DrawRect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void drawRect() | |
{ | |
Paint p = new Paint(); | |
p.setStrokeWidth(2); | |
p.setColor(Color.RED); | |
p.setStyle(Paint.Style.STROKE); | |
Canvas canvas = new Canvas(bitmap); | |
RectF rect = new RectF(100,100,400,400); | |
canvas.drawRect(rect, p); | |
} |

DrawRoundRect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void drawRoundRect() | |
{ | |
Paint p = new Paint(); | |
p.setStrokeWidth(2); | |
p.setColor(Color.RED); | |
p.setStyle(Paint.Style.FILL); | |
p.setAntiAlias(true); | |
Canvas canvas = new Canvas(bitmap); | |
RectF rect = new RectF(100, 50, 300, 500); | |
canvas.drawRoundRect(rect, 20, 20, p); | |
} |

完整程式碼: https://github.com/evanchen76/CanvasSample
沒有留言:
張貼留言