鍍金池/ 教程/ Android/ RadioButton 多邊形及路徑繪制
Dialog 顯示圖像
線程 Bezier 曲線
創(chuàng)建應(yīng)用程序框架
引路蜂二維圖形庫簡介及顏色示例
Android 應(yīng)用基本概念
Intents 和 Intent Filters
安裝開發(fā)環(huán)境
Option Menu 畫筆示例
自定義對話框 Transform
數(shù)據(jù)綁定 Data Binding
概述
Broadcast Receiver 短信觸發(fā)示例
發(fā)布應(yīng)用
自定義 Adapter 顯示列表
RadioButton 多邊形及路徑繪制
訪問 Internet 繪製在線地圖
第一個應(yīng)用 Hello World
Activities
Button 畫刷示例
使用資源 Resources
Context Menu 繪制幾何圖形
用戶界面設(shè)計
引路蜂二維圖形繪制實例功能定義

RadioButton 多邊形及路徑繪制

這個例子是繪制多邊形,多義形和路徑,采用單選鈕 RadioButton 來選擇 Polys 和 Path 示例:

UI 設(shè)計為 上部分用來顯示繪圖內(nèi)容,下部分為兩個單選按鈕 Polys ,Path。這樣 layout 就和main.xml 不一樣,main.xml 只含一個 com.pstreets.graphics2d.GuidebeeGraphics2DView。因此需在 res\layout 下新建一個 polys.xml:

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)”
    android:orientation=”vertical”
    android:background=”@drawable/white”
 android:layout_width=”fill_parent”
 android:layout_height=”fill_parent”>
    <com.pstreets.graphics2d.GuidebeeGraphics2DView
     android:id=”@+id/graphics2dview”
     android:layout_weight=”1″
     android:layout_width=”fill_parent”
     android:layout_height=”wrap_content”/>
 <LinearLayout xmlns:android=”[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)”
  android:layout_width=”wrap_content” android:layout_height=”wrap_content”
  android:orientation=”horizontal”
>
  <RadioGroup
     android:layout_width=”wrap_content”
     android:orientation=”horizontal”
     android:textSize=”20dp”
     android:layout_height=”wrap_content”>
   <RadioButton android:text=”Polys”
       android:id=”@+id/radioPolys”
    android:layout_width=”wrap_content”
    android:textColor=”@color/black”
    android:checked=”true”
    android:layout_height=”wrap_content”>
   </RadioButton>
   <RadioButton android:text=”Path”
        android:id=”@+id/radioPath”
    android:layout_width=”wrap_content”
    android:textColor=”@color/black”
    android:layout_height=”wrap_content”>
   </RadioButton>
  </RadioGroup>
 </LinearLayout>

</LinearLayout>

RadioButton 需包含在 RadioGroup 中做為一個分組,這里將 Polys 設(shè)為選中。

定義好 Layout 資源后,修改 Path.java

private RadioButton radioPoly;
private RadioButton radioPath;

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.polys);
 graphic2dView
  = (GuidebeeGraphics2DView)
    findViewById(R.id.graphics2dview);
 radioPath = (RadioButton) findViewById(R.id.radioPath);
 radioPoly = (RadioButton) findViewById(R.id.radioPolys);
 radioPath.setOnClickListener(this);
 radioPoly.setOnClickListener(this);
}

應(yīng)為需要處理按鍵消息,所以定義了兩個 RadioButton 對象,可以通過 findViewById 獲取實例。因為兩個 RadioButton 這里采用同樣的處理方法,可以讓 Path 實現(xiàn) OnClickListener ,即:public class Path extends Graphics2DActivity implements OnClickListener。完整代碼如下:

public class Path extends Graphics2DActivity 
   implements OnClickListener {

    private RadioButton radioPoly;
    private RadioButton radioPath;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.polys);
        graphic2dView 
         = (GuidebeeGraphics2DView) 
           findViewById(R.id.graphics2dview);
        radioPath = (RadioButton) findViewById(R.id.radioPath);
        radioPoly = (RadioButton) findViewById(R.id.radioPolys);
        radioPath.setOnClickListener(this);
        radioPoly.setOnClickListener(this);
    }

    @Override
    protected void drawImage() {
        if (radioPoly.isChecked()) {
            drawPolys();
        } else {
            drawPaths();
        }
        graphic2dView.refreshCanvas();

    }

    @Override
    public void onClick(View view) {
        drawImage();
    }

    private void drawPaths() {
        AffineTransform mat1;

        // The path. 
        com.mapdigit.drawing.geometry.Path path;

        // Colors 
        Color redColor = new Color(0x96ff0000, true);
        Color greenColor = new Color(0xff00ff00);
        Color blueColor = new Color(0x750000ff, true);

        String pathdata 
           = "M 60 20 Q -40 70 60 120 Q 160 70 60 20 z";
        mat1 = new AffineTransform();
        mat1.translate(30, 40);
        mat1.rotate(-30 * Math.PI / 180.0);
        path = com.mapdigit.drawing.geometry.Path.fromString(pathdata);
        // Clear the canvas with white color.
        graphics2D.clear(Color.WHITE);

        graphics2D.setAffineTransform(new AffineTransform());
        SolidBrush brush = new SolidBrush(greenColor);
        graphics2D.fill(brush, path);
        graphics2D.setAffineTransform(mat1);

        brush = new SolidBrush(blueColor);
        com.mapdigit.drawing.Pen pen 
           = new com.mapdigit.drawing.Pen(redColor, 5);
        graphics2D.setPenAndBrush(pen, brush);
        graphics2D.draw(null, path);
        graphics2D.fill(null, path);

    }

    private void drawPolys() {
        AffineTransform mat1;

        // Colors 
        Color redColor = new Color(0x96ff0000, true);
        Color greenColor = new Color(0xff00ff00);
        Color blueColor = new Color(0x750000ff, true);

        Polyline polyline;
        Polygon polygon;
        Polygon polygon1;

        String pointsdata1 
        = "59,45,95,63,108,105,82,139,39,140,11,107,19,65";
        mat1 = new AffineTransform();
        mat1.translate(30, 40);
        mat1.rotate(-30 * Math.PI / 180.0);
        polyline = new Polyline();
        polygon = new Polygon();
        polygon1 = new Polygon();
        Point[] points = Point.fromString(pointsdata1);
        for (int i = 0; i < points.length; i++) {
            polyline.addPoint(points[i].x, points[i].y);
            polygon.addPoint(points[i].x, points[i].y);
            polygon1.addPoint(points[i].x, points[i].y);
        }
        // Clear the canvas with white color.
        graphics2D.clear(Color.WHITE);

        graphics2D.setAffineTransform(new AffineTransform());
        SolidBrush brush = new SolidBrush(greenColor);
        graphics2D.fillPolygon(brush, polygon);
        graphics2D.setAffineTransform(mat1);

        brush = new SolidBrush(blueColor);
        com.mapdigit.drawing.Pen pen 
           = new com.mapdigit.drawing.Pen(redColor, 5);
        graphics2D.setPenAndBrush(pen, brush);
        graphics2D.fillPolygon(null, polygon1);
        graphics2D.drawPolyline(null, polyline);

    }

}

http://wiki.jikexueyuan.com/project/android-development-tutorial/images/25.png" alt="" />