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


          1.定義一個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,實現(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)的顯示對應寬度的線條


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

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















          posted on 2013-01-30 11:27 Ying-er 閱讀(967) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 开江县| 沙河市| 铜陵市| 蓬莱市| 巴青县| 平凉市| 白山市| 邢台市| 赫章县| 夹江县| 阳新县| 醴陵市| 丰镇市| 岑巩县| 济阳县| 临城县| 蒙阴县| 台东县| 方城县| 永靖县| 阜新市| 石门县| 克拉玛依市| 千阳县| 天长市| 佛山市| 宝鸡市| 昌黎县| 龙海市| 旌德县| 辰溪县| 景洪市| 永昌县| 大埔区| 文山县| 渝北区| 金寨县| 呼玛县| 长治市| 龙陵县| 团风县|