如下圖所示,本文將介紹如何通過修改EditText里的值,動態(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里追加如下代碼,以動態(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)就能夠動態(tài)的顯示對應(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 閱讀(967) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 潼南县| 安达市| 潮安县| 依兰县| 湖南省| 万州区| 库尔勒市| 奇台县| 阿拉尔市| 台南县| 威信县| 文登市| 大连市| 南宫市| 玉溪市| 哈尔滨市| 怀仁县| 会泽县| 慈溪市| 吴旗县| 扶余县| 临朐县| 札达县| 灵璧县| 化州市| 盐亭县| 峨眉山市| 密山市| 九江市| 上林县| 留坝县| 久治县| 天津市| 金昌市| 彩票| 巧家县| 内江市| 邹城市| 望奎县| 无极县| 大邑县|