2014年9月15日 星期一

Android 開發(六十七) Triangle drawable

最近有個需求,需要我們利用程式來畫三角形,當下小弟是一點頭緒都沒有,
還好平時有在讀別人code的習慣,就想到了可以使用Path來畫我們需要的圖形,

首先我們必須先選出三角形的三個頂點,


 Point a = new Point(paddingLeft+0+stroke, Math.max(canvas.getHeight()/3*2-(int)TriHeight-stroke,0));
     Point b = new Point(paddingLeft+(int)TriBottom*2 +stroke, Math.max(canvas.getHeight()/3*2-(int)TriHeight-stroke,0));
     Point c = new Point(paddingLeft+(int)TriBottom+stroke, Math.max(canvas.getHeight()/3*2-stroke,0));


上面的三個頂點,會畫出一個倒三角形






會置放在canvas的3分之2的位置,在決定了三個頂點之後,接著就只需要利用PATH將圖畫出來即可,


     Path path = new Path();
     path.setFillType(FillType.EVEN_ODD);
     path.moveTo(a.x, a.y);
     path.lineTo(b.x, b.y);
     path.lineTo(c.x, c.y);
     path.lineTo(a.x, a.y);
     path.close();


上面的程式讓我們可以填滿三角形,並描繪出該三角型。 詳細的程式碼可以參考 https://gist.github.com/nightbear1009/ad4d9d96faeb7a756931

沒有留言:

張貼留言