android,如何在edittext中绘制虚线

时间:2012-09-13 07:19:59

标签: android android-edittext draw ondraw dotted-line

我提到了这个链接: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 个答案:

答案 0 :(得分:7)

硬件加速不支持setPathEffect方法。默认情况下它已打开(我认为自Android 4.0以来)

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

您可以使用以下代码段关闭构造函数内的硬件加速:

  

setLayerType(View.LAYER_TYPE_SOFTWARE,null);

答案 1 :(得分:1)

new float[]{5,5,5,5}

尝试

new float[]{5,10,15,20}

答案 2 :(得分:0)

这应该有用。

  

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

     

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

答案 3 :(得分:0)

我认为你不应该使用&#34; for循环&#34;绘制line.set setStrokeWidth($ diameter)可能很有用。我写了一个支持这个函数的简单视图,详细信息Here