android多边形按钮,RadioButton 多边形及路径绘制

这个例子是绘制多边形,多义形和路径,采用单选钮 RadioButton 来选择 Polys 和 Path 示例:

UI 设计为 上部分用来显示绘图内容,下部分为两个单选按钮 Polys ,Path。这样 layout 就和main.xml 不一样,main.xml 只含一个 com.pstreets.graphics2d.GuidebeeGraphics2DView。因此需在 res\layout 下新建一个 polys.xml:

android:orientation=”vertical”

android:background=”@drawable/white”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”>

android:id=”@+id/graphics2dview”

android:layout_weight=”1″

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”/>

android:layout_width=”wrap_content” android:layout_height=”wrap_content”

android:orientation=”horizontal”

>

android:layout_width=”wrap_content”

android:orientation=”horizontal”

android:textSize=”20dp”

android:layout_height=”wrap_content”>

android:id=”@+id/radioPolys”

android:layout_width=”wrap_content”

android:textColor=”@color/black”

android:checked=”true”

android:layout_height=”wrap_content”>

android:id=”@+id/radioPath”

android:layout_width=”wrap_content”

android:textColor=”@color/black”

android:layout_height=”wrap_content”>

RadioButton 需包含在 RadioGroup 中做为一个分组,这里将 Polys 设为选中。

定义好 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);

}

应为需要处理按键消息,所以定义了两个 RadioButton 对象,可以通过 findViewById 获取实例。因为两个 RadioButton 这里采用同样的处理方法,可以让 Path 实现 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);

}

}

25.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值