如下圖所示,本文將介紹如何通過修改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"/>
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(), 10, 0, this.getHeight() - 10,
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);
.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();
}
* 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);
}
}
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/