android自定义虚线,Android自定义view的方式绘制虚线

Android自定义view绘制虚线

最近项目中有个需求,通过自定义view的方式绘制虚线

别的不多说先看一眼效果

d88bc07c3a995b4245b1cc884a8d3703.png

这个需求在我们的开发中应该是一个很常见的需求了吧,有人会说有更简单的实现方式,对,但是你试过么,比如网上提到的这种方式

代码如下

这里写图片描述

看样子好像已经成功了,最后在把这个作为background加进去,

结果在真机上跑一圈,发现显示的竟然是一条实线,在好几台手机上试都是这样,都是一条实线,后来查资料发现需要加上一句话

android:layerType=”software” . android:shape="line"完美解决

画水平虚线

1.直接建一个shape,设置stroke属性就行了,再将这个属性直接作为background的drawable属性引入就行了

注意在4.0以上的真机加一句

2.布局xml文件中直接引用

//4.0以上的加,不然真机中是实线

我的手机是华为 p9 系统7.0 最终发现问题没有效果,后来调整了 android:layout_height="1dp" 才发现,这样绘制出来的横向直线确实是成功了,但是是包裹在view四周的,这种先天性的缺陷导致了他无法在高度特别低的控件中使用

e20e71870be611872a4f34eb7404e2df.png

添加这句话就可以解决 android:shape="line"

最终的效果:

d88bc07c3a995b4245b1cc884a8d3703.png

其实我们可以通过自定义一个view的形式来实现这个效果

1.自定义一个java类继承view

/**

* Created by benchengzhou on 2018/3/16 15:47 .

* 作者邮箱: mappstore@163.com

* 功能描述: 一个横向的虚线view

* 类 名: HorizontalPartLineView

* 备 注: 网络参考链接: https://blog.csdn.net/cxmscb/article/details/51760528

* https://blog.csdn.net/xmxkf/article/details/51468648

* https://www.cnblogs.com/prophet-it/p/6651033.html

*/

public class HorizontalPartLineView extends View {

private int DEFAULT_SOLID_WIDTH = 1;

private int DEFAULT_HOLLOW_WIDTH = 1;

private int DEFAULT_BG_COLOR = 0xffffffff;

private int DEFAULT_SOLID_COLOR = 0xffdcdcdc;

private int SolidW;

private int hollowW;

private int SolidColor;

private int bgColor;

private int offset=0;

private Paint mPaint;

private int DEFAULT_LINE_HEIGHT = 30;

private int mWidth;

private int mHeight;

public HorizontalPartLineView(Context context) {

this(context, null);

}

public HorizontalPartLineView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public HorizontalPartLineView(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Horizontal_PartLine_View, defStyleAttr, 0);

//边框线宽

SolidW = a.getDimensionPixelSize(R.styleable.Horizontal_PartLine_View_solid_width, DEFAULT_SOLID_WIDTH);

hollowW = a.getDimensionPixelSize(R.styleable.Horizontal_PartLine_View_hollow_width, DEFAULT_HOLLOW_WIDTH);

SolidColor = a.getColor(R.styleable.Horizontal_PartLine_View_solid_color, DEFAULT_SOLID_COLOR);

bgColor = a.getColor(R.styleable.Horizontal_PartLine_View_bg_color, DEFAULT_BG_COLOR);

offset = a.getColor(R.styleable.Horizontal_PartLine_View_offset, 0);

if (SolidW % 2 != 0) {

SolidW++;

}

if (hollowW % 2 != 0) {

hollowW++;

}

if (SolidW == 0) {

new Throwable(new IllegalArgumentException("the value SolidW san not be zone !"));

}

if (hollowW == 0) {

new Throwable(new IllegalArgumentException("the value hollowW san not be zone !"));

}

a.recycle(); //使用完成之后记得回收

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

mWidth = MeasureSpec.getSize(widthMeasureSpec);

// int widthMode = MeasureSpec.getMode(widthMeasureSpec);

mHeight = MeasureSpec.getSize(heightMeasureSpec);

// int heightMode = MeasureSpec.getMode(heightMeasureSpec);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

Paint paint = new Paint();

paint.setAntiAlias(true);

paint.setColor(bgColor);

canvas.drawRect(0, 0, mWidth, mHeight, paint);

mPaint = new Paint();

mPaint.setStyle(Paint.Style.STROKE);

mPaint.setColor(SolidColor);

mPaint.setAntiAlias(true);

mPaint.setStrokeWidth(/*dip2px(ctx,2)*/mHeight);

Path path = new Path();

path.moveTo(0, 0);

path.lineTo(mWidth, 0);

PathEffect effects = new DashPathEffect(new float[]{SolidW, hollowW, SolidW, hollowW}, offset);

mPaint.setPathEffect(effects);

canvas.drawPath(path, mPaint);

}

}

2.创建自定义属性文件 attrs-line.xml

3.布局文件中使用

4.定义引用控件

xmlns:app="https://schemas.android.com/apk/res-auto"

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值