Android 自定义虚线段长和点宽

1、在attrs中自定义所需属性:

<declare-styleable name = "Dashedline">
    <attr name = "lineColor" format= "color" />
    <attr name = "lineWidth" format= "float"/>
</declare-styleable >

lineColor表示虚线的颜色,lineWidth虚线点的间隔和虚线点宽。

2、自定义继承View 的Dashedline:

/**
* 自定义虚线
*
*/
public class Dashedline extends View {

/**
 * 画笔
 */
private Paint paint = null;
/**
 * 设置虚线的宽度
 */
private Path path = null;
private PathEffect pe = null;

public Dashedline(Context context, AttributeSet attrs) {
    super(context, attrs);

    // 通过R.styleable.dashedline获得我们在attrs.xml中定义的
    // <declare-styleable name="dashedline"> TypedArray
    TypedArray dashedline = context.obtainStyledAttributes(attrs, R.styleable.Dashedline);

    // 我们在attrs.xml中<declare-styleable name="dashedline">节点下
    // 添加了<attr name="lineColor" format="color" />
    // 表示这个属性名为lineColor类型为color。当用户在布局文件中对它有设定值时
    // 可通过TypedArray获得它的值当用户无设置值是采用默认值0XFF00000

    // <attr name = "lineWidth" format= "float"/>
    // 表示这个属性名为lineWidth类型为float。当用户在布局文件中对它有设定值时
    // 可通过TypedArray获得它的值当用户无设置值是采用默认值2.0F线之间的间隔和虚线段宽
    int lineColor = dashedline.getColor(R.styleable.Dashedline_lineColor, 0XFF000000);
    float lineWidth = dashedline.getFloat(R.styleable.Dashedline_lineWidth, 2.0F);
    dashedline.recycle();
    this.paint = new Paint();
    this.path = new Path();
    this.paint.setStyle(Paint.Style.STROKE);
    this.paint.setColor(lineColor);
    this.paint.setAntiAlias(true);
    this.paint.setStrokeWidth(CommentUtils.dip2px(getContext(), lineWidth));
    float[] arrayOfFloat = new float[4];
    arrayOfFloat[0] = CommentUtils.dip2px(getContext(), lineWidth);
    arrayOfFloat[1] = CommentUtils.dip2px(getContext(), lineWidth);
    arrayOfFloat[2] = CommentUtils.dip2px(getContext(), lineWidth);
    arrayOfFloat[3] = CommentUtils.dip2px(getContext(), lineWidth);
    this.pe = new DashPathEffect(arrayOfFloat, CommentUtils.dip2px(getContext(), lineWidth));

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    this.path.moveTo(0.0F, 0.0F);
    this.path.lineTo(getMeasuredWidth(), 0.0F);
    this.paint.setPathEffect(this.pe);
    canvas.drawPath(this.path, this.paint);
    }
}

3、用到的转dp的工具类CommentUtils:

public class CommentUtils {
/**
 * convert the dip to the pix
 */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);

        }
 }

4、在用到虚线的地方引入 xmlns:dash=”http://schemas.android.com/apk/res/com.example.lines”
其中dash为自定义的控件属性代号,com.example.lines本应用包名:

<com.example.lines.Dashedline
    android:layout_width="match_parent"
    android:layout_height="5dp"
    dash:lineColor="#999999"
    dash:lineWidth="5" />

其中com.example.lines.Dashedline为自定义的控件所在全类名。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值