您的位置:首页 > 移动开发 > Android开发

Android中绘制虚线

2016-08-08 17:03 309 查看

概述

今天给大家介绍两种实现虚线的方式

方式一:利用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>


下面对属性进行一下介绍:

width:线段的高度

color:线段的颜色

dashWidth:线段宽度

dashGap:线段之间间隔宽度

然后在布局文件中使用如下:

<View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dash_view"
android:layerType="software"/>


在4.0的设备上,虚线会变成实线,我们需要加入 android:layerType=”software”属性

方式二:自定义View绘制虚线

这种方式更加灵活,可以实现水平和竖直方向的虚线,当然实现的难度也要高于第一种方式

先看效果图

水平虚线



竖直虚线



最后给出代码的实现

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());
Log.d(TAG, "onMeasure: "+widthSize+"----"+heightSize);
Log.d(TAG, "dashOrientation: "+dashOrientation);
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();
}
}


关于这种通过自定义View实现虚线的具体代码,大家可以链接至我的Github

自定义View实现绘制虚线
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: