4

我参考了这个链接:How do I make a dotted/dashed line in Android? , 并使用DashPathEffect. 但这对我不起作用?为什么?我的代码:

public class NoteEditText extends EditText {
    private Paint mPaint;

    public NoteEditText(Context context) {
        super(context);
    }

    public NoteEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setStrokeWidth(1);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(Color.DKGRAY);
        PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},1);  
        mPaint.setPathEffect(effects);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int height = this.getHeight();
        int lineHeight = this.getLineHeight();
        int lineNum = height / lineHeight;
        L.l("line count: " + lineNum);
        for (int i = 0; i < lineNum; i++) {
            int y = (i + 1) * lineHeight;
            canvas.drawLine(0, y, this.getWidth() - 1, y, mPaint);
        }
    }
}
4

4 回答 4

8

The method setPathEffect is not supported by hardware acceleration. By default it is turned on (I think since Android 4.0)

http://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported

You can turn off hardware acceleration inside the constructor with following code snippet:

setLayerType(View.LAYER_TYPE_SOFTWARE, null);

于 2014-06-28T13:38:18.193 回答
1
new float[]{5,5,5,5}

尝试

new float[]{5,10,15,20}
于 2012-09-15T14:27:21.170 回答
0

这应该有效。

EditText editText=(EditText) v.findViewById(android.R.id.text1);

editText.setPaintFlags(editText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

于 2012-09-13T07:57:07.090 回答
0

我认为您不应该使用“for 循环”来绘制 line.set setStrokeWidth($diameter) 可能有用。我已经写了一个支持这个功能的简单视图,详细在这里

于 2013-11-21T08:30:29.457 回答