android画虚线的自定义VIew

package com.yesway.ycarplus.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.util.AttributeSet;
import android.view.View;

/**
 * 画虚线的一个view
 * 
 * @author liulm
 *
 */
public class DottedLine extends View {

    private int color = Color.WHITE;

    public DottedLine(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setStyle(Style.STROKE);
        p.setColor(color);
        p.setStrokeWidth(10);
        PathEffect effects = new DashPathEffect(new float[] { 0, 10, 35, 2 }, 1);

        Path path = new Path();
        path.moveTo(0, 0);
        path.lineTo(0, getMeasuredHeight());
        path.lineTo(getMeasuredWidth(), getMeasuredHeight());

        p.setPathEffect(effects);
        canvas.drawPath(path, p);
    }

    /**
     * 设置颜色
     * 
     * @param color
     */
    public void setColor(int color) {
        this.color = color;
        invalidate();
    }

    /**
     * 设置颜色值
     * 
     * @param color
     */
    public void setColor(String color) {
        this.color = Color.parseColor(color);
        invalidate();
    }

}

 

转载于:https://www.cnblogs.com/tianshidechibang234/p/4535591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值