如下图所示,本文将介绍如何通过修改EditText里的值,动态的改变所画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里追加如下代码,以动态改变width的值,并刷新界面

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

 

5.给EditText追加TextChangedListener,实现其中的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里值的变化,旁边白色区域内就能够动态的显示对应宽度的线条


本文受启发于:http://nxsfan.co.uk/blog/2010/06/18/ondraw-drawing-a-simple-line-on-a-background/

另外,最近发现了个不错的jar包下载网站:http://jarfiles.pandaidea.com/















posted on 2013-01-30 11:27 Ying-er 阅读(951) 评论(0)  编辑  收藏 所属分类: Android

只有注册用户登录后才能发表评论。


网站导航: