Android开发中如何理解RadioButton及路径绘制
导读:本文共2934字符,通常情况下阅读需要10分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: 这个例子是绘制多边形,多义形和路径,采用单选钮RadioButton来选择Polys 和Path示例:UI 设计为 上部分用来显示绘图内容,下部分为两个单选按钮 Polys ,Path。这样layout就和main.xml 不一样,main.xml只含一个com.pstreets.graphics2d.GuidebeeGraphics2DView。因此需在 re... ...
目录
(为您整理了一些要点),点击可以直达。这个例子是绘制多边形,多义形和路径,采用单选钮RadioButton来选择Polys 和Path示例:
UI 设计为 上部分用来显示绘图内容,下部分为两个单选按钮 Polys ,Path。这样layout就和main.xml 不一样,main.xml只含一个com.pstreets.graphics2d.GuidebeeGraphics2DView。因此需在 res\layout下新建一个polys.xml:
<?xmlversion=”1.0″encoding=”utf-8″?><LinearLayoutxmlns: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.GuidebeeGraphics2DViewandroid:id=”@+id/graphics2dview”android:layout_weight=”1″android:layout_width=”fill_parent”android:layout_height=”wrap_content”/><LinearLayoutxmlns:android=”http://schemas.android.com/apk/res/android”android:layout_width=”wrap_content”android:layout_height=”wrap_content”android:orientation=”horizontal”><RadioGroupandroid:layout_width=”wrap_content”android:orientation=”horizontal”android:textSize=”20dp”android:layout_height=”wrap_content”><RadioButtonandroid:text=”Polys”android:id=”@+id/radioPolys”android:layout_width=”wrap_content”android:textColor=”@color/black”android:checked=”true”android:layout_height=”wrap_content”></RadioButton><RadioButtonandroid: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 设为选中。
定义好Layout资源后,修改 Path.java
privateRadioButtonradioPoly;privateRadioButtonradioPath;publicvoidonCreate(BundlesavedInstanceState){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。完整代码如下:
1publicclassPathextendsGraphics2DActivity2implementsOnClickListener{34privateRadioButtonradioPoly;5privateRadioButtonradioPath;67publicvoidonCreate(BundlesavedInstanceState){8super.onCreate(savedInstanceState);9setContentView(R.layout.polys);10graphic2dView11=(GuidebeeGraphics2DView)12findViewById(R.id.graphics2dview);13radioPath=(RadioButton)findViewById(R.id.radioPath);14radioPoly=(RadioButton)findViewById(R.id.radioPolys);15radioPath.setOnClickListener(this);16radioPoly.setOnClickListener(this);17}1819@Override20protectedvoiddrawImage(){21if(radioPoly.isChecked()){22drawPolys();23}else{24drawPaths();25}26graphic2dView.refreshCanvas();2728}2930@Override31publicvoidonClick(Viewview){32drawImage();33}3435privatevoiddrawPaths(){36AffineTransformmat1;3738//Thepath.39com.mapdigit.drawing.geometry.Pathpath;4041//Colors42ColorredColor=newColor(0x96ff0000,true);43ColorgreenColor=newColor(0xff00ff00);44ColorblueColor=newColor(0x750000ff,true);4546Stringpathdata47="M6020Q-407060120Q160706020z";48mat1=newAffineTransform();49mat1.translate(30,40);50mat1.rotate(-30*Math.PI/180.0);51path=com.mapdigit.drawing.geometry.Path.fromString(pathdata);52//Clearthecanvaswithwhitecolor.53graphics2D.clear(Color.WHITE);5455graphics2D.setAffineTransform(newAffineTransform());56SolidBrushbrush=newSolidBrush(greenColor);57graphics2D.fill(brush,path);58graphics2D.setAffineTransform(mat1);5960brush=newSolidBrush(blueColor);61com.mapdigit.drawing.Penpen62=newcom.mapdigit.drawing.Pen(redColor,5);63graphics2D.setPenAndBrush(pen,brush);64graphics2D.draw(null,path);65graphics2D.fill(null,path);6667}6869privatevoiddrawPolys(){70AffineTransformmat1;7172//Colors73ColorredColor=newColor(0x96ff0000,true);74ColorgreenColor=newColor(0xff00ff00);75ColorblueColor=newColor(0x750000ff,true);7677Polylinepolyline;78Polygonpolygon;79Polygonpolygon1;8081Stringpointsdata182="59,45,95,63,108,105,82,139,39,140,11,107,19,65";83mat1=newAffineTransform();84mat1.translate(30,40);85mat1.rotate(-30*Math.PI/180.0);86polyline=newPolyline();87polygon=newPolygon();88polygon1=newPolygon();89Point[]points=Point.fromString(pointsdata1);90for(inti=0;i<points.length;i++){91polyline.addPoint(points[i].x,points[i].y);92polygon.addPoint(points[i].x,points[i].y);93polygon1.addPoint(points[i].x,points[i].y);94}95//Clearthecanvaswithwhitecolor.96graphics2D.clear(Color.WHITE);9798graphics2D.setAffineTransform(newAffineTransform());99SolidBrushbrush=newSolidBrush(greenColor);100graphics2D.fillPolygon(brush,polygon);101graphics2D.setAffineTransform(mat1);102103brush=newSolidBrush(blueColor);104com.mapdigit.drawing.Penpen105=newcom.mapdigit.drawing.Pen(redColor,5);106graphics2D.setPenAndBrush(pen,brush);107graphics2D.fillPolygon(null,polygon1);108graphics2D.drawPolyline(null,polyline);109110}111112}
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
Android开发中如何理解RadioButton及路径绘制的详细内容,希望对您有所帮助,信息来源于网络。