這篇文章將為大家詳細(xì)講解有關(guān)Android開發(fā)中如何理解RadioButton及路徑繪制,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。
目前創(chuàng)新互聯(lián)建站已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、邵武網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
這個(gè)例子是繪制多邊形,多義形和路徑,采用單選鈕RadioButton來選擇Polys 和Path示例:
UI 設(shè)計(jì)為 上部分用來顯示繪圖內(nèi)容,下部分為兩個(gè)單選按鈕 Polys ,Path。這樣layout就和main.xml 不一樣,main.xml只含一個(gè)com.pstreets.graphics2d.GuidebeeGraphics2DView。因此需在 res\layout下新建一個(gè)polys.xml:
<?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns: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” 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中做為一個(gè)分組,這里將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)為需要處理按鍵消息,所以定義了兩個(gè)RadioButton對象,可以通過findViewById獲取實(shí)例。因?yàn)閮蓚€(gè)RadioButton這里采用 同樣的處理方法,可以讓Path實(shí)現(xiàn)OnClickListener ,即:public class Path extends Graphics2DActivity implements OnClickListener。完整代碼如下:
1 public class Path extends Graphics2DActivity 2 implements OnClickListener { 3 4 private RadioButton radioPoly; 5 private RadioButton radioPath; 6 7 public void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.polys); 10 graphic2dView 11 = (GuidebeeGraphics2DView) 12 findViewById(R.id.graphics2dview); 13 radioPath = (RadioButton) findViewById(R.id.radioPath); 14 radioPoly = (RadioButton) findViewById(R.id.radioPolys); 15 radioPath.setOnClickListener(this); 16 radioPoly.setOnClickListener(this); 17 } 18 19 @Override 20 protected void drawImage() { 21 if (radioPoly.isChecked()) { 22 drawPolys(); 23 } else { 24 drawPaths(); 25 } 26 graphic2dView.refreshCanvas(); 27 28 } 29 30 @Override 31 public void onClick(View view) { 32 drawImage(); 33 } 34 35 private void drawPaths() { 36 AffineTransform mat1; 37 38 // The path. 39 com.mapdigit.drawing.geometry.Path path; 40 41 // Colors 42 Color redColor = new Color(0x96ff0000, true); 43 Color greenColor = new Color(0xff00ff00); 44 Color blueColor = new Color(0x750000ff, true); 45 46 String pathdata 47 = "M 60 20 Q -40 70 60 120 Q 160 70 60 20 z"; 48 mat1 = new AffineTransform(); 49 mat1.translate(30, 40); 50 mat1.rotate(-30 * Math.PI / 180.0); 51 path = com.mapdigit.drawing.geometry.Path.fromString(pathdata); 52 // Clear the canvas with white color. 53 graphics2D.clear(Color.WHITE); 54 55 graphics2D.setAffineTransform(new AffineTransform()); 56 SolidBrush brush = new SolidBrush(greenColor); 57 graphics2D.fill(brush, path); 58 graphics2D.setAffineTransform(mat1); 59 60 brush = new SolidBrush(blueColor); 61 com.mapdigit.drawing.Pen pen 62 = new com.mapdigit.drawing.Pen(redColor, 5); 63 graphics2D.setPenAndBrush(pen, brush); 64 graphics2D.draw(null, path); 65 graphics2D.fill(null, path); 66 67 } 68 69 private void drawPolys() { 70 AffineTransform mat1; 71 72 // Colors 73 Color redColor = new Color(0x96ff0000, true); 74 Color greenColor = new Color(0xff00ff00); 75 Color blueColor = new Color(0x750000ff, true); 76 77 Polyline polyline; 78 Polygon polygon; 79 Polygon polygon1; 80 81 String pointsdata1 82 = "59,45,95,63,108,105,82,139,39,140,11,107,19,65"; 83 mat1 = new AffineTransform(); 84 mat1.translate(30, 40); 85 mat1.rotate(-30 * Math.PI / 180.0); 86 polyline = new Polyline(); 87 polygon = new Polygon(); 88 polygon1 = new Polygon(); 89 Point[] points = Point.fromString(pointsdata1); 90 for (int i = 0; i < points.length; i++) { 91 polyline.addPoint(points[i].x, points[i].y); 92 polygon.addPoint(points[i].x, points[i].y); 93 polygon1.addPoint(points[i].x, points[i].y); 94 } 95 // Clear the canvas with white color. 96 graphics2D.clear(Color.WHITE); 97 98 graphics2D.setAffineTransform(new AffineTransform()); 99 SolidBrush brush = new SolidBrush(greenColor); 100 graphics2D.fillPolygon(brush, polygon); 101 graphics2D.setAffineTransform(mat1); 102 103 brush = new SolidBrush(blueColor); 104 com.mapdigit.drawing.Pen pen 105 = new com.mapdigit.drawing.Pen(redColor, 5); 106 graphics2D.setPenAndBrush(pen, brush); 107 graphics2D.fillPolygon(null, polygon1); 108 graphics2D.drawPolyline(null, polyline); 109 110 } 111 112 }
關(guān)于Android開發(fā)中如何理解RadioButton及路徑繪制就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
當(dāng)前題目:Android開發(fā)中如何理解RadioButton及路徑繪制
URL網(wǎng)址:http://jinyejixie.com/article16/peocdg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、域名注冊、軟件開發(fā)、品牌網(wǎng)站設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)