Android畫虛線的問題
阿新 • • 發佈:2019-01-11
之前做標籤的時候,做過包圍整個控制元件的虛線邊框,當時利用了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的方法就不顯虛線,有時間繼續測試!!!!!