虚线控件有时会用到,横的虚线好实现,而竖的虚线则需要自定义,效果如下:
代码:
(1)自定义控件类:
package com.vvpinche.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;
import com.comotech.vv.R;
import com.vvpinche.util.CLog;
/**
* 画虚线
*
* @author chenwenbiao
* @date 2014-5-15
* @version
*/
public class DashedLine extends View {
private String TAG = getClass().getName();
/**
* 类型,0表示横线,1表示竖线
*/
private int type = 0;
public DashedLine(Context context){
this(context, null);
}
public DashedLine(Context context, AttributeSet attrs) {
this(context, attrs , 0);
}
public DashedLine(Context context, AttributeSet attrs , int defStyle) {
super(context, attrs , defStyle);
TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
R.styleable.MyDashLine);
//获取自定义属性和默认值
type = mTypedArray.getInt(R.styleable.MyDashLine_type , 0);
mTypedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
CLog.d(TAG , "onDraw=====>");
// TODO Auto-generated method stub
super.onDraw(canvas);
measure(0, 0);
/**
* 打印控件长宽
*/
CLog.d(TAG , "onDraw getWidth=====>" + getWidth());
CLog.d(TAG , "onDraw getHeight=====>" + getHeight());
CLog.d(TAG , "onDraw type=====>" + type);
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.DKGRAY);
Path path = new Path();
if(type == 1){
path.moveTo(0,0);
path.lineTo(0,getHeight());
}
else {
path.moveTo(0, 0);
path.lineTo(getWidth(), 0);
}
PathEffect effects = new DashPathEffect(new float[] { 5, 5, 5, 5 }, 1);
paint.setPathEffect(effects);
canvas.drawPath(path, paint);
}
}
(2)示例布局:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:ptr="http://schemas.android.com/apk/res/com.comotech.vv" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical" android:paddingLeft="20dp" > <!-- 虚线层 --> <LinearLayout android:id="@+id/user_route_line_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <com.vvpinche.view.DashedLine android:id="@+id/up_dashedLine" android:layout_width="20dp" android:layout_height="45dp" ptr:type="1" /> <com.vvpinche.view.DashedLine android:id="@+id/middle_dashedLine" android:layout_width="16dp" android:layout_height="1dp" ptr:type="0" /> <com.vvpinche.view.DashedLine android:id="@+id/down_dashedLine" android:layout_width="20dp" android:layout_height="120dp" ptr:type="1" /> </LinearLayout> </LinearLayout>
(3)属性配置文件attrs.xml
<?xml version="1.0" encoding="UTF-8"?> <resources> <!-- 虚线自定义属性 --> <declare-styleable name="MyDashLine"> <attr name="type" format="integer" /> </declare-styleable> </resources>
实用简单吧,呵呵。