之前做标签的时候,做过包围整个控件的虚线边框,当时利用了shape来实现
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="4dp"/>
<stroke android:width="1dp" android:dashWidth="4dp" android:dashGap="4dp"/>
</shape>
把这个shape文件设置成TextView的背景,给TextView设置一个padding,就能实现带虚线框的标签。
这次想直接给一个虚线的分割线。按照惯性思维我也用shape编写了一个文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="3px"
android:color="#FF0000"
android:dashWidth="10px"
android:dashGap="10px" />
</shape>
哈哈哈,依照之前的思想把它设置为一个view的背景,显示不出来,在网上查了一下原因,是这样给我指引的
http://blog.csdn.net/qiuqingpo/article/details/40394677
我照着测试了,也是没有效果。就想着直接自定义吧,感谢作者SmallLee
https://github.com/SmallLee/DashView 成功解救了我
这个自定义view的属性
<!--自定义虚线属性-->
<declare-styleable name="DashView">
<attr name="dashWidth" format="dimension" />
<attr name="lineWidth" format="dimension" />
<attr name="lineHeight" format="dimension" />
<attr name="lineColor" format="color" />
<attr name="dashOrientation" format="integer" />
</declare-styleable>
自定义控件代码
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by Administrator on 2016/8/26.
*/
public class DashView extends View {
private static final String TAG = "DashView";
public static final int DEFAULT_DASH_WIDTH = 100;
public static final int DEFAULT_LINE_WIDTH = 100;
public static final int DEFAULT_LINE_HEIGHT = 10;
public static final int DEFAULT_LINE_COLOR = 0x9E9E9E;
/**虚线的方向*/
public static final int ORIENTATION_HORIZONTAL = 0;
public static final int ORIENTATION_VERTICAL = 1;
/**默认为水平方向*/
public static final int DEFAULT_DASH_ORIENTATION = ORIENTATION_HORIZONTAL;
/**间距宽度*/
private float dashWidth;
/**线段高度*/
private float lineHeight;
/**线段宽度*/
private float lineWidth;
/**线段颜色*/
private int lineColor;
private int dashOrientation;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int widthSize;
private int heightSize;
public DashView(Context context) {
this(context,null);
}
public DashView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.DashView);
dashWidth = typedArray.getDimension(R.styleable.DashView_dashWidth,DEFAULT_DASH_WIDTH);
lineHeight = typedArray.getDimension(R.styleable.DashView_lineHeight, DEFAULT_LINE_HEIGHT);
lineWidth = typedArray.getDimension(R.styleable.DashView_lineWidth, DEFAULT_LINE_WIDTH);
lineColor = typedArray.getColor(R.styleable.DashView_lineColor, DEFAULT_LINE_COLOR);
dashOrientation = typedArray.getInteger(R.styleable.DashView_dashOrientation,DEFAULT_DASH_ORIENTATION);
mPaint.setColor(lineColor);
mPaint.setStrokeWidth(lineHeight);
typedArray.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
widthSize = MeasureSpec.getSize(widthMeasureSpec)-getPaddingLeft()-getPaddingRight();
heightSize = MeasureSpec.getSize(heightMeasureSpec - getPaddingTop() - getPaddingBottom());
if(dashOrientation == ORIENTATION_HORIZONTAL){
不管在布局文件中虚线高度设置为多少,控件的高度统一设置为线段的高度
setMeasuredDimension(widthSize, (int) lineHeight);
}else{
//当为竖直方向时,控件宽度设置为虚线的高度
setMeasuredDimension((int) lineHeight, heightSize);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (dashOrientation){
case ORIENTATION_VERTICAL:
drawVerticalLine(canvas);
break;
default:
drawHorizontalLine(canvas);
}
}
/**
* 画水平方向虚线
* @param canvas
*/
public void drawHorizontalLine(Canvas canvas){
float totalWidth = 0;
canvas.save();
float[] pts = {0,0,lineWidth,0};
//在画线之前需要先把画布向下平移办个线段高度的位置,目的就是为了防止线段只画出一半的高度
//因为画线段的起点位置在线段左下角
canvas.translate(0,lineHeight/2);
while(totalWidth<=widthSize){
canvas.drawLines(pts,mPaint);
canvas.translate(lineWidth + dashWidth,0);
totalWidth += lineWidth + dashWidth;
}
canvas.restore();
}
/**
* 画竖直方向虚线
* @param canvas
*/
public void drawVerticalLine(Canvas canvas){
float totalWidth = 0;
canvas.save();
float[] pts = {0,0,0,lineWidth};
//在画线之前需要先把画布向右平移半个线段高度的位置,目的就是为了防止线段只画出一半的高度
//因为画线段的起点位置在线段左下角
canvas.translate(lineHeight/2,0);
while(totalWidth<=heightSize){
canvas.drawLines(pts,mPaint);
canvas.translate(0,lineWidth + dashWidth);
totalWidth += lineWidth + dashWidth;
}
canvas.restore();
}
}
特次记录一下
我在布局中的使用
<com.xxxxxxx.widget.DashView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="10dp"
app:dashOrientation="0"
app:dashWidth="2dp"
app:lineColor="@color/grar_ef"
app:lineHeight="1dp"
app:lineWidth="4dp" />
我还是想知道为什么我用解决shape的方法就不显虚线,有时间继续测试!!!!!