android 线条选择器

如下图所示,本文将介绍如何通过修改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

Paintpaint = new Paint();
paint.setStrokeWidth(
this .width);
canvas.drawLine(
this .getWidth(), 10 , 0 , this .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的值,并刷新界面

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

5.给EditText追加TextChangedListener,实现其中的onTextChanged方法:

@Override
public void onTextChanged(CharSequencetext, 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/















更多相关文章

  1. Android(安卓)自定义View实例之进度圆环
  2. 关于Android(安卓)traslateAnimation的坐标系
  3. Android继承ViewGroup自定义流式布局
  4. Android中调用Paint的measureText()方法取得字符串显示的宽度值
  5. Android(安卓)RecyclerView —— 自定义分割线
  6. Android(安卓)TabLayout宽度在平板上未铺满解决方案
  7. Android利用ffmpeg做视频裁剪
  8. Android(安卓)ApiDemos示例解析(73):Graphics->Points
  9. Android(安卓)热补丁动态修复框架小结

随机推荐

  1. Mac Yosemite下Android(安卓)Studio环境
  2. android HTTPURLConnection解决不能访问H
  3. Android调用系统摄像头拍照并剪裁压缩
  4. Android(安卓)百分比布局
  5. linux安装安卓开发工具android studio
  6. ANDROID ADB工具使用
  7. android 常用intent
  8. 通过XML设置屏幕方向(android:screenOrie
  9. 安卓巴士Android开发神贴整理
  10. android:configChanges属性总结