zoukankan      html  css  js  c++  java
  • 自定义虚线 DashLineView

    //自定义虚线控件 可以动态改变虚线颜色 虚线间距 虚线长度 虚线高度

    /**
    * 虚线控件 * */ public class DashLineView extends View { /** * 虚线颜色 */ private int dashColor; /** * 虚线间距 */ private float dashGap; /** * 虚线长度 */ private float dashWith; /** * 虚线高度 */ private float lineHegiht; private Paint paint; private Path path; private int widthScreen; public DashLineView(Context context) { super(context); init(context, null); } public DashLineView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public DashLineView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } private void init(Context context, AttributeSet attrs) { if (attrs != null) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DashLine); dashColor = typedArray.getColor(R.styleable.DashLine_dashColor, R.color.black); dashGap = typedArray.getDimension(R.styleable.DashLine_dashGap, 5); dashWith = typedArray.getDimension(R.styleable.DashLine_dashWith, 15); lineHegiht = typedArray.getDimension(R.styleable.DashLine_lineHeight, 3); typedArray.recycle(); } paint = new Paint(); path = new Path(); //widthScreen = ToolsManager.getInstance().getScreenWidth(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setStyle(Paint.Style.STROKE);// 空心 paint.setColor(dashColor); paint.setStrokeWidth(lineHegiht); path.moveTo(0, 1); path.lineTo(widthScreen, 1); // DashPathEffect 可以使用DashPathEffect来创建一个虚线的轮廓(短横线/小圆点),而不是使用实线 // float[] { 5, 5, 5, 5 }值控制虚线间距,密度 PathEffect effects = new DashPathEffect(new float[] { dashWith, dashGap, dashWith, dashGap }, 1); paint.setPathEffect(effects); canvas.drawPath(path, paint); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); widthScreen = getMeasuredWidth(); } }
    <declare-styleable name="DashLine">
            <attr name="dashColor" format="color" />
            <attr name="dashWith" format="dimension" />
            <attr name="dashGap" format="dimension" />
            <attr name="lineHeight" format="dimension" />
        </declare-styleable>
  • 相关阅读:
    使用LR编写windows sockets协议xml报文格式脚本实战
    使用LR编写HTTP协议Json报文格式接口脚本实战
    web类协议脚本-飞机订票系统示例
    使用LR编写下载类脚本
    python算法-选择排序
    python算法-冒泡排序
    用户在浏览器中输入一个url发生的奥秘
    浅谈cookie和session
    selenium加载配置参数,让chrome浏览器不出现‘Chrome正在受到自动软件的控制’的提示语,以及后台静默模式启动自动化测试,不占用桌面的方法
    Python之文件和目录操作
  • 原文地址:https://www.cnblogs.com/gfqFighting/p/4482102.html
Copyright © 2011-2022 走看看