如下圖所示,本文將介紹如何通過修改EditText里的值,動(dòng)態(tài)的改變所畫Line的寬度(只介紹大概思路)


          1.定義一個(gè)layout,用于放置Line

          <LinearLayout android:id="@+id/ll_feature_width_legend"
                      android:layout_width
          ="58dp"
                      android:layout_height
          ="45dp"
                      android:layout_marginLeft
          ="15dp"
                      android:layout_toRightOf
          ="@id/tv_feature_width_edit"
                      android:background
          ="#FFFFFF"/>

           


          2.在自定義View里畫Line

          Paint paint = new Paint();
          paint.setStrokeWidth(
          this.width);
                          canvas.drawLine(
          this.getWidth(), 100this.getHeight() - 10,
                                  paint);

           

          其中,width就是EditText里傳過來的寬度


          3.將自定義的View放到【1】的layout里

          widthLayout = (LinearLayout) view
                          .findViewById(R.id.ll_feature_width_legend);
                  widthLegend 
          = new DBLayerListSymbolView(this.activity, width);
                  widthLayout.addView(widthLegend);

           

          其中, DBLayerListSymbolView是自定義的View,width是EditText傳過來的寬度


          4.在自定義的View里追加如下代碼,以動(dòng)態(tài)改變width的值,并刷新界面

          /**
               * change symbol draw width
               * 
               * 
          @param width
               
          */
              
          public void changeWidth(float width) {
                  
          this.width = width;
                  invalidate();
              }

           

          5.給EditText追加TextChangedListener,實(shí)現(xiàn)其中的onTextChanged方法:

          @Override
                      
          public void onTextChanged(CharSequence text, int arg1, int arg2,
                              
          int arg3) {
                          
          float width = Float.parseFloat(text.toString());
                          
          if (width >= 0 && width <= 20) {
                              mWidth 
          = width;
                              
          widthLegend.changeWidth(width);
                          } 

                      }

           

          這樣,隨著EditText里值的變化,旁邊白色區(qū)域內(nèi)就能夠動(dòng)態(tài)的顯示對(duì)應(yīng)寬度的線條


          本文受啟發(fā)于:http://nxsfan.co.uk/blog/2010/06/18/ondraw-drawing-a-simple-line-on-a-background/

          另外,最近發(fā)現(xiàn)了個(gè)不錯(cuò)的jar包下載網(wǎng)站:http://jarfiles.pandaidea.com/















          posted on 2013-01-30 11:27 Ying-er 閱讀(974) 評(píng)論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 克拉玛依市| 武义县| 调兵山市| 台南市| 定南县| 靖边县| 金堂县| 南康市| 从江县| 综艺| 江永县| 竹北市| 鄂温| 崇仁县| 齐河县| 扶绥县| 松溪县| 牡丹江市| 汉源县| 淳化县| 嘉峪关市| 吉木乃县| 宁德市| 贵德县| 丹江口市| 浦县| 将乐县| 永安市| 皋兰县| 文山县| 漠河县| 芒康县| 泸州市| 五台县| 拉萨市| 吴江市| 桃源县| 佛教| 宁陵县| 墨竹工卡县| 海林市|