Android開(kāi)發(fā)速成簡(jiǎn)潔教程十二:引路蜂二維圖形庫(kù)
AndroidGraphics2DTutorial定義了應(yīng)用的主Activity,下面就可以開(kāi)始寫每個(gè)具體的二維繪圖示例。不同的例子將盡量采用不同的UI控件:Menu,Content Menu,Dialog,Custom Dialog,Button等等。例子采用了引路蜂二維圖形庫(kù),引路蜂二維圖形庫(kù)Graphics 2D API實(shí)現(xiàn)了移動(dòng)平臺(tái)(Java ME,Blackberry,iPhone,Android,Windows Phone)上圖形引擎,它能夠以一種統(tǒng)一的方式處理各種基本圖形(Shape),路徑(Path),文本(Texts),適量字體及圖像。 簡(jiǎn)單的說(shuō)來(lái),Graphics 2D API實(shí)現(xiàn)了與之對(duì)應(yīng)的Java SE上類似的二維圖形庫(kù)API。
主要功能如下:
- 支持各種基本圖形:曲線,矩形,橢圓等;支持繪制任意幾何圖形
- 支持在圖形,文體,圖象上的碰撞檢測(cè)
- 增強(qiáng)的顏色扶持及顏色管理
- 控制圖形繪制的質(zhì)量
- 填充,外框,各種線條繪制
- 二維圖形變換
- 矢量字體
- 從左到右,從右到左,從上到下顯示文體
- 反走樣
- 透明度支持
- 圖標(biāo),及圖象繪制
詳細(xì)的內(nèi)容可以參見(jiàn) Silverlight 引路蜂二維圖形庫(kù)示例
我們?cè)?a target="_blank">Android簡(jiǎn)明開(kāi)發(fā)教程九:創(chuàng)建應(yīng)用程序框架 中定義了一個(gè)基類Graphics2DActivity作為所有示例Activity的父類:
- public abstract class Graphics2DActivity extends Activity{
- protected Graphics2D graphics2D
- =SharedGraphics2DInstance.graphics2d;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- protected abstract void drawImage();
- public void onStart() {
- super.onStart();
- drawImage();
- }
- }
其中g(shù)raphics2D為圖形畫板對(duì)象(Canvas)是以width x height 的二維整型數(shù)組來(lái)表示的。這個(gè)數(shù)組的每個(gè)值為一個(gè)32為整數(shù)。格式為ARGB,分別代表透明度,紅色,綠色,藍(lán)色。在畫板上的繪制操作(點(diǎn),線,多邊形, 填充等)是修改這些顏色值。
R.layout.main 中可以使用GuidebeeGraphics2DSurfaceView 或是GuidebeeGraphics2DView 來(lái)作為畫板的顯示結(jié)果。
抽象方法protected abstract void drawImage();用來(lái)繪制不同的內(nèi)容。
修改com.pstreets.graphics2d.example.Colors 來(lái)使用引路蜂二維圖形庫(kù)繪制不同的顏色,如果以前用過(guò)Java SE或是.Net Framework,你會(huì)覺(jué)得引路蜂二維圖形庫(kù)提供的API和它們非常相似,代碼很好理解。
- public class Colors extends Graphics2DActivity{
- protected void drawImage(){
- /**
- * The solid (full opaque) red color in the ARGB space
- */
- Color redColor = new Color(0xffff0000);
- /**
- * The semi-opaque green color in the ARGB space (alpha is 0x78)
- */
- Color greenColor = new Color(0x7800ff00,true);
- /**
- * The semi-opaque blue color in the ARGB space (alpha is 0x78)
- */
- Color blueColor = new Color(0x780000ff,true);
- /**
- * The semi-opaque yellow color in the ARGB space ( alpha is 0x78)
- */
- Color yellowColor = new Color(0x78ffff00,true);
- /**
- * The dash array
- */
- int dashArray[] = { 20 ,8 };
- graphics2D.clear(Color.WHITE);
- SolidBrush brush=new SolidBrush(redColor);
- graphics2D.fillOval(brush,30,60,80,80);
- brush=new SolidBrush(greenColor);
- graphics2D.fillOval(brush,60,30,80,80);
- Pen pen=new Pen(yellowColor,10,Pen.CAP_BUTT,Pen.JOIN_MITER,dashArray,0);
- brush=new SolidBrush(blueColor);
- graphics2D.setPenAndBrush(pen,brush);
- graphics2D.fillOval(null,90,60,80,80);
- graphics2D.drawOval(null,90,60,80,80);
- }
- }
Colors Activity非常簡(jiǎn)單,除View之外,沒(méi)有其它UI。 按“Back”后可以退回示例列表顯示UI。