android, how to draw dotted line in edittext

廉价感情. 提交于 2019-12-03 18:14:12

问题


I refered to this link: How do I make a dotted/dashed line in Android?, and used DashPathEffect. But this does not work for me? why? my code:

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);
        }
    }
}

回答1:


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);




回答2:


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

try

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



回答3:


This should work.

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

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




回答4:


I don't think you should use the "for loop" to draw line.set setStrokeWidth($diameter) maybe useful. I have write a simple view supporting this function,detail Here



来源:https://stackoverflow.com/questions/12401311/android-how-to-draw-dotted-line-in-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!