android 线条选择器
16lz
2021-01-26
android 线条选择器
android:layout_width ="58dp"
android:layout_height ="45dp"
android:layout_marginLeft ="15dp"
android:layout_toRightOf ="@id/tv_feature_width_edit"
android:background ="#FFFFFF" />
paint.setStrokeWidth( this .width);
canvas.drawLine( this .getWidth(), 10 , 0 , this .getHeight() - 10 ,
paint);
.findViewById(R.id.ll_feature_width_legend);
widthLegend = new DBLayerListSymbolView( this .activity,width);
widthLayout.addView(widthLegend);
*changesymboldrawwidth
*
* @param width
*/
public void changeWidth( float width){
this .width = width;
invalidate();
}
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里的值,动态的改变所画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/
更多相关文章
- Android(安卓)自定义View实例之进度圆环
- 关于Android(安卓)traslateAnimation的坐标系
- Android继承ViewGroup自定义流式布局
- Android中调用Paint的measureText()方法取得字符串显示的宽度值
- Android(安卓)RecyclerView —— 自定义分割线
- Android(安卓)TabLayout宽度在平板上未铺满解决方案
- Android利用ffmpeg做视频裁剪
- Android(安卓)ApiDemos示例解析(73):Graphics->Points
- Android(安卓)热补丁动态修复框架小结