别的不多说先看一眼效果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A3tuskSd-1584088203517)(https://i.imgur.com/TAWRgEb.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四周的,这种先天性的缺陷导致了他无法在高度特别低的控件中使用
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hH6mvpeC-1584088203518)(https://i.imgur.com/Tu2mvzU.png)]
添加这句话就可以解决
android:shape="line"
最终的效果:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vs1QfHLZ-1584088203519)(https://i.imgur.com/TAWRgEb.png)]
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);
}
}
xmlns:app="http://schemas.android.com/apk/res-auto"